]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/Mail.pm
* Switch to using Params::Validate in Debbugs::Mail
[debbugs.git] / Debbugs / Mail.pm
1 # $Id: Mail.pm,v 1.1 2005/08/17 21:46:16 don Exp $
2
3 package Debbugs::Mail;
4
5 =head1 NAME
6
7 Debbugs::Mail -- Outgoing Mail Handling
8
9 =head1 SYNOPSIS
10
11 use Debbugs::Mail qw(send_mail_message get_addresses);
12
13 my @addresses = get_addresses('blah blah blah foo@bar.com')
14 send_mail_message(message => <<END, recipients=>[@addresses]);
15 To: $addresses[0]
16 Subject: Testing
17
18 Testing 1 2 3
19 END
20
21 =head1 EXPORT TAGS
22
23 =over
24
25 =item :all -- all functions that can be exported
26
27 =back
28
29 =head1 FUNCTIONS
30
31
32 =cut
33
34 use warnings;
35 use strict;
36 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
37 use base qw(Exporter);
38
39 use IPC::Open3;
40 use POSIX ":sys_wait_h";
41 use Time::HiRes qw(usleep);
42 use Mail::Address ();
43 use Debbugs::MIME qw(encode_rfc1522);
44 use Debbugs::Config qw(:config);
45 use Params::Validate qw(:types validate_with);
46
47 BEGIN{
48      ($VERSION) = q$Revision: 1.1 $ =~ /^Revision:\s+([^\s+])/;
49      $DEBUG = 0 unless defined $DEBUG;
50
51      @EXPORT = ();
52      @EXPORT_OK = qw(send_mail_message get_addresses encode_headers);
53      $EXPORT_TAGS{all} = [@EXPORT_OK];
54
55 }
56
57 # We set this here so it can be overridden for testing purposes
58 our $SENDMAIL = $config{sendmail};
59
60 =head2 get_addresses
61
62      my @addresses = get_addresses('don@debian.org blars@debian.org
63                                     kamion@debian.org ajt@debian.org');
64
65 Given a string containing some e-mail addresses, parses the string
66 using Mail::Address->parse and returns a list of the addresses.
67
68 =cut
69
70 sub get_addresses {
71      return map { $_->address() } map { Mail::Address->parse($_) } @_;
72 }
73
74
75
76 =head2 send_mail_message
77
78      send_mail_message(message    => $message,
79                        recipients => [@recipients],
80                        envelope_from => 'don@debian.org',
81                       );
82
83
84 =over
85
86 =item message -- message to send out
87
88 =item recipients -- recipients to send the message to. If undefed or
89 an empty arrayref, will use '-t' to parse the message for recipients.
90
91 =item envelope_from -- envelope_from for outgoing messages
92
93 =item encode_headers -- encode headers using RFC1522 (default)
94
95 =item parse_for_recipients -- use -t to parse the message for
96 recipients in addition to those specified. [Can be used to set Bcc
97 recipients, for example.]
98
99 =back
100
101 Returns true on success, false on failures. All errors are indicated
102 using warn.
103
104 =cut
105
106 sub send_mail_message{
107      my %param = validate_with(param => \@_,
108                                spec  => {sendmail_arguments => {type => ARRAYREF,
109                                                                 default => [qw(-odq -oem -oi)],
110                                                                },
111                                          parse_for_recipients => {type => BOOLEAN,
112                                                                   default => 0,
113                                                                  },
114                                          encode_headers       => {type => BOOLEAN,
115                                                                   default => 1,
116                                                                  },
117                                          message              => {type => SCALAR,
118                                                                  },
119                                          envelope_from        => {type => SCALAR,
120                                                                   optional => 1,
121                                                                  },
122                                          recipients           => {type => ARRAYREF|UNDEF,
123                                                                   optional => 1,
124                                                                  },
125                                         },
126                               );
127      my @sendmail_arguments = qw(-odq -oem -oi);
128      push @sendmail_arguments, '-f', $param{envelope_from} if exists $param{envelope_from};
129
130      my @recipients;
131      @recipients = @{$param{recipients}} if defined $param{recipients} and
132           ref($param{recipients}) eq 'ARRAY';
133      # If there are no recipients, use -t to parse the message
134      if (@recipients == 0) {
135           $param{parse_for_recipients} = 1 unless exists $param{parse_for_recipients};
136      }
137      # Encode headers if necessary
138      $param{encode_headers} = 1 if not exists $param{encode_headers};
139      if ($param{encode_headers}) {
140           $param{message} = encode_headers($param{message});
141      }
142
143      # First, try to send the message as is.
144      eval {
145           _send_message($param{message},
146                         @sendmail_arguments,
147                         $param{parse_for_recipients}?q(-t):(),
148                         @recipients);
149      };
150      return 1 unless $@;
151      # If there's only one recipient, there's nothing more we can do,
152      # so bail out.
153      warn $@ and return 0 if $@ and @recipients == 0;
154      # If that fails, try to send the message to each of the
155      # recipients separately. We also send the -t option separately in
156      # case one of the @recipients is ok, but the addresses in the
157      # mail message itself are malformed.
158      my @errors;
159      for my $recipient ($param{parse_for_recipients}?q(-t):(),@recipients) {
160           eval {
161                _send_message($param{message},@sendmail_arguments,$recipient);
162           };
163           push @errors, "Sending to $recipient failed with $@" if $@;
164      }
165      # If it still fails, complain bitterly but don't die.
166      warn join(qq(\n),@errors) and return 0 if @errors;
167      return 1;
168 }
169
170 =head2 encode_headers
171
172      $message = encode_heeaders($message);
173
174 RFC 1522 encodes the headers of a message
175
176 =cut
177
178 sub encode_headers{
179      my ($message) = @_;
180
181      my ($header,$body) = split /\n\n/, $message, 2;
182      $header = encode_rfc1522($header);
183      return $header . qq(\n\n). $body;
184 }
185
186
187 =head1 PRIVATE FUNCTIONS
188
189 =head2 _send_message
190
191      _send_message($message,@sendmail_args);
192
193 Private function that actually calls sendmail with @sendmail_args and
194 sends message $message.
195
196 dies with errors, so calls to this function in send_mail_message
197 should be wrapped in eval.
198
199 =cut
200
201 sub _send_message{
202      my ($message,@sendmail_args) = @_;
203
204      my ($wfh,$rfh);
205      my $pid = open3($wfh,$rfh,$rfh,$SENDMAIL,@sendmail_args)
206           or die "Unable to fork off $SENDMAIL: $!";
207      local $SIG{PIPE} = 'IGNORE';
208      eval {
209           print {$wfh} $message or die "Unable to write to $SENDMAIL: $!";
210           close $wfh or die "$SENDMAIL exited with $?";
211      };
212      if ($@) {
213           local $\;
214           # Reap the zombie
215           waitpid($pid,WNOHANG);
216           # This shouldn't block because the pipe closing is the only
217           # way this should be triggered.
218           my $message = <$rfh>;
219           die "$@$message";
220      }
221      # Wait for sendmail to exit for at most 30 seconds.
222      my $loop = 0;
223      while (waitpid($pid, WNOHANG) == 0 or $loop++ >= 600){
224           # sleep for a 20th of a second
225           usleep(50_000);
226      }
227      if ($loop >= 600) {
228           warn "$SENDMAIL didn't exit within 30 seconds";
229      }
230 }
231
232
233 1;
234
235
236 __END__
237
238
239
240
241
242