1 # $Id: Mail.pm,v 1.1 2005/08/17 21:46:16 don Exp $
7 Debbugs::Mail -- Outgoing Mail Handling
11 use Debbugs::Mail qw(send_mail_message get_addresses);
13 my @addresses = get_addresses('blah blah blah foo@bar.com')
14 send_mail_message(message => <<END, recipients=>[@addresses]);
25 =item :all -- all functions that can be exported
36 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
37 use base qw(Exporter);
40 use POSIX ":sys_wait_h";
41 use Time::HiRes qw(usleep);
43 use Debbugs::MIME qw(encode_rfc1522);
46 ($VERSION) = q$Revision: 1.1 $ =~ /^Revision:\s+([^\s+])/;
47 $DEBUG = 0 unless defined $DEBUG;
50 @EXPORT_OK = qw(send_mail_message get_addresses encode_headers);
51 $EXPORT_TAGS{all} = [@EXPORT_OK];
57 my @addresses = get_addresses('don@debian.org blars@debian.org
58 kamion@debian.org ajt@debian.org');
60 Given a string containing some e-mail addresses, parses the string
61 using Mail::Address->parse and returns a list of the addresses.
66 return map { $_->address() } map { Mail::Address->parse($_) } @_;
71 =head2 send_mail_message
73 send_mail_message(message => $message,
74 recipients => [@recipients],
75 envelope_from => 'don@debian.org',
81 =item message -- message to send out
83 =item recipients -- recipients to send the message to. If undefed or
84 an empty arrayref, will use '-t' to parse the message for recipients.
86 =item envelope_from -- envelope_from for outgoing messages
88 =item encode_headers -- encode headers using RFC1522 (default)
90 =item parse_for_recipients -- use -t to parse the message for
91 recipients in addition to those specified. [Can be used to set Bcc
92 recipients, for example.]
96 Returns true on success, false on failures. All errors are indicated
101 sub send_mail_message{
102 die "send_mail_message requires an even number of arguments" if @_ % 2;
103 # It would be better to use Param::Validate instead...
106 die "send_mail_message requires a message" if not defined $param{message};
108 my @sendmail_arguments = qw(-odq -oem -oi);
109 push @sendmail_arguments, '-f', $param{envelope_from} if exists $param{envelope_from};
112 @recipients = @{$param{recipients}} if defined $param{recipients} and
113 ref($param{recipients}) eq 'ARRAY';
114 # If there are no recipients, use -t to parse the message
115 if (@recipients == 0) {
116 $param{parse_for_recipients} = 1 unless exists $param{parse_for_recipients};
118 # Encode headers if necessary
119 $param{encode_headers} = 1 if not exists $param{encode_headers};
120 if ($param{encode_headers}) {
121 $param{message} = encode_headers($param{message});
124 # First, try to send the message as is.
126 _send_message($param{message},
128 $param{parse_for_recipients}?q(-t):(),
132 # If there's only one recipient, there's nothing more we can do,
134 warn $@ and return 0 if $@ and @recipients == 0;
135 # If that fails, try to send the message to each of the
136 # recipients separately. We also send the -t option separately in
137 # case one of the @recipients is ok, but the addresses in the
138 # mail message itself are malformed.
140 for my $recipient ($param{parse_for_recipients}?q(-t):(),@recipients) {
142 _send_message($param{message},@sendmail_arguments,$recipient);
144 push @errors, "Sending to $recipient failed with $@" if $@;
146 # If it still fails, complain bitterly but don't die.
147 warn join(qq(\n),@errors) and return 0 if @errors;
151 =head2 encode_headers
153 $message = encode_heeaders($message);
155 RFC 1522 encodes the headers of a message
162 my ($header,$body) = split /\n\n/, $message, 2;
163 $header = encode_rfc1522($header);
164 return $header . qq(\n\n). $body;
168 =head1 PRIVATE FUNCTIONS
172 _send_message($message,@sendmail_args);
174 Private function that actually calls sendmail with @sendmail_args and
175 sends message $message.
177 dies with errors, so calls to this function in send_mail_message
178 should be wrapped in eval.
183 my ($message,@sendmail_args) = @_;
186 my $pid = open3($wfh,$rfh,$rfh,'/usr/lib/sendmail',@sendmail_args)
187 or die "Unable to fork off /usr/lib/sendmail: $!";
188 local $SIG{PIPE} = 'IGNORE';
190 print {$wfh} $message or die "Unable to write to /usr/lib/sendmail: $!";
191 close $wfh or die "/usr/lib/sendmail exited with $?";
196 waitpid($pid,WNOHANG);
197 # This shouldn't block because the pipe closing is the only
198 # way this should be triggered.
199 my $message = <$rfh>;
202 # Wait for sendmail to exit for at most 30 seconds.
204 while (waitpid($pid, WNOHANG) == 0 or $loop++ >= 600){
205 # sleep for a 20th of a second
209 warn "Sendmail didn't exit within 30 seconds";