1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later
3 # version at your option.
4 # See the file README and COPYING for more information.
6 # Copyright 2004-7 by Don Armstrong <don@donarmstrong.com>.
12 Debbugs::Mail -- Outgoing Mail Handling
16 use Debbugs::Mail qw(send_mail_message get_addresses);
18 my @addresses = get_addresses('blah blah blah foo@bar.com')
19 send_mail_message(message => <<END, recipients=>[@addresses]);
30 =item :all -- all functions that can be exported
41 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
42 use base qw(Exporter);
45 use POSIX ":sys_wait_h";
46 use Time::HiRes qw(usleep);
48 use Debbugs::MIME qw(encode_rfc1522);
49 use Debbugs::Config qw(:config);
50 use Params::Validate qw(:types validate_with);
53 ($VERSION) = q$Revision: 1.1 $ =~ /^Revision:\s+([^\s+])/;
54 $DEBUG = 0 unless defined $DEBUG;
57 @EXPORT_OK = qw(send_mail_message get_addresses encode_headers);
58 $EXPORT_TAGS{all} = [@EXPORT_OK];
62 # We set this here so it can be overridden for testing purposes
63 our $SENDMAIL = $config{sendmail};
67 my @addresses = get_addresses('don@debian.org blars@debian.org
68 kamion@debian.org ajt@debian.org');
70 Given a string containing some e-mail addresses, parses the string
71 using Mail::Address->parse and returns a list of the addresses.
76 return map { $_->address() } map { Mail::Address->parse($_) } @_;
81 =head2 send_mail_message
83 send_mail_message(message => $message,
84 recipients => [@recipients],
85 envelope_from => 'don@debian.org',
91 =item message -- message to send out
93 =item recipients -- recipients to send the message to. If undefed or
94 an empty arrayref, will use '-t' to parse the message for recipients.
96 =item envelope_from -- envelope_from for outgoing messages
98 =item encode_headers -- encode headers using RFC1522 (default)
100 =item parse_for_recipients -- use -t to parse the message for
101 recipients in addition to those specified. [Can be used to set Bcc
102 recipients, for example.]
106 Returns true on success, false on failures. All errors are indicated
111 sub send_mail_message{
112 my %param = validate_with(params => \@_,
113 spec => {sendmail_arguments => {type => ARRAYREF,
114 default => [qw(-odq -oem -oi)],
116 parse_for_recipients => {type => BOOLEAN,
119 encode_headers => {type => BOOLEAN,
122 message => {type => SCALAR,
124 envelope_from => {type => SCALAR,
127 recipients => {type => ARRAYREF|UNDEF,
132 my @sendmail_arguments = qw(-odq -oem -oi);
133 push @sendmail_arguments, '-f', $param{envelope_from} if exists $param{envelope_from};
136 @recipients = @{$param{recipients}} if defined $param{recipients} and
137 ref($param{recipients}) eq 'ARRAY';
139 @recipients{@recipients} = (1) x @recipients;
140 @recipients = keys %recipients;
141 # If there are no recipients, use -t to parse the message
142 if (@recipients == 0) {
143 $param{parse_for_recipients} = 1 unless exists $param{parse_for_recipients};
145 # Encode headers if necessary
146 $param{encode_headers} = 1 if not exists $param{encode_headers};
147 if ($param{encode_headers}) {
148 $param{message} = encode_headers($param{message});
151 # First, try to send the message as is.
153 _send_message($param{message},
155 $param{parse_for_recipients}?q(-t):(),
159 # If there's only one recipient, there's nothing more we can do,
161 warn $@ and return 0 if $@ and @recipients == 0;
162 # If that fails, try to send the message to each of the
163 # recipients separately. We also send the -t option separately in
164 # case one of the @recipients is ok, but the addresses in the
165 # mail message itself are malformed.
167 for my $recipient ($param{parse_for_recipients}?q(-t):(),@recipients) {
169 _send_message($param{message},@sendmail_arguments,$recipient);
171 push @errors, "Sending to $recipient failed with $@" if $@;
173 # If it still fails, complain bitterly but don't die.
174 warn join(qq(\n),@errors) and return 0 if @errors;
178 =head2 encode_headers
180 $message = encode_heeaders($message);
182 RFC 1522 encodes the headers of a message
189 my ($header,$body) = split /\n\n/, $message, 2;
190 $header = encode_rfc1522($header);
191 return $header . qq(\n\n). $body;
195 =head1 PRIVATE FUNCTIONS
199 _send_message($message,@sendmail_args);
201 Private function that actually calls sendmail with @sendmail_args and
202 sends message $message.
204 dies with errors, so calls to this function in send_mail_message
205 should be wrapped in eval.
210 my ($message,@sendmail_args) = @_;
213 my $pid = open3($wfh,$rfh,$rfh,$SENDMAIL,@sendmail_args)
214 or die "Unable to fork off $SENDMAIL: $!";
215 local $SIG{PIPE} = 'IGNORE';
217 print {$wfh} $message or die "Unable to write to $SENDMAIL: $!";
218 close $wfh or die "$SENDMAIL exited with $?";
223 waitpid($pid,WNOHANG);
224 # This shouldn't block because the pipe closing is the only
225 # way this should be triggered.
226 my $message = <$rfh>;
229 # Wait for sendmail to exit for at most 30 seconds.
231 while (waitpid($pid, WNOHANG) == 0 or $loop++ >= 600){
232 # sleep for a 20th of a second
236 warn "$SENDMAIL didn't exit within 30 seconds";