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 qw(:sys_wait_h strftime);
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_TAGS = (addresses => qw(get_addresses),
58 misc => qw(rfc822_date),
59 mail => qw(send_mail_message encode_headers default_headers),
62 Exporter::export_ok_tags(keys %EXPORT_TAGS);
63 $EXPORT_TAGS{all} = [@EXPORT_OK];
66 # We set this here so it can be overridden for testing purposes
67 our $SENDMAIL = $config{sendmail};
71 my @addresses = get_addresses('don@debian.org blars@debian.org
72 kamion@debian.org ajt@debian.org');
74 Given a string containing some e-mail addresses, parses the string
75 using Mail::Address->parse and returns a list of the addresses.
80 return map { $_->address() } map { Mail::Address->parse($_) } @_;
84 =head2 default_headers
86 my @head = default_headers(queue_file => 'foo',
88 msgid => $header{'message-id'},
92 create_mime_message(\@headers,
96 This function is generally called to generate the headers for
97 create_mime_message (and anything else that needs a set of default
100 In list context, returns an array of headers. In scalar context,
101 returns headers for shoving in a mail message after encoding using
106 =item queue_file -- the queue file which will generate this set of
107 headers (refered to as $nn in lots of the code)
109 =item data -- the data of the bug which this message involves; can be
110 undefined if there is no bug involved.
112 =item msgid -- the Message-ID: of the message which will generate this
115 =item msgtype -- the type of message that this is.
117 =item pr_msg -- the pr message field
119 =item headers -- a set of headers which will override the default
120 headers; these headers will be passed through (and may be reordered.)
121 If a particular header is undef, it overrides the default, but isn't
128 sub default_headers {
129 my %param = validate_with(params => \@_,
130 spec => {queue_file => {type => SCALAR,
133 data => {type => HASHREF,
136 msgid => {type => SCALAR,
139 msgtype => {type => SCALAR,
143 pr_msg => {type => SCALAR,
146 headers => {type => ARRAYREF,
151 my @header_order = (qw(X-Loop From To subject),
152 qw(Message-ID In-Reply-To References));
154 @header_order{map {lc $_} @header_order} = 0..$#header_order;
157 my @temp = @{$param{headers}};
159 while (my ($header,$value) = splice @temp,0,2) {
160 if (exists $header_order{lc($header)}) {
161 push @{$ordered_headers[$header_order{lc($header)}]},
165 push @other_headers,($header,$value);
167 $set_headers{lc($header)} = 1;
170 # calculate our headers
171 my $bug_num = exists $param{data} ? $param{data}{bug_num} : 'x';
172 my $nn = $param{queue_file};
173 # handle the user giving the actual queue filename instead of nn
174 $nn =~ s/^[a-zA-Z]([a-zA-Z])/$1/;
177 if (exists $param{msgid} and defined $param{msgid}) {
178 push @msgids, $param{msgid}
180 elsif (exists $param{data} and defined $param{data}{msgid}) {
181 push @msgids, $param{data}{msgid}
184 $default_header{'X-Loop'} = $config{maintainer_email};
185 $default_header{From} = "$config{maintainer_email} ($config{project} $config{ubug} Tracking System)";
186 $default_header{To} = "Unknown recipients";
187 $default_header{Subject} = "Unknown subject";
188 $default_header{'Message-ID'} = "<handler.${bug_num}.${nn}.$param{msgtype}\@$config{email_domain}>";
190 $default_header{'In-Reply-To'} = $msgids[0];
191 $default_header{'References'} = join(' ',@msgids);
193 $default_header{Precedence} = 'bulk';
194 $default_header{"X-$config{project}-PR-Message"} = $param{pr_msg} . (exists $param{data} ? ' '.$param{data}{bug_num}:'');
195 $default_header{Date} = rfc822_date();
196 if (exists $param{data}) {
197 if (defined $param{data}{keywords}) {
198 $default_header{"X-$config{project}-PR-Keywords"} = $param{data}{keywords};
200 if (defined $param{data}{package}) {
201 $default_header{"X-$config{project}-PR-Package"} = $param{data}{package};
202 if ($param{data}{package} =~ /^src:(.+)$/) {
203 $default_header{"X-$config{project}-PR-Source"} = $1;
206 my $pkg_src = getpkgsrc();
207 $default_header{"X-$config{project}-PR-Source"} = $pkg_src->{$param{data}{package}};
211 for my $header (sort keys %default_header) {
212 next if $set_headers{lc($header)};
213 if (exists $header_order{lc($header)}) {
214 push @{$ordered_headers[$header_order{lc($header)}]},
215 ($header,$default_header{$header});
218 push @other_headers,($header,$header_order{lc($header)});
222 for my $hdr1 (@ordered_headers) {
223 next if not defined $hdr1;
225 while (my ($header,$value) = splice @temp,0,2) {
226 next if not defined $value;
227 push @headers,($header,$value);
230 push @headers,@other_headers;
236 while (my ($header,$value) = splice @headers,0,2) {
237 $headers .= "${header}: $value\n";
245 =head2 send_mail_message
247 send_mail_message(message => $message,
248 recipients => [@recipients],
249 envelope_from => 'don@debian.org',
255 =item message -- message to send out
257 =item recipients -- recipients to send the message to. If undefed or
258 an empty arrayref, will use '-t' to parse the message for recipients.
260 =item envelope_from -- envelope_from for outgoing messages
262 =item encode_headers -- encode headers using RFC1522 (default)
264 =item parse_for_recipients -- use -t to parse the message for
265 recipients in addition to those specified. [Can be used to set Bcc
266 recipients, for example.]
270 Returns true on success, false on failures. All errors are indicated
275 sub send_mail_message{
276 my %param = validate_with(params => \@_,
277 spec => {sendmail_arguments => {type => ARRAYREF,
278 default => [qw(-odq -oem -oi)],
280 parse_for_recipients => {type => BOOLEAN,
283 encode_headers => {type => BOOLEAN,
286 message => {type => SCALAR,
288 envelope_from => {type => SCALAR,
291 recipients => {type => ARRAYREF|UNDEF,
296 my @sendmail_arguments = qw(-odq -oem -oi);
297 push @sendmail_arguments, '-f', $param{envelope_from} if exists $param{envelope_from};
300 @recipients = @{$param{recipients}} if defined $param{recipients} and
301 ref($param{recipients}) eq 'ARRAY';
303 @recipients{@recipients} = (1) x @recipients;
304 @recipients = keys %recipients;
305 # If there are no recipients, use -t to parse the message
306 if (@recipients == 0) {
307 $param{parse_for_recipients} = 1 unless exists $param{parse_for_recipients};
309 # Encode headers if necessary
310 $param{encode_headers} = 1 if not exists $param{encode_headers};
311 if ($param{encode_headers}) {
312 $param{message} = encode_headers($param{message});
315 # First, try to send the message as is.
317 _send_message($param{message},
319 $param{parse_for_recipients}?q(-t):(),
323 # If there's only one recipient, there's nothing more we can do,
325 warn $@ and return 0 if $@ and @recipients == 0;
326 # If that fails, try to send the message to each of the
327 # recipients separately. We also send the -t option separately in
328 # case one of the @recipients is ok, but the addresses in the
329 # mail message itself are malformed.
331 for my $recipient ($param{parse_for_recipients}?q(-t):(),@recipients) {
333 _send_message($param{message},@sendmail_arguments,$recipient);
335 push @errors, "Sending to $recipient failed with $@" if $@;
337 # If it still fails, complain bitterly but don't die.
338 warn join(qq(\n),@errors) and return 0 if @errors;
342 =head2 encode_headers
344 $message = encode_heeaders($message);
346 RFC 1522 encodes the headers of a message
353 my ($header,$body) = split /\n\n/, $message, 2;
354 $header = encode_rfc1522($header);
355 return $header . qq(\n\n). $body;
362 Return the current date in RFC822 format in the UTC timezone
367 return scalar strftime "%a, %d %h %Y %T +0000", gmtime;
370 =head1 PRIVATE FUNCTIONS
374 _send_message($message,@sendmail_args);
376 Private function that actually calls sendmail with @sendmail_args and
377 sends message $message.
379 dies with errors, so calls to this function in send_mail_message
380 should be wrapped in eval.
385 my ($message,@sendmail_args) = @_;
388 my $pid = open3($wfh,$rfh,$rfh,$SENDMAIL,@sendmail_args)
389 or die "Unable to fork off $SENDMAIL: $!";
390 local $SIG{PIPE} = 'IGNORE';
392 print {$wfh} $message or die "Unable to write to $SENDMAIL: $!";
393 close $wfh or die "$SENDMAIL exited with $?";
398 waitpid($pid,WNOHANG);
399 # This shouldn't block because the pipe closing is the only
400 # way this should be triggered.
401 my $message = <$rfh>;
404 # Wait for sendmail to exit for at most 30 seconds.
406 while (waitpid($pid, WNOHANG) == 0 or $loop++ >= 600){
407 # sleep for a 20th of a second
411 warn "$SENDMAIL didn't exit within 30 seconds";