SendVarious.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 276 CC 31 SCAR 50.6
CC
31
Cov
0%
sendmail
272.0
make_message
182.0
splitto
12.0
CRAP 157.8
1  
2 # Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com>
3  
4 package Mail::SendVarious;
5  
6 use strict;
7 use warnings;
8 use Net::SMTP;
9 use IO::Pipe;
10 use Carp;
11 require Exporter;
12  
13 our $VERSION = 0.4;
14 our @ISA = qw(Exporter);
15 our @EXPORT = qw(sendmail);
16 our @EXPORT_OK = qw($mail_error make_message @to_rejected @mail_hostlist @mail_command);
17  
18 our $mail_error;
19 our @to_rejected;
20  
21 our @mail_hostlist;
22 our @mail_command;
23  
24 our %net_smtp_options;
25  
26 @mail_hostlist = qw(127.0.0.1)
27 unless @mail_hostlist;
28 @mail_command = qw(/usr/sbin/sendmail -oeml -i)
29 unless @mail_command;
30  
31  
32 # EXAMPLE:
33 #
34 # sendmail(
35 # From => 'Account Signup Form',
36 # from => 'root@myhost',
37 # to => 'support@myhost',
38 # subject => "Account Signup: $d{owner} <$d{login}>",
39 # body => $x,
40 #);
41 #
42  
43 sub Daemon::Generic::Sendmail::T::TIEHASH { my $p = shift; return bless shift, $p; }
44 sub Daemon::Generic::Sendmail::T::FETCH { my $f = shift; return &$f(shift) }
45  
46 tie our %jointo, 'Daemon::Generic::Sendmail::T', sub {
47 my $to = shift;
48 return join(', ', @$to) if ref($to);
49 return $to;
50 };
51  
52 sub splitto
53 {
54 my $to = shift;
55 return @$to if ref $to;
56 return unless $to;
57 $to =~ s/".*?"//;
58 $to =~ s/[^<>,]*<(.*?)>[^<>,]/$1/g;
59 return split(',',$to);
60 };
61  
62 sub make_message
63 {
64 my (%options) = @_;
65  
66 my $from = $options{from} || $options{envelope_from};
67 $from = qq{"$options{From}" <$from>}
68 if $options{From};
69  
70 my $message = '';
71 if ($options{build_header} ||
72 ! ($options{header}
73 || $options{message}
74 || (defined($options{build_header}) && ! $options{build_header})))
75 {
76 $message .= "From: $from\n" if $from;
77 $message .= "To: $jointo{$options{to}}\n" if $options{to};
78 $message .= "Subject: $options{subject}\n" if $options{subject};
79 $message .= "Cc: $jointo{$options{cc}}\n" if $options{cc};
80 $message .= $options{xheader} if $options{xheader};
81 }
82  
83 if ($options{message}) {
84 $message .= $options{message} if $options{message};
85 } else {
86 $message .= $options{header} if $options{header};
87 $message .= "\n".$options{body} if $options{body};
88 }
89  
90 my @to = splitto($options{envelope_to});
91 @to = (splitto($options{to}), splitto($options{cc})) unless @to;
92  
93 push(@to, splitto($options{bcc}));
94  
95 return ($from, $message, @to);
96 }
97  
98 sub sendmail
99 {
100 my (%options) = @_;
101  
102 @to_rejected = ();
103 my @hostlist = $options{hostlist}
104 ? splitto($options{hostlist})
105 : @mail_hostlist;
106 croak "no 'from' set" unless $options{from};
107  
108 my ($from, $message, @to) = make_message(%options);
109  
110 my $debuglog = $options{debuglogger} || sub { print STDERR "@_\n" };
111 my $errorlog = $options{errorlogger} || sub { print STDERR "@_\n" };
112  
113 @hostlist = () unless @to;
114 my $smtpfrom = $options{envelope_from} || $options{from};
115  
116 HOST:
117 for my $host (@hostlist) {
118 my @rejects;
119 my $smtp;
120 unless ($smtp = Net::SMTP->new($host, %net_smtp_options)) {
121 error("Could not establish SMTP connection to $host: $!", $errorlog);
122 next;
123 }
124  
125 unless ($smtp->mail($smtpfrom)) {
126 error("MAIL FROM: $smtpfrom - rejected", $errorlog);
127 next;
128 }
129
130 for my $t (@to) {
131 unless ($smtp->to($t)) {
132 if ($options{no_rejects} || @to == 1) {
133 error("RCPT TO: $t - rejected", $errorlog);
134 next HOST;
135 } else {
136 push(@rejects, $t);
137 }
138 }
139 }
140 unless ($smtp->data()) {
141 error("SMTP DATA - failed", $errorlog);
142 next;
143 }
144  
145 unless ($smtp->datasend($message)) {
146 error("datasend() failed", $errorlog);
147 next;
148 }
149  
150 unless ($smtp->dataend()) {
151 error("dataend() failed", $errorlog);
152 next;
153 }
154  
155 if (@rejects) {
156 @to_rejected = @rejects;
157 $debuglog->("Mail from $smtpfrom to @rejects rejected, other mail accepted");
158 }
159  
160 $smtp->quit();
161  
162 $debuglog->("Mail from $smtpfrom to @to injected via $host");
163 $mail_error = '';
164 return 1;
165 }
166  
167 $debuglog->("Mail from $smtpfrom to @to, falling back to sendmail");
168 #
169 # sending via smtp failed, try the local sendmail instead
170 #
171  
172 my @sm;
173 push(@sm, "-f$options{from}") if $options{from};
174 push(@sm, "-F$options{From}") if $options{From};
175  
176 @to = ('-t') unless @to;
177  
178 my @command = $options{mail_command}
179 ? splitto($options{mail_commnd})
180 : @mail_command;
181  
182  
183 my $MAIL;
184 eval { $MAIL = open_to_child(@command, @sm, @to) } or do {
185 error("Could not fork/exec child @command: $@", $errorlog);
186 return 0;
187 };
188 (print $MAIL $message) or do {
189 error("Could not write message to sendmail process: $!", $errorlog);
190 return 0;
191 };
192 close($MAIL) or do {
193 error("Could not close handle to sendmail process: $!", $errorlog);
194 return 0;
195 };
196 $mail_error = '';
197 return 1;
198 }
199  
200 sub open_to_child
201 {
202 my ($command, @args) = @_;
203 my $pipe = IO::Pipe->new();
204 $pipe->writer($command, @args);
205 return $pipe;
206 }
207  
208 sub error
209 {
210 my ($error, $logger) = @_;
211 $mail_error = $error;
212 if ($logger) {
213 $logger->($error);
214 } else {
215 print STDERR "$error\n";
216 }
217 }
218  
219 1;