From cfef2e41957051211399ba9bef55cff633cd9544 Mon Sep 17 00:00:00 2001 From: Guillaume Rousse <guillaume.rousse@renater.fr> Date: Thu, 26 Jul 2018 14:59:11 +0200 Subject: [PATCH] use MIME::Lite to create mail notifications --- README.md | 1 + lib/AccountManager/App.pm | 50 +++++++++---------- templates/Makefile.am | 4 +- ....eml => send_authentication_token.tt2.txt} | 5 -- ....eml => send_authentication_token.tt2.txt} | 5 -- 5 files changed, 26 insertions(+), 39 deletions(-) rename templates/mail/{send_authentication_token.tt2.eml => send_authentication_token.tt2.txt} (83%) rename templates/renater/mail/{send_authentication_token.tt2.eml => send_authentication_token.tt2.txt} (83%) diff --git a/README.md b/README.md index 8aa6dda..c5a7316 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ It requires the following CPAN distributions: * List-MoreUtils * Locale-Maketext-Lexicon * Log-Any +* MIME-Lite * Rose-DB-Object * Template-Toolkit * Text-CSV diff --git a/lib/AccountManager/App.pm b/lib/AccountManager/App.pm index 40c3b15..73b6296 100644 --- a/lib/AccountManager/App.pm +++ b/lib/AccountManager/App.pm @@ -5,10 +5,11 @@ use warnings; use CGI; use DateTime; -use Encode; use English qw(-no_match_vars); +use Encode; use Log::Any::Adapter; use List::MoreUtils qw(uniq); +use MIME::Lite; use Template; use Template::Constants qw(:chomp); use Text::CSV; @@ -573,28 +574,14 @@ sub req_complete_challenge { ); } - my $sender = $self->{configuration}->{mailer}->{from}; - my $sendmail = $self->{configuration}->{mailer}->{sendmail_path} || - '/usr/sbin/sendmail'; - my $recipient = $self->{in}->{email}; - - open(my $handle, '|-', "$sendmail -f $sender $recipient") or do { - $self->{logger}->errorf("Unable to run sendmail executable: %s", $ERRNO); - $self->respond( - template => 'errors.tt2.html', - data => { - errors => [ "mail_notification_error" ] - } - ); - }; - + # build content + my $content; my $tt2 = Template->new({ ENCODING => 'utf8', PRE_CHOMP => CHOMP_ONE, INCLUDE_PATH => $self->{configuration}->{setup}->{templates_dir} . "/mail", - FILTERS => { mime => [ sub { return encode("MIME-Header", $_[0]); }, 0 ] }, }); - my $template = 'send_authentication_token.tt2.eml'; + my $template = 'send_authentication_token.tt2.txt'; my $data = { app => { url => $self->{configuration}->{app}->{url}, @@ -602,17 +589,28 @@ sub req_complete_challenge { version => $self->{configuration}->{app}->{version}, }, sourceip => $ENV{REMOTE_ADDR}, - from => $sender, - to => $recipient, + to => $self->{in}->{email}, entityid => $self->{in}->{entityid}, token => $token->token(), - lh => $self->{lh}, + lh => $self->{lh}, }; + $tt2->process($template, $data, \$content); + + # wrap content in message + my $message = MIME::Lite->new( + From => sprintf('eduGAIN Access Check <%s>', $self->{configuration}->{mailer}->{from}), + To => $self->{in}->{email}, + Subject => encode("MIME-Header", sprintf('[eduGAIN Access Check] %s', $self->{lh}->maketext("Test accounts request"))), + Type =>'text/plain', + Data => $content, + ); - binmode($handle, ":utf8"); - - unless ($tt2->process($template, $data, $handle)) { - $self->{logger}->errorf("Mail notification error: %s", $tt2->error()); + # send message + eval { + $message->send_by_sendmail(); + }; + if ($EVAL_ERROR) { + $self->{logger}->errorf("Mail notification error: %s", $EVAL_ERROR); $self->respond( template => 'errors.tt2.html', data => { @@ -621,8 +619,6 @@ sub req_complete_challenge { ); } - close $handle; - $self->{logger}->infof( "Token send to %s for entityid=%s;token=%s", $self->{in}->{email}, diff --git a/templates/Makefile.am b/templates/Makefile.am index 641da5e..9a18e24 100644 --- a/templates/Makefile.am +++ b/templates/Makefile.am @@ -1,6 +1,6 @@ nobase_templates_DATA = \ accounts/accounts.php.tt2 \ - mail/send_authentication_token.tt2.eml \ + mail/send_authentication_token.tt2.txt \ web/errors.tt2.html \ web/home.tt2.html \ web/index.tt2.html \ @@ -10,7 +10,7 @@ nobase_templates_DATA = \ web/complete_challenge.tt2.html \ web/create_accounts.tt2.html \ renater/accounts/accounts.php.tt2 \ - renater/mail/send_authentication_token.tt2.eml \ + renater/mail/send_authentication_token.tt2.txt \ renater/web/errors.tt2.html \ renater/web/home.tt2.html \ renater/web/index.tt2.html \ diff --git a/templates/mail/send_authentication_token.tt2.eml b/templates/mail/send_authentication_token.tt2.txt similarity index 83% rename from templates/mail/send_authentication_token.tt2.eml rename to templates/mail/send_authentication_token.tt2.txt index ca2a373..179001c 100644 --- a/templates/mail/send_authentication_token.tt2.eml +++ b/templates/mail/send_authentication_token.tt2.txt @@ -1,8 +1,3 @@ -From: eduGAIN Access Check <[% from %]> -To: [% to %] -Subject: eduGAIN Access Check - [% lh.maketext("Test accounts request") | mime %] -Content-type: text/plain; charset=UTF-8; format=flowed - [% lh.maketext("This is an email challenge automatically sent to you by eduGAIN Access Check.") %] [% lh.maketext("Somebody with IP address [_1] has requested to create test accounts for eduGAIN Service Provider with entityID [_2].", sourceip, entityid) %] [% lh.maketext("The address [_1] is mentioned in the eduGAIN metadata as a contact for this Service Provider.", to) %] diff --git a/templates/renater/mail/send_authentication_token.tt2.eml b/templates/renater/mail/send_authentication_token.tt2.txt similarity index 83% rename from templates/renater/mail/send_authentication_token.tt2.eml rename to templates/renater/mail/send_authentication_token.tt2.txt index ca2a373..179001c 100644 --- a/templates/renater/mail/send_authentication_token.tt2.eml +++ b/templates/renater/mail/send_authentication_token.tt2.txt @@ -1,8 +1,3 @@ -From: eduGAIN Access Check <[% from %]> -To: [% to %] -Subject: eduGAIN Access Check - [% lh.maketext("Test accounts request") | mime %] -Content-type: text/plain; charset=UTF-8; format=flowed - [% lh.maketext("This is an email challenge automatically sent to you by eduGAIN Access Check.") %] [% lh.maketext("Somebody with IP address [_1] has requested to create test accounts for eduGAIN Service Provider with entityID [_2].", sourceip, entityid) %] [% lh.maketext("The address [_1] is mentioned in the eduGAIN metadata as a contact for this Service Provider.", to) %] -- GitLab