Mail/Mailer/smtp.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 126 CC 4 SCAR 19.2
CC
4
Cov
0%
exec
20.0
can_cc
2.0
set_headers
2.0
CRAP 6.8
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::smtp;{
10 our $VERSION = '2.22';
11 }
12  
13 use base 'Mail::Mailer::rfc822';
14  
15 use strict;
16  
17 use Net::SMTP;
18 use Mail::Util qw(mailaddress);
19 use Carp;
20  
21 sub can_cc { 0 }
22  
23 sub exec {
24 my ($self, $exe, $args, $to) = @_;
25 my %opt = @$args;
26 my $host = $opt{Server} || undef;
27 $opt{Debug} ||= 0;
28  
29 my $smtp = Net::SMTP->new($host, %opt)
30 or return undef;
31  
32 if($opt{StartTLS})
33 { $smtp->starttls
34 or return undef;
35 }
36  
37 if($opt{Auth})
38 { $smtp->auth(@{$opt{Auth}})
39 or return undef;
40 }
41  
42 ${*$self}{sock} = $smtp;
43  
44 $smtp->mail($opt{From} || mailaddress());
45 $smtp->to($_) for @$to;
46 $smtp->data;
47  
48 untie *$self if tied *$self;
49 tie *$self, 'Mail::Mailer::smtp::pipe', $self;
50 $self;
51 }
52  
53 sub set_headers($)
54 { my ($self, $hdrs) = @_;
55 $self->SUPER::set_headers
56 ( { From => "<" . mailaddress() . ">"
57 , %$hdrs
58 , 'X-Mailer' => "Mail::Mailer[v$Mail::Mailer::VERSION] Net::SMTP[v$Net::SMTP::VERSION]"
59 }
60 );
61 }
62  
63 sub epilogue()
64 { my $self = shift;
65 my $sock = ${*$self}{sock};
66  
67 my $ok = $sock->dataend;
68 $sock->quit;
69  
70 delete ${*$self}{sock};
71 untie *$self;
72 $ok;
73 }
74  
75 sub close(@)
76 { my ($self, @to) = @_;
77 my $sock = ${*$self}{sock};
78  
79 $sock && fileno $sock
80 or return 1;
81  
82 my $ok = $self->epilogue;
83  
84 # Epilogue should destroy the SMTP filehandle,
85 # but just to be on the safe side.
86 $sock && fileno $sock
87 or return $ok;
88  
89 close $sock
90 or croak 'Cannot destroy socket filehandle';
91  
92 $ok;
93 }
94  
95 package Mail::Mailer::smtp::pipe;{
96 our $VERSION = '2.22';
97 }
98  
99  
100 sub TIEHANDLE
101 { my ($class, $self) = @_;
102 my $sock = ${*$self}{sock};
103 bless \$sock, $class;
104 }
105  
106 sub PRINT
107 { my $self = shift;
108 my $sock = $$self;
109 $sock->datasend( @_ );
110 }
111  
112 1;