File Coverage

blib/lib/Email/Send/Sendmail.pm
Criterion Covered Total %
statement 36 36 100.0
branch 14 18 77.7
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 58 64 90.6


line stmt bran cond sub pod time code
1             package Email::Send::Sendmail;
2 3     3   1238 use strict;
  3         4  
  3         96  
3              
4 3     3   12 use File::Spec ();
  3         3  
  3         106  
5             BEGIN {
6 3     3   4 local $Return::Value::NO_CLUCK = 1;
7 3         23 require Return::Value;
8 3         146 Return::Value->import;
9             }
10 3     3   912 use Symbol qw(gensym);
  3         1420  
  3         152  
11              
12 3     3   12 use vars qw[$SENDMAIL $VERSION];
  3         4  
  3         898  
13              
14             $VERSION = '2.201';
15              
16             sub is_available {
17 8     8 0 837 my $class = shift;
18              
19             # This is RIDICULOUS. Why do we say it's available if it isn't?
20             # -- rjbs, 2006-07-06
21 8 100       18 return success "No Sendmail found" unless $class->_find_sendmail;
22 5         21 return success '';
23             }
24              
25             sub _find_sendmail {
26 12     12   386 my $class = shift;
27 12 100       33 return $SENDMAIL if defined $SENDMAIL;
28              
29 6         9 my $sendmail;
30 6 100       110 for my $dir (
31             File::Spec->path,
32             ($ENV{PERL_EMAIL_SEND_SENDMAIL_NO_EXTRA_PATHS} ? () : (
33             File::Spec->catfile('', qw(usr sbin)),
34             File::Spec->catfile('', qw(usr lib)),
35             ))
36             ) {
37 11 100       118 if ( -x "$dir/sendmail" ) {
38 2         4 $sendmail = "$dir/sendmail";
39 2         3 last;
40             }
41             }
42              
43 6         29 return $sendmail;
44             }
45              
46             sub send {
47 3     3 0 6 my ($class, $message, @args) = @_;
48 3         8 my $mailer = $class->_find_sendmail;
49              
50 3 50       9 return failure "Couldn't find 'sendmail' executable in your PATH"
51             ." and \$".__PACKAGE__."::SENDMAIL is not set"
52             unless $mailer;
53              
54 3 100       54 return failure "Found $mailer but cannot execute it"
55             unless -x $mailer;
56            
57 2         32 local $SIG{'CHLD'} = 'DEFAULT';
58              
59 2         8 my $pipe = gensym;
60              
61 2 50       3355 open $pipe, "| $mailer -t -oi @args"
62             or return failure "Error executing $mailer: $!";
63 2 50       38 print $pipe $message->as_string
64             or return failure "Error printing via pipe to $mailer: $!";
65 2 50       4054 close $pipe
66             or return failure "error when closing pipe to $mailer: $!";
67 2         21 return success;
68             }
69              
70             1;
71              
72             __END__