1 # $Id: Mail.pm,v 1.1 2005/08/17 21:46:16 don Exp $
7 Debbugs::Mail -- Outgoing Mail Handling
11 use Debbugs::Mail qw(send_mail_message get_addresses);
13 my @addresses = get_addresses('blah blah blah foo@bar.com')
14 send_mail_message(message => <<END, recipients=>[@addresses]);
25 =item :all -- all functions that can be exported
36 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
37 use base qw(Exporter);
40 use POSIX ":sys_wait_h";
41 use Time::HiRes qw(usleep);
43 use Debbugs::MIME qw(encode_rfc1522);
44 use Debbugs::Config qw(:config);
45 use Params::Validate qw(:types validate_with);
48 ($VERSION) = q$Revision: 1.1 $ =~ /^Revision:\s+([^\s+])/;
49 $DEBUG = 0 unless defined $DEBUG;
52 @EXPORT_OK = qw(send_mail_message get_addresses encode_headers);
53 $EXPORT_TAGS{all} = [@EXPORT_OK];
57 # We set this here so it can be overridden for testing purposes
58 our $SENDMAIL = $config{sendmail};
62 my @addresses = get_addresses('don@debian.org blars@debian.org
63 kamion@debian.org ajt@debian.org');
65 Given a string containing some e-mail addresses, parses the string
66 using Mail::Address->parse and returns a list of the addresses.
71 return map { $_->address() } map { Mail::Address->parse($_) } @_;
76 =head2 send_mail_message
78 send_mail_message(message => $message,
79 recipients => [@recipients],
80 envelope_from => 'don@debian.org',
86 =item message -- message to send out
88 =item recipients -- recipients to send the message to. If undefed or
89 an empty arrayref, will use '-t' to parse the message for recipients.
91 =item envelope_from -- envelope_from for outgoing messages
93 =item encode_headers -- encode headers using RFC1522 (default)
95 =item parse_for_recipients -- use -t to parse the message for
96 recipients in addition to those specified. [Can be used to set Bcc
97 recipients, for example.]
101 Returns true on success, false on failures. All errors are indicated
106 sub send_mail_message{
107 my %param = validate_with(param => \@_,
108 spec => {sendmail_arguments => {type => ARRAYREF,
109 default => [qw(-odq -oem -oi)],
111 parse_for_recipients => {type => BOOLEAN,
114 encode_headers => {type => BOOLEAN,
117 message => {type => SCALAR,
119 envelope_from => {type => SCALAR,
122 recipients => {type => ARRAYREF|UNDEF,
127 my @sendmail_arguments = qw(-odq -oem -oi);
128 push @sendmail_arguments, '-f', $param{envelope_from} if exists $param{envelope_from};
131 @recipients = @{$param{recipients}} if defined $param{recipients} and
132 ref($param{recipients}) eq 'ARRAY';
133 # If there are no recipients, use -t to parse the message
134 if (@recipients == 0) {
135 $param{parse_for_recipients} = 1 unless exists $param{parse_for_recipients};
137 # Encode headers if necessary
138 $param{encode_headers} = 1 if not exists $param{encode_headers};
139 if ($param{encode_headers}) {
140 $param{message} = encode_headers($param{message});
143 # First, try to send the message as is.
145 _send_message($param{message},
147 $param{parse_for_recipients}?q(-t):(),
151 # If there's only one recipient, there's nothing more we can do,
153 warn $@ and return 0 if $@ and @recipients == 0;
154 # If that fails, try to send the message to each of the
155 # recipients separately. We also send the -t option separately in
156 # case one of the @recipients is ok, but the addresses in the
157 # mail message itself are malformed.
159 for my $recipient ($param{parse_for_recipients}?q(-t):(),@recipients) {
161 _send_message($param{message},@sendmail_arguments,$recipient);
163 push @errors, "Sending to $recipient failed with $@" if $@;
165 # If it still fails, complain bitterly but don't die.
166 warn join(qq(\n),@errors) and return 0 if @errors;
170 =head2 encode_headers
172 $message = encode_heeaders($message);
174 RFC 1522 encodes the headers of a message
181 my ($header,$body) = split /\n\n/, $message, 2;
182 $header = encode_rfc1522($header);
183 return $header . qq(\n\n). $body;
187 =head1 PRIVATE FUNCTIONS
191 _send_message($message,@sendmail_args);
193 Private function that actually calls sendmail with @sendmail_args and
194 sends message $message.
196 dies with errors, so calls to this function in send_mail_message
197 should be wrapped in eval.
202 my ($message,@sendmail_args) = @_;
205 my $pid = open3($wfh,$rfh,$rfh,$SENDMAIL,@sendmail_args)
206 or die "Unable to fork off $SENDMAIL: $!";
207 local $SIG{PIPE} = 'IGNORE';
209 print {$wfh} $message or die "Unable to write to $SENDMAIL: $!";
210 close $wfh or die "$SENDMAIL exited with $?";
215 waitpid($pid,WNOHANG);
216 # This shouldn't block because the pipe closing is the only
217 # way this should be triggered.
218 my $message = <$rfh>;
221 # Wait for sendmail to exit for at most 30 seconds.
223 while (waitpid($pid, WNOHANG) == 0 or $loop++ >= 600){
224 # sleep for a 20th of a second
228 warn "$SENDMAIL didn't exit within 30 seconds";