Support HTML emails.

This commit is contained in:
Chris 2015-07-17 15:22:23 +01:00
parent adb96a3773
commit 2e9154d647
2 changed files with 30 additions and 9 deletions

View File

@ -101,6 +101,8 @@ sub new {
# be extremely careful using this if a message has more than one recipient # be extremely careful using this if a message has more than one recipient
# and `unique_recip` is set, as this function may take a long time to return in # and `unique_recip` is set, as this function may take a long time to return in
# that situation. # that situation.
# - `format` (optional) Allow the format of the message to be specified. This defaults
# to 'text', but other formats may be suppported by transports.
# #
# @param args A hash, or a reference to a hash, of arguments defining the message. # @param args A hash, or a reference to a hash, of arguments defining the message.
# @return true on success, undef on error. # @return true on success, undef on error.
@ -116,7 +118,7 @@ sub queue_message {
$args -> {"send_after"} += $args -> {"delay"} if($args -> {"delay"}); $args -> {"send_after"} += $args -> {"delay"} if($args -> {"delay"});
# FUTURE: potentially support other formats here. See also: https://www.youtube.com/watch?v=JENdgiAPD6c however. # FUTURE: potentially support other formats here. See also: https://www.youtube.com/watch?v=JENdgiAPD6c however.
$args -> {"format"} = "text"; $args -> {"format"} = $args -> {"format"} || "text";
# Force required fields # Force required fields
return $self -> self_error("Email subject not specified") unless($args -> {"subject"}); return $self -> self_error("Email subject not specified") unless($args -> {"subject"});

View File

@ -26,6 +26,7 @@ use strict;
use base qw(Webperl::Message::Transport); use base qw(Webperl::Message::Transport);
use Encode; use Encode;
use Email::MIME; use Email::MIME;
use Email::MIME::CreateHTML;
use Email::Sender::Simple qw(sendmail); use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP; use Email::Sender::Transport::SMTP;
use Email::Sender::Transport::SMTP::Persistent; use Email::Sender::Transport::SMTP::Persistent;
@ -154,14 +155,32 @@ sub deliver {
} }
return 1 if(!$to); # Nothing to do if there are no recipients. return 1 if(!$to); # Nothing to do if there are no recipients.
my $email = Email::MIME -> create(header_str => [ From => $from, my $email;
To => $to,
Subject => $message -> {"subject"} if($message -> {"format"} eq "text") {
], $email = Email::MIME -> create(header_str => [ From => $from,
body_str => Encode::encode_utf8($message -> {"body"}), To => $to,
attributes => { charset => 'utf8', Subject => Encode::encode("iso-8859-1", decode_entities($message -> {"subject"})),
content_type => "text/plain", ],
encoding => 'base64' }); body_str => Encode::encode_utf8($message -> {"body"}),
attributes => { charset => 'utf8',
content_type => "text/plain",
encoding => 'base64' });
} elsif($message -> {"format"} eq "html") {
my $html_body = Encode::encode("iso-8859-1", $message -> {"body"});
my $text_body = Encode::encode("iso-8859-1", $self -> {"template"} -> html_to_markdown(Encode::encode_utf8($html), [], {"image" => "email/md_image.tem",
"images" => "email/md_images.tem",
"markdown" => "email/markdown.tem"}));
$email = Email::MIME -> create_html(header_str => [ From => $from,
To => $to,
Subject => Encode::encode("iso-8859-1", decode_entities($message -> {"subject"})),
],
embed => 0,
body => $html_body,
text_body => $text_body);
}
try { try {
sendmail($email, { from => $self -> {"env_sender"}, sendmail($email, { from => $self -> {"env_sender"},
transport => $self -> {"smtp"}}); transport => $self -> {"smtp"}});