| 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.0'; |
| 10 |
|
| 11 |
sub new { |
| 12 |
my ( $class, $conf ) = @_; |
| 13 |
my $transport; |
| 14 |
my $smtpTls = $conf->{SMTPTLS}; |
| 15 |
return undef |
| 16 |
unless ( $conf->{SMTPServer} ); |
| 17 |
if ( $smtpTls |
| 18 |
and $Email::Sender::Transport::SMTP::VERSION < 1.300027 ) |
| 19 |
{ |
| 20 |
# Try to use Email::Sender::Transport::SMTPS |
| 21 |
eval { require Email::Sender::Transport::SMTPS; }; |
| 22 |
|
| 23 |
# fall back to Email::Sender::Transport::SMTP if not available |
| 24 |
unless ($@) { |
| 25 |
$transport = Email::Sender::Transport::SMTPS->new( |
| 26 |
host => $conf->{SMTPServer}, |
| 27 |
( $conf->{SMTPPort} ? ( port => $conf->{SMTPPort} ) : () ), |
| 28 |
( |
| 29 |
$conf->{SMTPAuthUser} |
| 30 |
? ( |
| 31 |
sasl_username => $conf->{SMTPAuthUser}, |
| 32 |
sasl_password => $conf->{SMTPAuthPass} |
| 33 |
) |
| 34 |
: () |
| 35 |
), |
| 36 |
ssl => $smtpTls, |
| 37 |
); |
| 38 |
return $transport; |
| 39 |
} |
| 40 |
else { |
| 41 |
if ( $smtpTls and $smtpTls eq "ssl" ) { |
| 42 |
$smtpTls = 1; |
| 43 |
} |
| 44 |
else { |
| 45 |
$smtpTls = 0; |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
$transport = Email::Sender::Transport::SMTP->new( |
| 50 |
host => $conf->{SMTPServer}, |
| 51 |
( $conf->{SMTPPort} ? ( port => $conf->{SMTPPort} ) : () ), |
| 52 |
( |
| 53 |
$conf->{SMTPAuthUser} |
| 54 |
? ( |
| 55 |
sasl_username => $conf->{SMTPAuthUser}, |
| 56 |
sasl_password => $conf->{SMTPAuthPass} |
| 57 |
) |
| 58 |
: () |
| 59 |
), |
| 60 |
( $smtpTls ? ( ssl => $smtpTls ) : () ), |
| 61 |
( |
| 62 |
$conf->{SMTPTLSOpts} ? ( ssl_options => $conf->{SMTPTLSOpts} ) |
| 63 |
: () |
| 64 |
), |
| 65 |
); |
| 66 |
return $transport; |
| 67 |
} |
| 68 |
|
| 69 |
sub configTest { |
| 70 |
my ( $class, $conf ) = @_; |
| 71 |
my $res = 1; |
| 72 |
my $message; |
| 73 |
if ( $Email::Sender::Transport::SMTP::VERSION < 1.300027 ) { |
| 74 |
if ( $conf->{SMTPTLS} ) { |
| 75 |
$message = "Email::Sender < 1.3.00027 does not validate" |
| 76 |
. " server identity when using SMTPS, use at your own risks"; |
| 77 |
} |
| 78 |
if ( $conf->{SMTPTLSOpts} and keys %{ $conf->{SMTPTLSOpts} } ) { |
| 79 |
$message = |
| 80 |
( $message ? "$message. " : "" ) |
| 81 |
. "Setting TLS parameters is not supported on " |
| 82 |
. "Email::Sender < 1.3.00027"; |
| 83 |
} |
| 84 |
eval { require Email::Sender::Transport::SMTPS; }; |
| 85 |
if ($@) { |
| 86 |
if ( $conf->{SMTPTLS} and $conf->{SMTPTLS} eq "starttls" ) { |
| 87 |
$message = |
| 88 |
( $message ? "$message. " : "" ) |
| 89 |
. "StartTLS is not supported, " |
| 90 |
. "install Email::Sender::Transport::SMTPS"; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
return $res, $message; |
| 95 |
} |
| 96 |
|
| 97 |
sub sendTestMail { |
| 98 |
my ( $conf, $dest ) = @_; |
| 99 |
my $transport = Lemonldap::NG::Common::EmailTransport->new($conf); |
| 100 |
my $message = MIME::Entity->build( |
| 101 |
From => $conf->{mailFrom}, |
| 102 |
To => $dest, |
| 103 |
Subject => 'LemonLDAP::NG test email', |
| 104 |
Type => 'TEXT', |
| 105 |
Data => 'This test message was sent from the LemonLDAP::NG Manager', |
| 106 |
Type => 'text/plain', |
| 107 |
Date => email_date, |
| 108 |
); |
| 109 |
|
| 110 |
# Send the mail |
| 111 |
eval { sendmail( $message->stringify, { transport => $transport } ); }; |
| 112 |
if ($@) { |
| 113 |
my $error = ( $@->isa('Throwable::Error') ? $@->message : $@ ); |
| 114 |
die $error; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
1; |