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);
52 use Debbugs::Packages;
55 ($VERSION) = q$Revision: 1.1 $ =~ /^Revision:\s+([^\s+])/;
56 $DEBUG = 0 unless defined $DEBUG;
59 %EXPORT_TAGS = (addresses => [qw(get_addresses)],
60 misc => [qw(rfc822_date)],
61 mail => [qw(send_mail_message encode_headers default_headers)],
64 Exporter::export_ok_tags(keys %EXPORT_TAGS);
65 $EXPORT_TAGS{all} = [@EXPORT_OK];
68 # We set this here so it can be overridden for testing purposes
69 our $SENDMAIL = $config{sendmail};
73 my @addresses = get_addresses('don@debian.org blars@debian.org
74 kamion@debian.org ajt@debian.org');
76 Given a string containing some e-mail addresses, parses the string
77 using Mail::Address->parse and returns a list of the addresses.
82 return map { $_->address() } map { Mail::Address->parse($_) } @_;
86 =head2 default_headers
88 my @head = default_headers(queue_file => 'foo',
90 msgid => $header{'message-id'},
94 create_mime_message(\@headers,
98 This function is generally called to generate the headers for
99 create_mime_message (and anything else that needs a set of default
102 In list context, returns an array of headers. In scalar context,
103 returns headers for shoving in a mail message after encoding using
110 =item queue_file -- the queue file which will generate this set of
111 headers (refered to as $nn in lots of the code)
113 =item data -- the data of the bug which this message involves; can be
114 undefined if there is no bug involved.
116 =item msgid -- the Message-ID: of the message which will generate this
119 =item msgtype -- the type of message that this is.
121 =item pr_msg -- the pr message field
123 =item headers -- a set of headers which will override the default
124 headers; these headers will be passed through (and may be reordered.)
125 If a particular header is undef, it overrides the default, but isn't
130 =head3 default headers
134 =item X-Loop -- set to the maintainer e-mail
136 =item From -- set to the maintainer e-mail
138 =item To -- set to Unknown recipients
140 =item Subject -- set to Unknown subject
142 =item Message-ID -- set appropriately (see code)
144 =item Precedence -- set to bulk
146 =item References -- set to the full set of message ids that are known
147 (from data and the msgid option)
149 =item In-Reply-To -- set to msg id or the msgid from data
151 =item X-Project-PR-Message -- set to pr_msg with the bug number appended
153 =item X-Project-PR-Package -- set to the package of the bug
155 =item X-Project-PR-Keywords -- set to the keywords of the bug
157 =item X-Project-PR-Source -- set to the source of the bug
163 sub default_headers {
164 my %param = validate_with(params => \@_,
165 spec => {queue_file => {type => SCALAR,
168 data => {type => HASHREF,
171 msgid => {type => SCALAR,
174 msgtype => {type => SCALAR,
178 pr_msg => {type => SCALAR,
181 headers => {type => ARRAYREF,
186 my @header_order = (qw(X-Loop From To subject),
187 qw(Message-ID In-Reply-To References));
189 @header_order{map {lc $_} @header_order} = 0..$#header_order;
192 my @temp = @{$param{headers}};
194 while (my ($header,$value) = splice @temp,0,2) {
195 if (exists $header_order{lc($header)}) {
196 push @{$ordered_headers[$header_order{lc($header)}]},
200 push @other_headers,($header,$value);
202 $set_headers{lc($header)} = 1;
205 # calculate our headers
206 my $bug_num = exists $param{data} ? $param{data}{bug_num} : 'x';
207 my $nn = $param{queue_file};
208 # handle the user giving the actual queue filename instead of nn
209 $nn =~ s/^[a-zA-Z]([a-zA-Z])/$1/;
212 if (exists $param{msgid} and defined $param{msgid}) {
213 push @msgids, $param{msgid}
215 elsif (exists $param{data} and defined $param{data}{msgid}) {
216 push @msgids, $param{data}{msgid}
219 $default_header{'X-Loop'} = $config{maintainer_email};
220 $default_header{From} = "$config{maintainer_email} ($config{project} $config{ubug} Tracking System)";
221 $default_header{To} = "Unknown recipients";
222 $default_header{Subject} = "Unknown subject";
223 $default_header{'Message-ID'} = "<handler.${bug_num}.${nn}.$param{msgtype}\@$config{email_domain}>";
225 $default_header{'In-Reply-To'} = $msgids[0];
226 $default_header{'References'} = join(' ',@msgids);
228 $default_header{Precedence} = 'bulk';
229 $default_header{"X-$config{project}-PR-Message"} = $param{pr_msg} . (exists $param{data} ? ' '.$param{data}{bug_num}:'');
230 $default_header{Date} = rfc822_date();
231 if (exists $param{data}) {
232 if (defined $param{data}{keywords}) {
233 $default_header{"X-$config{project}-PR-Keywords"} = $param{data}{keywords};
235 if (defined $param{data}{package}) {
236 $default_header{"X-$config{project}-PR-Package"} = $param{data}{package};
237 if ($param{data}{package} =~ /^src:(.+)$/) {
238 $default_header{"X-$config{project}-PR-Source"} = $1;
241 my $pkg_src = Debbugs::Packages::getpkgsrc();
242 $default_header{"X-$config{project}-PR-Source"} = $pkg_src->{$param{data}{package}};
246 for my $header (sort keys %default_header) {
247 next if $set_headers{lc($header)};
248 if (exists $header_order{lc($header)}) {
249 push @{$ordered_headers[$header_order{lc($header)}]},
250 ($header,$default_header{$header});
253 push @other_headers,($header,$header_order{lc($header)});
257 for my $hdr1 (@ordered_headers) {
258 next if not defined $hdr1;
260 while (my ($header,$value) = splice @temp,0,2) {
261 next if not defined $value;
262 push @headers,($header,$value);
265 push @headers,@other_headers;
271 while (my ($header,$value) = splice @headers,0,2) {
272 $headers .= "${header}: $value\n";
280 =head2 send_mail_message
282 send_mail_message(message => $message,
283 recipients => [@recipients],
284 envelope_from => 'don@debian.org',
290 =item message -- message to send out
292 =item recipients -- recipients to send the message to. If undefed or
293 an empty arrayref, will use '-t' to parse the message for recipients.
295 =item envelope_from -- envelope_from for outgoing messages
297 =item encode_headers -- encode headers using RFC1522 (default)
299 =item parse_for_recipients -- use -t to parse the message for
300 recipients in addition to those specified. [Can be used to set Bcc
301 recipients, for example.]
305 Returns true on success, false on failures. All errors are indicated
310 sub send_mail_message{
311 my %param = validate_with(params => \@_,
312 spec => {sendmail_arguments => {type => ARRAYREF,
313 default => [qw(-odq -oem -oi)],
315 parse_for_recipients => {type => BOOLEAN,
318 encode_headers => {type => BOOLEAN,
321 message => {type => SCALAR,
323 envelope_from => {type => SCALAR,
326 recipients => {type => ARRAYREF|UNDEF,
331 my @sendmail_arguments = qw(-odq -oem -oi);
332 push @sendmail_arguments, '-f', $param{envelope_from} if exists $param{envelope_from};
335 @recipients = @{$param{recipients}} if defined $param{recipients} and
336 ref($param{recipients}) eq 'ARRAY';
338 @recipients{@recipients} = (1) x @recipients;
339 @recipients = keys %recipients;
340 # If there are no recipients, use -t to parse the message
341 if (@recipients == 0) {
342 $param{parse_for_recipients} = 1 unless exists $param{parse_for_recipients};
344 # Encode headers if necessary
345 $param{encode_headers} = 1 if not exists $param{encode_headers};
346 if ($param{encode_headers}) {
347 $param{message} = encode_headers($param{message});
350 # First, try to send the message as is.
352 _send_message($param{message},
354 $param{parse_for_recipients}?q(-t):(),
358 # If there's only one recipient, there's nothing more we can do,
360 warn $@ and return 0 if $@ and @recipients == 0;
361 # If that fails, try to send the message to each of the
362 # recipients separately. We also send the -t option separately in
363 # case one of the @recipients is ok, but the addresses in the
364 # mail message itself are malformed.
366 for my $recipient ($param{parse_for_recipients}?q(-t):(),@recipients) {
368 _send_message($param{message},@sendmail_arguments,$recipient);
370 push @errors, "Sending to $recipient failed with $@" if $@;
372 # If it still fails, complain bitterly but don't die.
373 warn join(qq(\n),@errors) and return 0 if @errors;
377 =head2 encode_headers
379 $message = encode_heeaders($message);
381 RFC 1522 encodes the headers of a message
388 my ($header,$body) = split /\n\n/, $message, 2;
389 $header = encode_rfc1522($header);
390 return $header . qq(\n\n). $body;
397 Return the current date in RFC822 format in the UTC timezone
402 return scalar strftime "%a, %d %h %Y %T +0000", gmtime;
405 =head1 PRIVATE FUNCTIONS
409 _send_message($message,@sendmail_args);
411 Private function that actually calls sendmail with @sendmail_args and
412 sends message $message.
414 dies with errors, so calls to this function in send_mail_message
415 should be wrapped in eval.
420 my ($message,@sendmail_args) = @_;
423 my $pid = open3($wfh,$rfh,$rfh,$SENDMAIL,@sendmail_args)
424 or die "Unable to fork off $SENDMAIL: $!";
425 local $SIG{PIPE} = 'IGNORE';
427 print {$wfh} $message or die "Unable to write to $SENDMAIL: $!";
428 close $wfh or die "$SENDMAIL exited with $?";
433 waitpid($pid,WNOHANG);
434 # This shouldn't block because the pipe closing is the only
435 # way this should be triggered.
436 my $message = <$rfh>;
439 # Wait for sendmail to exit for at most 30 seconds.
441 while (waitpid($pid, WNOHANG) == 0 or $loop++ >= 600){
442 # sleep for a 20th of a second
446 warn "$SENDMAIL didn't exit within 30 seconds";