Mail/Mailer.pm

filter: Statementstmt 50.4% 53 / 105 Branchbran 26.7% 15 / 56 Conditioncond 26.0% 6 / 23 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 47.3% 9 / 19 Podpod 27.2% 3 / 11 totaltotal 38.1% 87 / 228 CC 35 SCAR 27.0
CC
35
Cov
40%
open
42.0
_cleanup_hdrs
20.0
who_to
20.0
CRAP 14.9
« Mail/Field.pm ↑ Summary Mail/Cap.pm »
1 # Copyrights 1995-2024 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of the bundle MailTools. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md for Copyright.
7 # Licensed under the same terms as Perl itself.
8  
9 package Mail::Mailer;{
10 3 our $VERSION = '2.22';
11 }
12  
13 3 ▶use base 'IO::Handle';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use POSIX qw/_exit/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #--------------
22  
23  
24 sub is_exe($);
25  
26 0 ▶sub Version { our $VERSION }
Subroutine Version: not called CC=1 SCAR=6.9
Pod documented
27  
28 3 our @Mailers =
29 ( sendmail => '/usr/lib/sendmail;/usr/sbin/sendmail;/usr/ucblib/sendmail'
30 , smtp => undef
31 , smtps => undef
32 , qmail => '/usr/sbin/qmail-inject;/var/qmail/bin/qmail-inject'
33 , testfile => undef
34 );
35  
36 0 push @Mailers, map { split /\:/, $_, 2 }
37 split /$Config{path_sep}/, $ENV{PERL_MAILERS}
38 3 ▶ if $ENV{PERL_MAILERS};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PERL_MAILERS"} 0 3 3
39  
40 3 our %Mailers = @Mailers;
41 3 our $MailerType;
42 3 our $MailerBinary;
43  
44 # does this really need to be done? or should a default mailer be specified?
45  
46 $Mailers{sendmail} = 'sendmail'
47 3 ▶ if $^O eq 'os2' && ! is_exe $Mailers{sendmail};
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "os2" and not &is_exe($Mail::Mailer::Mailers{"sendmail"}) 0 3 3
Condition33%1/3
$^O eq "os2" and not &is_exe($Mail::Mailer::Mailers{"sendmail"})
!ll&&!rl&&r
300
MC/DC0%0/2
$^O eq "os2" and not &is_exe($Mail::Mailer::Mailers{"sendmail"})
$^O eq "os2" not &is_exe($Mail::Mailer::Mailers{"sendmail"})
Truth table33%1/3
A and not(B)
A= $^O eq "os2"B= not &is_exe($Mail::Mailer::Mailers{"sendmail"})
ABresult
0X0
100
111
48  
49 3 ▶if($^O =~ m/MacOS|VMS|MSWin|os2|NetWare/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /MacOS|VMS|MSWin|os2|NetWare/i) { } 0 3 3
50 0 { $MailerType = 'smtp';
51 0 $MailerBinary = $Mailers{$MailerType};
52 }
53 else
54 3 ▶{ for(my $i = 0 ; $i < @Mailers ; $i += 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @Mail::Mailer::Mailers) 15 3 18
55 15 { $MailerType = $Mailers[$i];
56 15 ▶ if(my $binary = is_exe $Mailers{$MailerType})
Branch: 1 branch
BranchTrueFalseTotal
if (my $binary = &is_exe($Mail::Mailer::Mailers{$Mail::Mailer::MailerType})) 0 15 15
57 0 { $MailerBinary = $binary;
58 0 last;
59 }
60 }
61 }
62  
63 sub import
64 1 ▶{ shift; # class
Subroutine import: called CC=4 SCAR=24.6
65 1 ▶ @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 0 1 1
66  
67 0 my $type = shift;
68 0 ▶ my $exe = shift || $Mailers{$type};
Condition0%0/3
shift() || $Mail::Mailer::Mailers{$type}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $Mail::Mailer::Mailers{$type}
shift() $Mail::Mailer::Mailers{$type}
Truth table0%0/3
A || B
A= shift()B= $Mail::Mailer::Mailers{$type}
ABresult
1X1
011
000
69  
70 0 ▶ is_exe $exe
Branch: 1 branch
BranchTrueFalseTotal
&is_exe($exe) or carp("Cannot locate '${exe}'") 0 0 0
71 or carp "Cannot locate '$exe'";
72  
73 0 $MailerType = $type;
74 0 $Mailers{$MailerType} = $exe;
75 }
76  
77 sub to_array($)
78 0 ▶{ my ($self, $thing) = @_;
Subroutine to_array: not called CC=2 SCAR=17.9
Pod undocumented
79 0 ▶ ref $thing ? @$thing : $thing;
Branch: 1 branch
BranchTrueFalseTotal
ref $thing ? : 0 0 0
80 }
81  
82 sub is_exe($)
83 15 ▶{ my $exe = shift || '';
Subroutine is_exe: called CC=10 SCAR=26.5
Pod undocumented
Condition100%2/2
shift() || ''
l!l
69
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
84  
85 15 foreach my $cmd (split /\;/, $exe)
86 15 { $cmd =~ s/^\s+//;
87  
88 # remove any options
89 15 my $name = ($cmd =~ /^(\S+)/)[0];
90  
91 # check for absolute or relative path
92 15 ▶ return $cmd
Branch: 1 branch
BranchTrueFalseTotal
if -x $name and not -d $name and $name =~ m[[\\/]] 0 15 15
Condition33%2/6
-x $name and not -d $name and $name =~ m[[\\/]]
!ll&&!rl&&r
1500
-x $name and not -d $name
!ll&&!rl&&r
1500
MC/DC0%0/3
-x $name and not -d $name and $name =~ m[[\\/]]
-x $name not -d $name $name =~ m[[\\/]]
Truth table25%1/4
A and not(B) and C
A= -x $nameB= not -d $nameC= $name =~ m[[\\/]]
ABCresult
0XX0
10X0
1100
1111
93 if -x $name && ! -d $name && $name =~ m![\\/]!;
94  
95 15 ▶ if(defined $ENV{PATH})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"PATH"}) 15 0 15
96 15 { foreach my $dir (split /$Config{path_sep}/, $ENV{PATH})
97 150 ▶ { return "$dir/$cmd"
Branch: 1 branch
BranchTrueFalseTotal
if -x "$dir/$name" and not -d "$dir/$name" 0 150 150
Condition33%1/3
-x "$dir/$name" and not -d "$dir/$name"
!ll&&!rl&&r
15000
MC/DC0%0/2
-x "$dir/$name" and not -d "$dir/$name"
-x "$dir/$name" not -d "$dir/$name"
Truth table33%1/3
A and not(B)
A= -x "$dir/$name"B= not -d "$dir/$name"
ABresult
0X0
100
111
98 if -x "$dir/$name" && ! -d "$dir/$name";
99 }
100 }
101 }
102 15 0;
103 }
104  
105  
106 sub new($@)
107 1 ▶{ my ($class, $type, @args) = @_;
Subroutine new: called CC=9 SCAR=27.6
Pod documented
108  
109 1 ▶ unless($type)
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 1 0 1
110 1 ▶ { $MailerType or croak "No MailerType specified";
Branch: 1 branch
BranchTrueFalseTotal
$Mail::Mailer::MailerType or croak("No MailerType specified") 1 0 1
111  
112 1 ▶ warn "No real MTA found, using '$MailerType'"
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Mailer::MailerType eq "testfile" 1 0 1
113 if $MailerType eq 'testfile';
114  
115 1 $type = $MailerType;
116 }
117  
118 1 my $exe = $Mailers{$type};
119  
120 1 ▶ if(defined $exe)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exe) 0 1 1
121 0 ▶ { $exe = is_exe $exe
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 0 0 0
122 if defined $type;
123  
124 0 ▶ $exe ||= $MailerBinary
Branch: 1 branch
BranchTrueFalseTotal
$exe ||= $Mail::Mailer::MailerBinary or croak("No mailer type specified (and no default available), thus can not find executable program.") 0 0 0
Condition0%0/3
$exe ||= $Mail::Mailer::MailerBinary
l!l&&r!l&&!r
000
MC/DC0%0/2
$exe ||= $Mail::Mailer::MailerBinary
$exe $Mail::Mailer::MailerBinary
Truth table0%0/3
A ||= B
A= $exeB= $Mail::Mailer::MailerBinary
ABresult
1X1
011
000
125 or croak "No mailer type specified (and no default available), thus can not find executable program.";
126 }
127  
128 1 $class = "Mail::Mailer::$type";
129 1 ▶ eval "require $class" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class" or die $@ 1 0 1
130  
131 1 my $glob = $class->SUPER::new; # object is a GLOB!
132 1 %{*$glob} = (Exe => $exe, Args => [ @args ]);
133 1 $glob;
134 }
135  
136  
137 sub open($)
138 0 ▶{ my ($self, $hdrs) = @_;
Subroutine open: not called CC=6 SCAR=37.4
Pod documented
139 0 my $exe = *$self->{Exe}; # no exe, then direct smtp
140 0 my $args = *$self->{Args};
141  
142 0 my @to = $self->who_to($hdrs);
143 0 my $sender = $self->who_sender($hdrs);
144
145 0 $self->close; # just in case;
146  
147 0 ▶ if(defined $exe)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exe) { } 0 0 0
148 { # Fork and start a mailer
149 0 my $child = open $self, '|-';
150 0 ▶ defined $child or die "Failed to send: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $child or die "Failed to send: $!" 0 0 0
151  
152 0 ▶ if($child==0)
Branch: 1 branch
BranchTrueFalseTotal
if ($child == 0) 0 0 0
153 { # Child process will handle sending, but this is not real exec()
154 # this is a setup!!!
155 0 ▶ unless($self->exec($exe, $args, \@to, $sender))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->exec($exe, $args, \@to, $sender)) 0 0 0
156 0 { warn $!; # setup failed
157 0 _exit(1); # no DESTROY(), keep it for parent
158 }
159 }
160 }
161 else
162 { # Sending is handled by a subclass
163 0 ▶ $self->exec(undef, $args, \@to)
Branch: 1 branch
BranchTrueFalseTotal
$self->exec(undef, $args, \@to) or die $! 0 0 0
164 or die $!;
165 }
166  
167 0 $self->set_headers($hdrs);
168 0 $self;
169 }
170  
171 sub _cleanup_hdrs($)
172 0 ▶{ foreach my $h (values %{(shift)})
Subroutine _cleanup_hdrs: not called CC=4 SCAR=30.0
173 0 ▶ { foreach (ref $h ? @$h : $h)
Branch: 1 branch
BranchTrueFalseTotal
ref $h ? : 0 0 0
174 0 { s/\n\s*/ /g;
175 0 s/\s+$//;
176 }
177 }
178 }
179  
180 sub exec($$$$)
181 0 ▶{ my($self, $exe, $args, $to, $sender) = @_;
Subroutine exec: not called CC=1 SCAR=6.9
Pod undocumented
182  
183 # Fork and exec the mailer (no shell involved to avoid risks)
184 0 my @exe = split /\s+/, $exe;
185 0 exec @exe, @$args, @$to;
186 }
187  
188 0 ▶sub can_cc { 1 } # overridden in subclass for mailer that can't
Subroutine can_cc: not called CC=1 SCAR=6.9
Pod undocumented
189  
190 sub who_to($)
191 0 ▶{ my($self, $hdrs) = @_;
Subroutine who_to: not called CC=4 SCAR=30.0
Pod undocumented
192 0 my @to = $self->to_array($hdrs->{To});
193 0 ▶ unless($self->can_cc) # Can't cc/bcc so add them to @to
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can_cc) 0 0 0
194 0 ▶ { push @to, $self->to_array($hdrs->{Cc} ) if $hdrs->{Cc};
Branch: 1 branch
BranchTrueFalseTotal
if $hdrs->{"Cc"} 0 0 0
195 0 ▶ push @to, $self->to_array($hdrs->{Bcc}) if $hdrs->{Bcc};
Branch: 1 branch
BranchTrueFalseTotal
if $hdrs->{"Bcc"} 0 0 0
196 }
197 0 @to;
198 }
199  
200 sub who_sender($)
201 0 ▶{ my ($self, $hdrs) = @_;
Subroutine who_sender: not called CC=2 SCAR=17.9
Pod undocumented
202 0 ▶ ($self->to_array($hdrs->{Sender} || $hdrs->{From}))[0];
Condition0%0/3
$hdrs->{'Sender'} || $hdrs->{'From'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hdrs->{'Sender'} || $hdrs->{'From'}
$hdrs->{'Sender'} $hdrs->{'From'}
Truth table0%0/3
A || B
A= $hdrs->{'Sender'}B= $hdrs->{'From'}
ABresult
1X1
011
000
203 }
204  
205 0 ▶sub epilogue {
Subroutine epilogue: not called CC=1 SCAR=0.0
Pod undocumented
206 # This could send a .signature, also see ::smtp subclass
207 }
208  
209 sub close(@)
210 0 ▶{ my $self = shift;
Subroutine close: not called CC=2 SCAR=17.9
Pod undocumented
211 0 ▶ fileno $self or return;
Branch: 1 branch
BranchTrueFalseTotal
fileno $self or return 0 0 0
212  
213 0 $self->epilogue;
214 0 CORE::close $self;
215 }
216  
217 1 ▶sub DESTROY { shift->close }
Subroutine DESTROY: called CC=1 SCAR=0.0
218  
219 #--------------
220  
221 3 1;
« Mail/Field.pm ↑ Summary Mail/Cap.pm »