diff options
author | Ed Morley <emorley@mozilla.com> | 2015-03-19 08:16:30 +0100 |
---|---|---|
committer | Byron Jones <glob@mozilla.com> | 2015-03-19 08:16:30 +0100 |
commit | 6638a015487885a47bb0ba851865ef8b073d08fb (patch) | |
tree | 114070587bef8d7eb8dbc1f833258adbcd73dafd /Bugzilla/Mailer.pm | |
parent | 2f65ef51e9ba430783a2d9ef06e0aa0af041d510 (diff) | |
download | bugzilla-6638a015487885a47bb0ba851865ef8b073d08fb.tar.gz bugzilla-6638a015487885a47bb0ba851865ef8b073d08fb.tar.xz |
Bug 1105568: Add support for HTML flagmail
r=glob,a=glob
Diffstat (limited to 'Bugzilla/Mailer.pm')
-rw-r--r-- | Bugzilla/Mailer.pm | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/Bugzilla/Mailer.pm b/Bugzilla/Mailer.pm index ef75d0cf8..4f13f77d2 100644 --- a/Bugzilla/Mailer.pm +++ b/Bugzilla/Mailer.pm @@ -12,11 +12,12 @@ use strict; use warnings; use parent qw(Exporter); -@Bugzilla::Mailer::EXPORT = qw(MessageToMTA build_thread_marker); +@Bugzilla::Mailer::EXPORT = qw(MessageToMTA build_thread_marker generate_email); use Bugzilla::Constants; use Bugzilla::Error; use Bugzilla::Hook; +use Bugzilla::User; use Bugzilla::Util; use Date::Format qw(time2str); @@ -28,6 +29,63 @@ use Email::Sender::Simple qw(sendmail); use Email::Sender::Transport::SMTP::Persistent; use Bugzilla::Sender::Transport::Sendmail; +sub generate_email { + my ($vars, $templates) = @_; + my ($lang, $email_format, $msg_text, $msg_html, $msg_header); + + if ($vars->{to_user}) { + $lang = $vars->{to_user}->setting('lang'); + $email_format = $vars->{to_user}->setting('email_format'); + } else { + # If there are users in the CC list who don't have an account, + # use the default language for email notifications. + $lang = Bugzilla::User->new()->setting('lang'); + # However we cannot fall back to the default email_format, since + # it may be HTML, and many of the includes used in the HTML + # template require a valid user object. Instead we fall back to + # the plaintext template. + $email_format = 'text_only'; + } + + my $template = Bugzilla->template_inner($lang); + + $template->process($templates->{header}, $vars, \$msg_header) + || ThrowTemplateError($template->error()); + $template->process($templates->{text}, $vars, \$msg_text) + || ThrowTemplateError($template->error()); + + my @parts = ( + Email::MIME->create( + attributes => { + content_type => "text/plain", + }, + body => $msg_text, + ) + ); + if ($templates->{html} && $email_format eq 'html') { + $template->process($templates->{html}, $vars, \$msg_html) + || ThrowTemplateError($template->error()); + push @parts, Email::MIME->create( + attributes => { + content_type => "text/html", + }, + body => $msg_html, + ); + } + + # TT trims the trailing newline, and threadingmarker may be ignored. + my $email = new Email::MIME("$msg_header\n"); + if (scalar(@parts) == 1) { + $email->content_type_set($parts[0]->content_type); + } else { + $email->content_type_set('multipart/alternative'); + # Some mail clients need same encoding for each part, even empty ones. + $email->charset_set('UTF-8') if Bugzilla->params->{'utf8'}; + } + $email->parts_set(\@parts); + return $email; +} + sub MessageToMTA { my ($msg, $send_now) = (@_); my $method = Bugzilla->params->{'mail_delivery_method'}; @@ -285,6 +343,10 @@ Bugzilla::Mailer - Provides methods for sending email =over +=item C<generate_email> + +Generates a multi-part email message, using the supplied list of templates. + =item C<MessageToMTA> Sends the passed message to the mail transfer agent. |