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 gettimeofday);
48 use Debbugs::MIME qw(encode_rfc1522);
49 use Debbugs::Config qw(:config);
50 use Params::Validate qw(:types validate_with);
51 use Encode qw(encode is_utf8);
53 use Debbugs::Packages;
56 ($VERSION) = q$Revision: 1.1 $ =~ /^Revision:\s+([^\s+])/;
57 $DEBUG = 0 unless defined $DEBUG;
60 %EXPORT_TAGS = (addresses => [qw(get_addresses)],
61 misc => [qw(rfc822_date)],
62 mail => [qw(send_mail_message encode_headers default_headers)],
65 Exporter::export_ok_tags(keys %EXPORT_TAGS);
66 $EXPORT_TAGS{all} = [@EXPORT_OK];
69 # We set this here so it can be overridden for testing purposes
70 our $SENDMAIL = $config{sendmail};
74 my @addresses = get_addresses('don@debian.org blars@debian.org
75 kamion@debian.org ajt@debian.org');
77 Given a string containing some e-mail addresses, parses the string
78 using Mail::Address->parse and returns a list of the addresses.
83 return map { $_->address() } map { Mail::Address->parse($_) } @_;
87 =head2 default_headers
89 my @head = default_headers(queue_file => 'foo',
91 msgid => $header{'message-id'},
95 create_mime_message(\@headers,
99 This function is generally called to generate the headers for
100 create_mime_message (and anything else that needs a set of default
103 In list context, returns an array of headers. In scalar context,
104 returns headers for shoving in a mail message after encoding using
111 =item queue_file -- the queue file which will generate this set of
112 headers (refered to as $nn in lots of the code)
114 =item data -- the data of the bug which this message involves; can be
115 undefined if there is no bug involved.
117 =item msgid -- the Message-ID: of the message which will generate this
120 =item msgtype -- the type of message that this is.
122 =item pr_msg -- the pr message field
124 =item headers -- a set of headers which will override the default
125 headers; these headers will be passed through (and may be reordered.)
126 If a particular header is undef, it overrides the default, but isn't
131 =head3 default headers
135 =item X-Loop -- set to the maintainer e-mail
137 =item From -- set to the maintainer e-mail
139 =item To -- set to Unknown recipients
141 =item Subject -- set to Unknown subject
143 =item Message-ID -- set appropriately (see code)
145 =item Precedence -- set to bulk
147 =item References -- set to the full set of message ids that are known
148 (from data and the msgid option)
150 =item In-Reply-To -- set to msg id or the msgid from data
152 =item X-Project-PR-Message -- set to pr_msg with the bug number appended
154 =item X-Project-PR-Package -- set to the package of the bug
156 =item X-Project-PR-Keywords -- set to the keywords of the bug
158 =item X-Project-PR-Source -- set to the source of the bug
164 sub default_headers {
165 my %param = validate_with(params => \@_,
166 spec => {queue_file => {type => SCALAR,
169 data => {type => HASHREF,
172 msgid => {type => SCALAR,
175 msgtype => {type => SCALAR,
179 pr_msg => {type => SCALAR,
182 headers => {type => ARRAYREF,
187 my @header_order = (qw(X-Loop From To subject),
188 qw(Message-ID In-Reply-To References));
190 @header_order{map {lc $_} @header_order} = 0..$#header_order;
193 my @temp = @{$param{headers}};
195 while (my ($header,$value) = splice @temp,0,2) {
196 if (exists $header_order{lc($header)}) {
197 push @{$ordered_headers[$header_order{lc($header)}]},
201 push @other_headers,($header,$value);
203 $set_headers{lc($header)} = 1;
206 # calculate our headers
207 my $bug_num = exists $param{data} ? $param{data}{bug_num} : 'x';
208 my $nn = exists $param{queue_file} ? $param{queue_file} : join('',gettimeofday());
209 # handle the user giving the actual queue filename instead of nn
210 $nn =~ s/^[a-zA-Z]([a-zA-Z])/$1/;
213 if (exists $param{msgid} and defined $param{msgid}) {
214 push @msgids, $param{msgid}
216 elsif (exists $param{data} and defined $param{data}{msgid}) {
217 push @msgids, $param{data}{msgid}
220 $default_header{'X-Loop'} = $config{maintainer_email};
221 $default_header{From} = "$config{maintainer_email} ($config{project} $config{ubug} Tracking System)";
222 $default_header{To} = "Unknown recipients";
223 $default_header{Subject} = "Unknown subject";
224 $default_header{'Message-ID'} = "<handler.${bug_num}.${nn}.$param{msgtype}\@$config{email_domain}>";
226 $default_header{'In-Reply-To'} = $msgids[0];
227 $default_header{'References'} = join(' ',@msgids);
229 $default_header{Precedence} = 'bulk';
230 $default_header{"X-$config{project}-PR-Message"} = $param{pr_msg} . (exists $param{data} ? ' '.$param{data}{bug_num}:'');
231 $default_header{Date} = rfc822_date();
232 if (exists $param{data}) {
233 if (defined $param{data}{keywords}) {
234 $default_header{"X-$config{project}-PR-Keywords"} = $param{data}{keywords};
236 if (defined $param{data}{package}) {
237 $default_header{"X-$config{project}-PR-Package"} = $param{data}{package};
238 if ($param{data}{package} =~ /^src:(.+)$/) {
239 $default_header{"X-$config{project}-PR-Source"} = $1;
242 my $pkg_src = Debbugs::Packages::getpkgsrc();
243 $default_header{"X-$config{project}-PR-Source"} = $pkg_src->{$param{data}{package}};
247 for my $header (sort keys %default_header) {
248 next if $set_headers{lc($header)};
249 if (exists $header_order{lc($header)}) {
250 push @{$ordered_headers[$header_order{lc($header)}]},
251 ($header,$default_header{$header});
254 push @other_headers,($header,$default_header{$header});
258 for my $hdr1 (@ordered_headers) {
259 next if not defined $hdr1;
261 while (my ($header,$value) = splice @temp,0,2) {
262 next if not defined $value;
263 push @headers,($header,$value);
266 push @headers,@other_headers;
272 while (my ($header,$value) = splice @headers,0,2) {
273 $headers .= "${header}: $value\n";
281 =head2 send_mail_message
283 send_mail_message(message => $message,
284 recipients => [@recipients],
285 envelope_from => 'don@debian.org',
291 =item message -- message to send out
293 =item recipients -- recipients to send the message to. If undefed or
294 an empty arrayref, will use '-t' to parse the message for recipients.
296 =item envelope_from -- envelope_from for outgoing messages
298 =item encode_headers -- encode headers using RFC1522 (default)
300 =item parse_for_recipients -- use -t to parse the message for
301 recipients in addition to those specified. [Can be used to set Bcc
302 recipients, for example.]
306 Returns true on success, false on failures. All errors are indicated
311 sub send_mail_message{
312 my %param = validate_with(params => \@_,
313 spec => {sendmail_arguments => {type => ARRAYREF,
314 default => $config{sendmail_arguments},
316 parse_for_recipients => {type => BOOLEAN,
319 encode_headers => {type => BOOLEAN,
322 message => {type => SCALAR,
324 envelope_from => {type => SCALAR,
327 recipients => {type => ARRAYREF|UNDEF,
332 my @sendmail_arguments = @{$param{sendmail_arguments}};
333 push @sendmail_arguments, '-f', $param{envelope_from} if exists $param{envelope_from};
336 @recipients = @{$param{recipients}} if defined $param{recipients} and
337 ref($param{recipients}) eq 'ARRAY';
339 @recipients{@recipients} = (1) x @recipients;
340 @recipients = keys %recipients;
341 # If there are no recipients, use -t to parse the message
342 if (@recipients == 0) {
343 $param{parse_for_recipients} = 1 unless exists $param{parse_for_recipients};
345 # Encode headers if necessary
346 $param{encode_headers} = 1 if not exists $param{encode_headers};
347 if ($param{encode_headers}) {
348 $param{message} = encode_headers($param{message});
351 if (is_utf8($param{message})) {
352 $param{message} = encode('utf8',$param{message});
356 # First, try to send the message as is.
358 _send_message($param{message},
360 $param{parse_for_recipients}?q(-t):(),
364 # If there's only one recipient, there's nothing more we can do,
366 warn $@ and return 0 if $@ and @recipients == 0;
367 # If that fails, try to send the message to each of the
368 # recipients separately. We also send the -t option separately in
369 # case one of the @recipients is ok, but the addresses in the
370 # mail message itself are malformed.
372 for my $recipient ($param{parse_for_recipients}?q(-t):(),@recipients) {
374 _send_message($param{message},@sendmail_arguments,$recipient);
376 push @errors, "Sending to $recipient failed with $@" if $@;
378 # If it still fails, complain bitterly but don't die.
379 warn join(qq(\n),@errors) and return 0 if @errors;
383 =head2 encode_headers
385 $message = encode_heeaders($message);
387 RFC 1522 encodes the headers of a message
394 my ($header,$body) = split /\n\n/, $message, 2;
395 $header = encode_rfc1522($header);
396 return $header . qq(\n\n). $body;
403 Return the current date in RFC822 format in the UTC timezone
408 return scalar strftime "%a, %d %h %Y %T +0000", gmtime;
411 =head1 PRIVATE FUNCTIONS
415 _send_message($message,@sendmail_args);
417 Private function that actually calls sendmail with @sendmail_args and
418 sends message $message.
420 dies with errors, so calls to this function in send_mail_message
421 should be wrapped in eval.
426 my ($message,@sendmail_args) = @_;
429 my $pid = open3($wfh,$rfh,$rfh,$SENDMAIL,@sendmail_args)
430 or die "Unable to fork off $SENDMAIL: $!";
431 local $SIG{PIPE} = 'IGNORE';
433 print {$wfh} $message or die "Unable to write to $SENDMAIL: $!";
434 close $wfh or die "$SENDMAIL exited with $?";
439 waitpid($pid,WNOHANG);
440 # This shouldn't block because the pipe closing is the only
441 # way this should be triggered.
442 my $message = <$rfh>;
445 # Wait for sendmail to exit for at most 30 seconds.
447 while (waitpid($pid, WNOHANG) == 0 or $loop++ >= 600){
448 # sleep for a 20th of a second
452 warn "$SENDMAIL didn't exit within 30 seconds";