MT#55576 Switch mail interface from sendmail(1) to SMTP

We had to disable plenty of systemd security hardening options because
NGCP::Panel::Utils::Email used the sendmail(1) interface.

Switch to usage of sending mail via SMTP Net::SMTP instead, which should
allow us to apply our common systemd hardening.  Note that we can't drop
the dependency on libemail-sender-perl yet, as
share/tools/generate_invoices.pl:use still relies on it.

Sendmail doesn't talk to any SMTP port, as it invokes the sendmail(1)
binary and writing the message to its stdin. Default to SMTP on
localhost with port 25, to get similar behavior.

While at it, fixed the swapped log levels (original $c->log->info was
used for failure, and $c->log->error on success).

Change-Id: Ie7120d4a8e55eb645c99b5d52c8326e7a7d42f8b
master
Michael Prokop 4 weeks ago
parent ce12aed828
commit 5993a1e1bc

@ -41,6 +41,11 @@ log4perl.appender.Default.layout.ConversionPattern=%d{ISO8601} [%p] [%F +%L] %m{
email postmaster@domain.invalid
</contact>
<email>
smtp_host localhost
smtp_port 25
</email>
<features>
callflow 1
multidomain 1

@ -2,19 +2,19 @@ package NGCP::Panel::Utils::Email;
use Sipwise::Base;
use Template;
use Email::Sender::Simple qw();
use Net::SMTP;
use Email::Simple;
use Email::Simple::Creator;
use Email::Sender::Transport::Sendmail qw();
sub send_email {
my %args = @_;
my $subject = $args{subject};
my $body = $args{body};
my $from = $args{from};
my $to = $args{to};
my $subject = $args{subject};
my $body = $args{body};
my $from = $args{from};
my $to = $args{to};
my $smtp_host = $args{smtp_host} // 'localhost';
my $smtp_port = $args{smtp_port} // 25;
my $transport = Email::Sender::Transport::Sendmail->new;
my $email = Email::Simple->create(
header => [
To => $to,
@ -23,11 +23,15 @@ sub send_email {
],
body => $body,
);
try {
Email::Sender::Simple->send($email, { transport => $transport });
} catch($e) {
return $e->message;
}
my $smtp = Net::SMTP->new($smtp_host, Port => $smtp_port, Timeout => 30);
return "Failed to connect to SMTP server $smtp_host:$smtp_port" unless $smtp;
$smtp->mail($from) or return "SMTP MAIL FROM failed: " . $smtp->message;
$smtp->to($to) or return "SMTP RCPT TO failed: " . $smtp->message;
$smtp->data($email->as_string) or return "SMTP DATA failed: " . $smtp->message;
$smtp->quit;
return;
}
@ -45,29 +49,19 @@ sub send_template {
$t->process(\$subject, $vars, \$processed_subject) ||
die "error processing email template, type=".$t->error->type.", info='".$t->error->info."'";
my $smtp_config = $c->config->{email} // {};
my $err = send_email(
subject => $processed_subject,
body => $processed_body,
from => $from,
to => $to,
subject => $processed_subject,
body => $processed_body,
from => $from,
to => $to,
smtp_host => $smtp_config->{smtp_host},
smtp_port => $smtp_config->{smtp_port},
);
#my $template_processed = process_template({
# subject => $subject,
# body => $body,
# from_email => $from,
# to => $to,
#},$vars);
#
#send_email(
# subject => $template_processed->{subject},
# body => $template_processed->{body},
# from => $template_processed->{from_email},
# to => $template_processed->{to},
#);
$err ? $c->log->info("Could not send email from '" . $c->qs($from) . "' to '" . $c->qs($to) . "' error=$err")
: $c->log->error("Successfully handed over mail from '" . $c->qs($from) . "' to '" . $c->qs($to) . "'");
$err ? $c->log->error("Could not send email from '" . $c->qs($from) . "' to '" . $c->qs($to) . "' error=$err")
: $c->log->info("Successfully handed over mail from '" . $c->qs($from) . "' to '" . $c->qs($to) . "'");
return 1;
}

Loading…
Cancel
Save