From 6638a015487885a47bb0ba851865ef8b073d08fb Mon Sep 17 00:00:00 2001 From: Ed Morley Date: Thu, 19 Mar 2015 15:16:30 +0800 Subject: Bug 1105568: Add support for HTML flagmail r=glob,a=glob --- Bugzilla/BugMail.pm | 45 +++++-------------------------------- Bugzilla/Flag.pm | 27 +++++++++------------- Bugzilla/Mailer.pm | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++- Bugzilla/Product.pm | 1 - 4 files changed, 79 insertions(+), 58 deletions(-) (limited to 'Bugzilla') diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index df39bd108..f655c4ae6 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -437,45 +437,12 @@ sub _flatten_object { sub _generate_bugmail { my ($vars) = @_; - my $user = $vars->{to_user}; - my $template = Bugzilla->template_inner($user->setting('lang')); - my ($msg_text, $msg_html, $msg_header); - - $template->process("email/bugmail-header.txt.tmpl", $vars, \$msg_header) - || ThrowTemplateError($template->error()); - $template->process("email/bugmail.txt.tmpl", $vars, \$msg_text) - || ThrowTemplateError($template->error()); - - my @parts = ( - Email::MIME->create( - attributes => { - content_type => "text/plain", - }, - body => $msg_text, - ) - ); - if ($user->setting('email_format') eq 'html') { - $template->process("email/bugmail.html.tmpl", $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'); - } - $email->parts_set(\@parts); - return $email; + my $templates = { + header => "email/bugmail-header.txt.tmpl", + text => "email/bugmail.txt.tmpl", + html => "email/bugmail.html.tmpl", + }; + return generate_email($vars, $templates); } sub _get_diffs { diff --git a/Bugzilla/Flag.pm b/Bugzilla/Flag.pm index 50474b885..6d056e7e6 100644 --- a/Bugzilla/Flag.pm +++ b/Bugzilla/Flag.pm @@ -1116,13 +1116,6 @@ sub notify { if ($addressee && $addressee->email_enabled) { $recipients{$addressee->email} = $addressee; } - # Process and send notification for each recipient. - # If there are users in the CC list who don't have an account, - # use the default language for email notifications. - my $default_lang; - if (grep { !$_ } values %recipients) { - $default_lang = Bugzilla::User->new()->setting('lang'); - } # Get comments on the bug my $all_comments = $bug->comments({ after => $bug->lastdiffed }); @@ -1132,18 +1125,20 @@ sub notify { my $public_comments = [ grep { !$_->is_private } @$all_comments ]; foreach my $to (keys %recipients) { + my $user = $recipients{$to}; # Add threadingmarker to allow flag notification emails to be the # threaded similar to normal bug change emails. - my $thread_user_id = $recipients{$to} ? $recipients{$to}->id : 0; + my $thread_user_id = $user ? $user->id : 0; # We only want to show private comments to users in the is_insider group - my $comments = $recipients{$to} && $recipients{$to}->is_insider + my $comments = $user && $user->is_insider ? $all_comments : $public_comments; my $vars = { flag => $flag, old_flag => $old_flag, to => $to, + to_user => $user, date => $timestamp, bug => $bug, attachment => $attachment, @@ -1151,15 +1146,13 @@ sub notify { new_comments => $comments, }; - my $lang = $recipients{$to} ? - $recipients{$to}->setting('lang') : $default_lang; - - my $template = Bugzilla->template_inner($lang); - my $message; - $template->process("email/flagmail.txt.tmpl", $vars, \$message) - || ThrowTemplateError($template->error()); + my $templates = { + header => "email/flagmail-header.txt.tmpl", + text => "email/flagmail.txt.tmpl", + html => "email/flagmail.html.tmpl", + }; - MessageToMTA($message); + MessageToMTA(generate_email($vars, $templates)); } } 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 + +Generates a multi-part email message, using the supplied list of templates. + =item C Sends the passed message to the mail transfer agent. diff --git a/Bugzilla/Product.pm b/Bugzilla/Product.pm index 30ebc7c6c..0c0cb458d 100644 --- a/Bugzilla/Product.pm +++ b/Bugzilla/Product.pm @@ -22,7 +22,6 @@ use Bugzilla::Milestone; use Bugzilla::Field; use Bugzilla::Status; use Bugzilla::Install::Requirements; -use Bugzilla::Mailer; use Bugzilla::Series; use Bugzilla::Hook; use Bugzilla::FlagType; -- cgit v1.2.3-24-g4f1b