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
108 =item queue_file -- the queue file which will generate this set of
109 headers (refered to as $nn in lots of the code)
111 =item data -- the data of the bug which this message involves; can be
112 undefined if there is no bug involved.
114 =item msgid -- the Message-ID: of the message which will generate this
117 =item msgtype -- the type of message that this is.
119 =item pr_msg -- the pr message field
121 =item headers -- a set of headers which will override the default
122 headers; these headers will be passed through (and may be reordered.)
123 If a particular header is undef, it overrides the default, but isn't
128 =head3 default headers
132 =item X-Loop -- set to the maintainer e-mail
134 =item From -- set to the maintainer e-mail
136 =item To -- set to Unknown recipients
138 =item Subject -- set to Unknown subject
140 =item Message-ID -- set appropriately (see code)
142 =item Precedence -- set to bulk
144 =item References -- set to the full set of message ids that are known
145 (from data and the msgid option)
147 =item In-Reply-To -- set to msg id or the msgid from data
149 =item X-Project-PR-Message -- set to pr_msg with the bug number appended
151 =item X-Project-PR-Package -- set to the package of the bug
153 =item X-Project-PR-Keywords -- set to the keywords of the bug
155 =item X-Project-PR-Source -- set to the source of the bug
161 sub default_headers {
162 my %param = validate_with(params => \@_,
163 spec => {queue_file => {type => SCALAR,
166 data => {type => HASHREF,
169 msgid => {type => SCALAR,
172 msgtype => {type => SCALAR,
176 pr_msg => {type => SCALAR,
179 headers => {type => ARRAYREF,
184 my @header_order = (qw(X-Loop From To subject),
185 qw(Message-ID In-Reply-To References));
187 @header_order{map {lc $_} @header_order} = 0..$#header_order;
190 my @temp = @{$param{headers}};
192 while (my ($header,$value) = splice @temp,0,2) {
193 if (exists $header_order{lc($header)}) {
194 push @{$ordered_headers[$header_order{lc($header)}]},
198 push @other_headers,($header,$value);
200 $set_headers{lc($header)} = 1;
203 # calculate our headers
204 my $bug_num = exists $param{data} ? $param{data}{bug_num} : 'x';
205 my $nn = $param{queue_file};
206 # handle the user giving the actual queue filename instead of nn
207 $nn =~ s/^[a-zA-Z]([a-zA-Z])/$1/;
210 if (exists $param{msgid} and defined $param{msgid}) {
211 push @msgids, $param{msgid}
213 elsif (exists $param{data} and defined $param{data}{msgid}) {
214 push @msgids, $param{data}{msgid}
217 $default_header{'X-Loop'} = $config{maintainer_email};
218 $default_header{From} = "$config{maintainer_email} ($config{project} $config{ubug} Tracking System)";
219 $default_header{To} = "Unknown recipients";
220 $default_header{Subject} = "Unknown subject";
221 $default_header{'Message-ID'} = "<handler.${bug_num}.${nn}.$param{msgtype}\@$config{email_domain}>";
223 $default_header{'In-Reply-To'} = $msgids[0];
224 $default_header{'References'} = join(' ',@msgids);
226 $default_header{Precedence} = 'bulk';
227 $default_header{"X-$config{project}-PR-Message"} = $param{pr_msg} . (exists $param{data} ? ' '.$param{data}{bug_num}:'');
228 $default_header{Date} = rfc822_date();
229 if (exists $param{data}) {
230 if (defined $param{data}{keywords}) {
231 $default_header{"X-$config{project}-PR-Keywords"} = $param{data}{keywords};
233 if (defined $param{data}{package}) {
234 $default_header{"X-$config{project}-PR-Package"} = $param{data}{package};
235 if ($param{data}{package} =~ /^src:(.+)$/) {
236 $default_header{"X-$config{project}-PR-Source"} = $1;
239 my $pkg_src = getpkgsrc();
240 $default_header{"X-$config{project}-PR-Source"} = $pkg_src->{$param{data}{package}};
244 for my $header (sort keys %default_header) {
245 next if $set_headers{lc($header)};
246 if (exists $header_order{lc($header)}) {
247 push @{$ordered_headers[$header_order{lc($header)}]},
248 ($header,$default_header{$header});
251 push @other_headers,($header,$header_order{lc($header)});
255 for my $hdr1 (@ordered_headers) {
256 next if not defined $hdr1;
258 while (my ($header,$value) = splice @temp,0,2) {
259 next if not defined $value;
260 push @headers,($header,$value);
263 push @headers,@other_headers;
269 while (my ($header,$value) = splice @headers,0,2) {
270 $headers .= "${header}: $value\n";
278 =head2 send_mail_message
280 send_mail_message(message => $message,
281 recipients => [@recipients],
282 envelope_from => 'don@debian.org',
288 =item message -- message to send out
290 =item recipients -- recipients to send the message to. If undefed or
291 an empty arrayref, will use '-t' to parse the message for recipients.
293 =item envelope_from -- envelope_from for outgoing messages
295 =item encode_headers -- encode headers using RFC1522 (default)
297 =item parse_for_recipients -- use -t to parse the message for
298 recipients in addition to those specified. [Can be used to set Bcc
299 recipients, for example.]
303 Returns true on success, false on failures. All errors are indicated
308 sub send_mail_message{
309 my %param = validate_with(params => \@_,
310 spec => {sendmail_arguments => {type => ARRAYREF,
311 default => [qw(-odq -oem -oi)],
313 parse_for_recipients => {type => BOOLEAN,
316 encode_headers => {type => BOOLEAN,
319 message => {type => SCALAR,
321 envelope_from => {type => SCALAR,
324 recipients => {type => ARRAYREF|UNDEF,
329 my @sendmail_arguments = qw(-odq -oem -oi);
330 push @sendmail_arguments, '-f', $param{envelope_from} if exists $param{envelope_from};
333 @recipients = @{$param{recipients}} if defined $param{recipients} and
334 ref($param{recipients}) eq 'ARRAY';
336 @recipients{@recipients} = (1) x @recipients;
337 @recipients = keys %recipients;
338 # If there are no recipients, use -t to parse the message
339 if (@recipients == 0) {
340 $param{parse_for_recipients} = 1 unless exists $param{parse_for_recipients};
342 # Encode headers if necessary
343 $param{encode_headers} = 1 if not exists $param{encode_headers};
344 if ($param{encode_headers}) {
345 $param{message} = encode_headers($param{message});
348 # First, try to send the message as is.
350 _send_message($param{message},
352 $param{parse_for_recipients}?q(-t):(),
356 # If there's only one recipient, there's nothing more we can do,
358 warn $@ and return 0 if $@ and @recipients == 0;
359 # If that fails, try to send the message to each of the
360 # recipients separately. We also send the -t option separately in
361 # case one of the @recipients is ok, but the addresses in the
362 # mail message itself are malformed.
364 for my $recipient ($param{parse_for_recipients}?q(-t):(),@recipients) {
366 _send_message($param{message},@sendmail_arguments,$recipient);
368 push @errors, "Sending to $recipient failed with $@" if $@;
370 # If it still fails, complain bitterly but don't die.
371 warn join(qq(\n),@errors) and return 0 if @errors;
375 =head2 encode_headers
377 $message = encode_heeaders($message);
379 RFC 1522 encodes the headers of a message
386 my ($header,$body) = split /\n\n/, $message, 2;
387 $header = encode_rfc1522($header);
388 return $header . qq(\n\n). $body;
395 Return the current date in RFC822 format in the UTC timezone
400 return scalar strftime "%a, %d %h %Y %T +0000", gmtime;
403 =head1 PRIVATE FUNCTIONS
407 _send_message($message,@sendmail_args);
409 Private function that actually calls sendmail with @sendmail_args and
410 sends message $message.
412 dies with errors, so calls to this function in send_mail_message
413 should be wrapped in eval.
418 my ($message,@sendmail_args) = @_;
421 my $pid = open3($wfh,$rfh,$rfh,$SENDMAIL,@sendmail_args)
422 or die "Unable to fork off $SENDMAIL: $!";
423 local $SIG{PIPE} = 'IGNORE';
425 print {$wfh} $message or die "Unable to write to $SENDMAIL: $!";
426 close $wfh or die "$SENDMAIL exited with $?";
431 waitpid($pid,WNOHANG);
432 # This shouldn't block because the pipe closing is the only
433 # way this should be triggered.
434 my $message = <$rfh>;
437 # Wait for sendmail to exit for at most 30 seconds.
439 while (waitpid($pid, WNOHANG) == 0 or $loop++ >= 600){
440 # sleep for a 20th of a second
444 warn "$SENDMAIL didn't exit within 30 seconds";