aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Email/Sender.pm
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2017-03-17 22:18:45 +0000
committerMatthew Somerville <matthew-github@dracos.co.uk>2017-03-28 13:10:38 +0100
commitb26da0da5e1f8631646a34fdacbce9bb5bc3b706 (patch)
treecd42e16d51054606b85bf6ff28a47e586437406c /perllib/FixMyStreet/Email/Sender.pm
parent02fcb1606bc2b739fdc798e5ca06f2ed1b6bf6ea (diff)
Upgrade to using Email::Sender.
Email::Send is long deprecated and uses submodules that no longer work correctly (e.g. Net::SMTP::TLS breaks with recent IO::Socket::SSL). We create an Email::Sender subclass to perform the same functionality and this also simplifies the email code with simpler envelope handling. Bundle Email::Sender::Transport::SMTP to include fix from https://github.com/rjbs/Email-Sender/issues/46
Diffstat (limited to 'perllib/FixMyStreet/Email/Sender.pm')
-rw-r--r--perllib/FixMyStreet/Email/Sender.pm50
1 files changed, 50 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/Email/Sender.pm b/perllib/FixMyStreet/Email/Sender.pm
new file mode 100644
index 000000000..e6148a56c
--- /dev/null
+++ b/perllib/FixMyStreet/Email/Sender.pm
@@ -0,0 +1,50 @@
+package FixMyStreet::Email::Sender;
+
+use parent Email::Sender::Simple;
+use strict;
+use warnings;
+
+use Email::Sender::Util;
+use FixMyStreet;
+
+=head1 NAME
+
+FixMyStreet::Email::Sender
+
+=head1 DESCRIPTION
+
+Subclass of Email::Sender - configuring it correctly according to our config.
+
+If the config value 'SMTP_SMARTHOST' is set then email is routed via SMTP to
+that. Otherwise it is sent using a 'sendmail' like binary on the local system.
+
+And finally if if FixMyStreet->test_mode returns true then emails are not sent
+at all but are stored in memory for the test suite to inspect (using
+Email::Send::Test).
+
+=cut
+
+sub build_default_transport {
+ if ( FixMyStreet->test_mode ) {
+ Email::Sender::Util->easy_transport(Test => {});
+ } elsif ( my $smtp_host = FixMyStreet->config('SMTP_SMARTHOST') ) {
+ my $type = FixMyStreet->config('SMTP_TYPE') || '';
+ my $port = FixMyStreet->config('SMTP_PORT') || '';
+ my $username = FixMyStreet->config('SMTP_USERNAME') || '';
+ my $password = FixMyStreet->config('SMTP_PASSWORD') || '';
+
+ my $ssl = $type eq 'tls' ? 'starttls' : $type eq 'ssl' ? 'ssl' : '';
+ my $args = {
+ host => $smtp_host,
+ ssl => $ssl,
+ sasl_username => $username,
+ sasl_password => $password,
+ };
+ $args->{port} = $port if $port;
+ Email::Sender::Util->easy_transport(SMTP => $args);
+ } else {
+ Email::Sender::Util->easy_transport(Sendmail => {});
+ }
+}
+
+1;