| 1 | package Lemonldap::NG::Common::EmailTransport; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Email::Sender::Transport::SMTP qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use MIME::Entity; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Email::Sender::Simple qw(sendmail); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Email::Date::Format qw(email_date); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '2.23.3'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # Check that SMTPAuthMech can be honored: it requires Authen::SASL and an | ||||||||||||||||||||||||||||
| 12 | # Email::Sender version providing the sasl_authenticator attribute (>= 1.300032) | ||||||||||||||||||||||||||||
| 13 | # Returns an error message, or undef when the configuration is usable | ||||||||||||||||||||||||||||
| 14 | 3 | ▶ | sub checkSasl { | ||||||||||||||||||||||||||
|
Subroutine
checkSasl: called CC=6 SCAR=19.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 3 | my ( $class, $conf, $transportClass ) = @_; | |||||||||||||||||||||||||||
| 16 | 3 | ▶ | return undef unless $conf->{SMTPAuthMech} and $conf->{SMTPAuthUser}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$conf->{"SMTPAuthMech"} and $conf->{"SMTPAuthUser"}
MC/DC0%0/2
$conf->{"SMTPAuthMech"} and $conf->{"SMTPAuthUser"}
$conf->{"SMTPAuthMech"}
$conf->{"SMTPAuthUser"}
Truth table33%1/3
A and B
A= $conf->{"SMTPAuthMech"}B= $conf->{"SMTPAuthUser"}
| |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | $transportClass ||= 'Email::Sender::Transport::SMTP'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$transportClass ||= "Email::Sender::Transport::SMTP"
MC/DC100%1/1
$transportClass ||= "Email::Sender::Transport::SMTP"
$transportClass
Truth table100%2/2
A
A= $transportClass
| |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | return "Choosing the SASL mechanism (SMTPAuthMech) is not supported by " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | . "$transportClass, Email::Sender 1.300032 or higher is required" | ||||||||||||||||||||||||||||
| 20 | unless $transportClass->can('sasl_authenticator'); | ||||||||||||||||||||||||||||
| 21 | 3 | eval { require Authen::SASL; }; | |||||||||||||||||||||||||||
| 22 | 3 | ▶ | return "Choosing the SASL mechanism (SMTPAuthMech) requires Authen::SASL" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | if $@; | ||||||||||||||||||||||||||||
| 24 | 3 | return undef; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Build the SASL related arguments given to the transport constructor. | ||||||||||||||||||||||||||||
| 28 | # When SMTPAuthMech is set, an Authen::SASL object restricted to the wanted | ||||||||||||||||||||||||||||
| 29 | # mechanism(s) is used instead of sasl_username/sasl_password: else Net::SMTP | ||||||||||||||||||||||||||||
| 30 | # builds itself a SASL client using all the mechanisms advertised by the | ||||||||||||||||||||||||||||
| 31 | # server, and picks the "strongest" one, which may be broken server side | ||||||||||||||||||||||||||||
| 32 | # (DIGEST-MD5 on some providers for example). | ||||||||||||||||||||||||||||
| 33 | # NB: sasl_authenticator and sasl_username are mutually exclusive. | ||||||||||||||||||||||||||||
| 34 | 6 | ▶ | sub _saslArgs { | ||||||||||||||||||||||||||
|
Subroutine
_saslArgs: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 35 | 6 | my ( $transportClass, $conf ) = @_; | |||||||||||||||||||||||||||
| 36 | 6 | ▶ | return () unless $conf->{SMTPAuthUser}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | if ( $conf->{SMTPAuthMech} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 2 | my $error = __PACKAGE__->checkSasl( $conf, $transportClass ); | |||||||||||||||||||||||||||
| 39 | 2 | ▶ | die "$error\n" if $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | return ( | ||||||||||||||||||||||||||||
| 41 | sasl_authenticator => Authen::SASL->new( | ||||||||||||||||||||||||||||
| 42 | mechanism => $conf->{SMTPAuthMech}, | ||||||||||||||||||||||||||||
| 43 | callback => { | ||||||||||||||||||||||||||||
| 44 | user => $conf->{SMTPAuthUser}, | ||||||||||||||||||||||||||||
| 45 | authname => $conf->{SMTPAuthUser}, | ||||||||||||||||||||||||||||
| 46 | pass => $conf->{SMTPAuthPass}, | ||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||
| 48 | ) | ||||||||||||||||||||||||||||
| 49 | 2 | ); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | return ( | ||||||||||||||||||||||||||||
| 52 | sasl_username => $conf->{SMTPAuthUser}, | ||||||||||||||||||||||||||||
| 53 | sasl_password => $conf->{SMTPAuthPass}, | ||||||||||||||||||||||||||||
| 54 | 2 | ); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=36.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 7 | my ( $class, $conf ) = @_; | |||||||||||||||||||||||||||
| 59 | 7 | my $transport; | |||||||||||||||||||||||||||
| 60 | 7 | my $smtpTls = $conf->{SMTPTLS}; | |||||||||||||||||||||||||||
| 61 | return undef | ||||||||||||||||||||||||||||
| 62 | 7 | ▶ | unless ( $conf->{SMTPServer} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 6 | ▶ | if ( $smtpTls | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$smtpTls and $Email::Sender::Transport::SMTP::VERSION < "1.300027"
MC/DC0%0/2
$smtpTls and $Email::Sender::Transport::SMTP::VERSION < "1.300027"
$smtpTls
$Email::Sender::Transport::SMTP::VERSION < "1.300027"
Truth table33%1/3
A and B
A= $smtpTlsB= $Email::Sender::Transport::SMTP::VERSION < "1.300027"
| |||||||||||||||||||||||||||||
| 64 | and $Email::Sender::Transport::SMTP::VERSION < 1.300027 ) | ||||||||||||||||||||||||||||
| 65 | { | ||||||||||||||||||||||||||||
| 66 | # Try to use Email::Sender::Transport::SMTPS | ||||||||||||||||||||||||||||
| 67 | 0 | eval { require Email::Sender::Transport::SMTPS; }; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # fall back to Email::Sender::Transport::SMTP if not available | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | unless ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | $transport = Email::Sender::Transport::SMTPS->new( | ||||||||||||||||||||||||||||
| 72 | host => $conf->{SMTPServer}, | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | ( $conf->{SMTPPort} ? ( port => $conf->{SMTPPort} ) : () ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | _saslArgs( 'Email::Sender::Transport::SMTPS', $conf ), | ||||||||||||||||||||||||||||
| 75 | ssl => $smtpTls, | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | 0 | return $transport; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | else { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ( $smtpTls and $smtpTls eq "ssl" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$smtpTls and $smtpTls eq "ssl"
MC/DC0%0/2
$smtpTls and $smtpTls eq "ssl"
$smtpTls
$smtpTls eq "ssl"
Truth table0%0/3
A and B
A= $smtpTlsB= $smtpTls eq "ssl"
| |||||||||||||||||||||||||||||
| 81 | 0 | $smtpTls = 1; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||
| 84 | 0 | $smtpTls = 0; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | $transport = Email::Sender::Transport::SMTP->new( | ||||||||||||||||||||||||||||
| 89 | host => $conf->{SMTPServer}, | ||||||||||||||||||||||||||||
| 90 | ( $conf->{SMTPPort} ? ( port => $conf->{SMTPPort} ) : () ), | ||||||||||||||||||||||||||||
| 91 | _saslArgs( 'Email::Sender::Transport::SMTP', $conf ), | ||||||||||||||||||||||||||||
| 92 | ( $smtpTls ? ( ssl => $smtpTls ) : () ), | ||||||||||||||||||||||||||||
| 93 | ( | ||||||||||||||||||||||||||||
| 94 | $conf->{SMTPTLSOpts} | ||||||||||||||||||||||||||||
| 95 | ? ( ssl_options => $conf->{SMTPTLSOpts} ) | ||||||||||||||||||||||||||||
| 96 | 6 | ▶ | : () | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 97 | ), | ||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||
| 99 | 6 | return $transport; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | ▶ | sub configTest { | ||||||||||||||||||||||||||
|
Subroutine
configTest: called CC=12 SCAR=44.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 1 | my ( $class, $conf ) = @_; | |||||||||||||||||||||||||||
| 104 | 1 | my $res = 1; | |||||||||||||||||||||||||||
| 105 | 1 | my $message; | |||||||||||||||||||||||||||
| 106 | 1 | ▶ | if ( $Email::Sender::Transport::SMTP::VERSION < 1.300027 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ( $conf->{SMTPTLS} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | $message = "Email::Sender < 1.3.00027 does not validate" | |||||||||||||||||||||||||||
| 109 | . " server identity when using SMTPS, use at your own risks"; | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ( $conf->{SMTPTLSOpts} and keys %{ $conf->{SMTPTLSOpts} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$conf->{"SMTPTLSOpts"} and keys %{$$conf{"SMTPTLSOpts"};}
MC/DC0%0/2
$conf->{"SMTPTLSOpts"} and keys %{$$conf{"SMTPTLSOpts"};}
$conf->{"SMTPTLSOpts"}
keys %{$$conf{"SMTPTLSOpts"};}
Truth table0%0/3
A and B
A= $conf->{"SMTPTLSOpts"}B= keys %{$$conf{"SMTPTLSOpts"};}
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $message = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | ( $message ? "$message. " : "" ) | ||||||||||||||||||||||||||||
| 114 | . "Setting TLS parameters is not supported on " | ||||||||||||||||||||||||||||
| 115 | . "Email::Sender < 1.3.00027"; | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 0 | eval { require Email::Sender::Transport::SMTPS; }; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ( $conf->{SMTPTLS} and $conf->{SMTPTLS} eq "starttls" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$conf->{"SMTPTLS"} and $conf->{"SMTPTLS"} eq "starttls"
MC/DC0%0/2
$conf->{"SMTPTLS"} and $conf->{"SMTPTLS"} eq "starttls"
$conf->{"SMTPTLS"}
$conf->{"SMTPTLS"} eq "starttls"
Truth table0%0/3
A and B
A= $conf->{"SMTPTLS"}B= $conf->{"SMTPTLS"} eq "starttls"
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $message = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | ( $message ? "$message. " : "" ) | ||||||||||||||||||||||||||||
| 122 | . "StartTLS is not supported, " | ||||||||||||||||||||||||||||
| 123 | . "install Email::Sender::Transport::SMTPS"; | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 1 | ▶ | if ( my $error = $class->checkSasl($conf) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | $message = ( $message ? "$message. " : "" ) . $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 1 | return ( $res, $message ); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub sendTestMail { | ||||||||||||||||||||||||||
|
Subroutine
sendTestMail: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | 0 | my ( $conf, $dest ) = @_; | |||||||||||||||||||||||||||
| 135 | 0 | my $transport = Lemonldap::NG::Common::EmailTransport->new($conf); | |||||||||||||||||||||||||||
| 136 | my $message = MIME::Entity->build( | ||||||||||||||||||||||||||||
| 137 | From => $conf->{mailFrom}, | ||||||||||||||||||||||||||||
| 138 | 0 | To => $dest, | |||||||||||||||||||||||||||
| 139 | Subject => 'LemonLDAP::NG test email', | ||||||||||||||||||||||||||||
| 140 | Type => 'TEXT', | ||||||||||||||||||||||||||||
| 141 | Data => 'This test message was sent from the LemonLDAP::NG Manager', | ||||||||||||||||||||||||||||
| 142 | Type => 'text/plain', | ||||||||||||||||||||||||||||
| 143 | Date => email_date, | ||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Send the mail | ||||||||||||||||||||||||||||
| 147 | 0 | eval { sendmail( $message->stringify, { transport => $transport } ); }; | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | my $error = ( $@->isa('Throwable::Error') ? $@->message : $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | die $error; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | 1; | |||||||||||||||||||||||||||