| 1 |
package Lemonldap::NG::Common::EmailTransport; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Email::Sender::Transport::SMTP qw(); |
| 5 |
use MIME::Entity; |
| 6 |
use Email::Sender::Simple qw(sendmail); |
| 7 |
use Email::Date::Format qw(email_date); |
| 8 |
|
| 9 |
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 |
sub checkSasl { |
| 15 |
my ( $class, $conf, $transportClass ) = @_; |
| 16 |
return undef unless $conf->{SMTPAuthMech} and $conf->{SMTPAuthUser}; |
| 17 |
$transportClass ||= 'Email::Sender::Transport::SMTP'; |
| 18 |
return "Choosing the SASL mechanism (SMTPAuthMech) is not supported by " |
| 19 |
. "$transportClass, Email::Sender 1.300032 or higher is required" |
| 20 |
unless $transportClass->can('sasl_authenticator'); |
| 21 |
eval { require Authen::SASL; }; |
| 22 |
return "Choosing the SASL mechanism (SMTPAuthMech) requires Authen::SASL" |
| 23 |
if $@; |
| 24 |
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 |
sub _saslArgs { |
| 35 |
my ( $transportClass, $conf ) = @_; |
| 36 |
return () unless $conf->{SMTPAuthUser}; |
| 37 |
if ( $conf->{SMTPAuthMech} ) { |
| 38 |
my $error = __PACKAGE__->checkSasl( $conf, $transportClass ); |
| 39 |
die "$error\n" if $error; |
| 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 |
); |
| 50 |
} |
| 51 |
return ( |
| 52 |
sasl_username => $conf->{SMTPAuthUser}, |
| 53 |
sasl_password => $conf->{SMTPAuthPass}, |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
sub new { |
| 58 |
my ( $class, $conf ) = @_; |
| 59 |
my $transport; |
| 60 |
my $smtpTls = $conf->{SMTPTLS}; |
| 61 |
return undef |
| 62 |
unless ( $conf->{SMTPServer} ); |
| 63 |
if ( $smtpTls |
| 64 |
and $Email::Sender::Transport::SMTP::VERSION < 1.300027 ) |
| 65 |
{ |
| 66 |
# Try to use Email::Sender::Transport::SMTPS |
| 67 |
eval { require Email::Sender::Transport::SMTPS; }; |
| 68 |
|
| 69 |
# fall back to Email::Sender::Transport::SMTP if not available |
| 70 |
unless ($@) { |
| 71 |
$transport = Email::Sender::Transport::SMTPS->new( |
| 72 |
host => $conf->{SMTPServer}, |
| 73 |
( $conf->{SMTPPort} ? ( port => $conf->{SMTPPort} ) : () ), |
| 74 |
_saslArgs( 'Email::Sender::Transport::SMTPS', $conf ), |
| 75 |
ssl => $smtpTls, |
| 76 |
); |
| 77 |
return $transport; |
| 78 |
} |
| 79 |
else { |
| 80 |
if ( $smtpTls and $smtpTls eq "ssl" ) { |
| 81 |
$smtpTls = 1; |
| 82 |
} |
| 83 |
else { |
| 84 |
$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 |
: () |
| 97 |
), |
| 98 |
); |
| 99 |
return $transport; |
| 100 |
} |
| 101 |
|
| 102 |
sub configTest { |
| 103 |
my ( $class, $conf ) = @_; |
| 104 |
my $res = 1; |
| 105 |
my $message; |
| 106 |
if ( $Email::Sender::Transport::SMTP::VERSION < 1.300027 ) { |
| 107 |
if ( $conf->{SMTPTLS} ) { |
| 108 |
$message = "Email::Sender < 1.3.00027 does not validate" |
| 109 |
. " server identity when using SMTPS, use at your own risks"; |
| 110 |
} |
| 111 |
if ( $conf->{SMTPTLSOpts} and keys %{ $conf->{SMTPTLSOpts} } ) { |
| 112 |
$message = |
| 113 |
( $message ? "$message. " : "" ) |
| 114 |
. "Setting TLS parameters is not supported on " |
| 115 |
. "Email::Sender < 1.3.00027"; |
| 116 |
} |
| 117 |
eval { require Email::Sender::Transport::SMTPS; }; |
| 118 |
if ($@) { |
| 119 |
if ( $conf->{SMTPTLS} and $conf->{SMTPTLS} eq "starttls" ) { |
| 120 |
$message = |
| 121 |
( $message ? "$message. " : "" ) |
| 122 |
. "StartTLS is not supported, " |
| 123 |
. "install Email::Sender::Transport::SMTPS"; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
if ( my $error = $class->checkSasl($conf) ) { |
| 128 |
$message = ( $message ? "$message. " : "" ) . $error; |
| 129 |
} |
| 130 |
return ( $res, $message ); |
| 131 |
} |
| 132 |
|
| 133 |
sub sendTestMail { |
| 134 |
my ( $conf, $dest ) = @_; |
| 135 |
my $transport = Lemonldap::NG::Common::EmailTransport->new($conf); |
| 136 |
my $message = MIME::Entity->build( |
| 137 |
From => $conf->{mailFrom}, |
| 138 |
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 |
eval { sendmail( $message->stringify, { transport => $transport } ); }; |
| 148 |
if ($@) { |
| 149 |
my $error = ( $@->isa('Throwable::Error') ? $@->message : $@ ); |
| 150 |
die $error; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
1; |