Use Email::MIME instead of Email::Simple

Email::Simple cannot handle non-ASCII characters.

Fixes #191.
This commit is contained in:
Eelco Dolstra
2014-11-19 14:44:04 +01:00
parent 41bc918382
commit 8523130ebb
6 changed files with 90 additions and 95 deletions

View File

@ -4,10 +4,6 @@ use utf8;
use strict;
use Exporter;
use Readonly;
use Email::Simple;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP;
use Sys::Hostname::Long;
use Nix::Store;
use Hydra::Helper::Nix;
use feature qw/switch/;
@ -19,7 +15,6 @@ our @EXPORT = qw(
forceLogin requireUser requireProjectOwner requireAdmin requirePost isAdmin isProjectOwner
trim
getLatestFinishedEval
sendEmail
paramToList
backToReferer
$pathCompRE $relPathRE $relNameRE $projectNameRE $jobsetNameRE $jobNameRE $systemRE $userNameRE $inputNameRE
@ -193,27 +188,6 @@ sub getLatestFinishedEval {
}
sub sendEmail {
my ($c, $to, $subject, $body) = @_;
my $sender = $c->config->{'notification_sender'} ||
(($ENV{'USER'} || "hydra") . "@" . hostname_long);
my $email = Email::Simple->create(
header => [
To => $to,
From => "Hydra <$sender>",
Subject => $subject
],
body => $body
);
print STDERR "Sending email:\n", $email->as_string if $ENV{'HYDRA_MAIL_TEST'};
sendmail($email);
}
# Catalyst request parameters can be an array or a scalar or
# undefined, making them annoying to handle. So this utility function
# always returns a request parameter as a list.

View File

@ -0,0 +1,49 @@
package Hydra::Helper::Email;
use strict;
use Exporter 'import';
use Email::Sender::Simple qw(sendmail);
use Email::MIME;
use File::Slurp;
use Sys::Hostname::Long;
our @EXPORT = qw(sendEmail getBaseUrl);
sub sendEmail {
my ($config, $to, $subject, $body, $extraHeaders) = @_;
my $url = getBaseUrl($config);
my $sender = $config->{'notification_sender'} // (($ENV{'USER'} // "hydra") . "@" . $url);
my @headers = (
To => $to,
From => "Hydra Build Daemon <$sender>",
Subject => $subject,
'X-Hydra-Instance' => $url, @{$extraHeaders}
);
my $email = Email::MIME->create(
attributes => {
encoding => 'quoted-printable',
charset => 'UTF-8',
},
header_str => [ @headers ],
body_str => $body
);
print STDERR "sending email:\n", $email->as_string if $ENV{'HYDRA_MAIL_TEST'};
if (defined $ENV{'HYDRA_MAIL_SINK'}) {
# For testing, redirect all mail to a file.
write_file($ENV{'HYDRA_MAIL_SINK'}, { append => 1 }, $email->as_string . "\n");
} else {
sendmail($email, { from => $sender });
}
}
sub getBaseUrl {
my ($config) = @_;
return $config->{'base_uri'} // "http://" . hostname_long . ":3000";
}
1;