diff --git a/lib/IdPAccountManager/Tools.pm b/lib/IdPAccountManager/Tools.pm
index dfc3fb1051310fe1d0853d4bb7e332935c757835..1e75739b0d7cfa3dda7b56110f83e24af1dc29b7 100644
--- a/lib/IdPAccountManager/Tools.pm
+++ b/lib/IdPAccountManager/Tools.pm
@@ -104,30 +104,6 @@ sub update_ssp_authsources {
 
 }
 
-## Send a mail notice
-## Default is to send email to the manager admins, unless other recipients are specified
-## mail_notice(IN)
-## IN is a HASH with expected entries :
-##   template : mail template file
-##   data : data used by the TT2 parser
-sub mail_notice {
-    my (%args) = @_;
-
-    die "unable to send mail, no sendmail executable found"
-        unless -x '/usr/sbin/sendmail';
-
-    open SENDMAIL,
-        "|/usr/sbin/sendmail -f $args{data}->{from} $args{data}->{to}";
-
-    my $tt2 = Template->new({
-        INCLUDE_PATH => $args{templates_dir}
-    });
-    $tt2->process($args{template}, $args{data}, \*SENDMAIL)
-        or die $tt2->error();
-
-    close SENDMAIL;
-}
-
 1;
 __END__
 
diff --git a/lib/IdPAccountManager/WebRequest.pm b/lib/IdPAccountManager/WebRequest.pm
index 04e02a9cba88dfef8de3ab141a85ce30f3879bd2..8b64b1dc805ba32531b224350824d83c7b323898 100755
--- a/lib/IdPAccountManager/WebRequest.pm
+++ b/lib/IdPAccountManager/WebRequest.pm
@@ -362,34 +362,43 @@ sub req_generate_token {
     $self->{out}->{sp_entityid}   = $self->{in}->{sp_entityid};
     $self->{out}->{subtitle}      = 'Generate an authentication token';
 
-    ## Send the challenge email with the token
-    eval {
-        IdPAccountManager::Tools::mail_notice(
-            template      => 'mail/send_authentication_token.tt2.eml',
-            templates_dir => $self->{configuration}->{templates_dir},
-            data          => {
-                env => {
-                    REMOTE_HOST => $ENV{REMOTE_HOST},
-                    REMOTE_ADDR => $ENV{REMOTE_ADDR},
-                },
-                conf => {
-                    app_name      => $self->{configuration}->{app_name},
-                    app_url       => $self->{configuration}->{app_url},
-                    support_email => $self->{configuration}->{support_email},
-                },
-                from                 => $self->{configuration}->{notice_from},
-                to                   => $self->{in}->{email_address},
-                sp_entityid          => $self->{in}->{sp_entityid},
-                authentication_token => $token->token(),
-            }
-        );
+    my $sender    = $self->{configuration}->{notice_from};
+    my $recipient = $self->{in}->{email_address};
+
+    unless (open(SENDMAIL, "|/usr/sbin/sendmail -f $sender $recipient")) {
+        push @{ $self->{out}->{errors} }, "mail_notification_error";
+        $self->{logger}->errorf("Mail notification error: %s", $ERRNO);
+        return undef;
+    }
+
+    my $tt2 = Template->new({
+        INCLUDE_PATH => $self->{configuration}->{templates_dir}
+    });
+    my $template = 'mail/send_authentication_token.tt2.eml';
+    my $data = {
+        env => {
+            REMOTE_HOST => $ENV{REMOTE_HOST},
+            REMOTE_ADDR => $ENV{REMOTE_ADDR},
+        },
+        conf => {
+            app_name      => $self->{configuration}->{app_name},
+            app_url       => $self->{configuration}->{app_url},
+            support_email => $self->{configuration}->{support_email},
+        },
+        from                 => $sender,
+        to                   => $recipient,
+        sp_entityid          => $self->{in}->{sp_entityid},
+        authentication_token => $token->token(),
     };
-    if ($EVAL_ERROR) {
+
+    unless ($tt2->process($template, $data, \*SENDMAIL)) {
         push @{ $self->{out}->{errors} }, "mail_notification_error";
         $self->{logger}->errorf("Mail notification error: %s", $EVAL_ERROR);
         return undef;
     }
 
+    close SENDMAIL;
+
     $self->{logger}->infof(
         "Token send to %s for sp_entityid=%s;token=%s",
         $self->{in}->{email_address},