Skip to content

Commit 09f1940

Browse files
justdaveLpSolit
andauthored
Bug 2059943: fix generate_email() crash with text mails (#252)
Co-authored-by: Frédéric Buclin <LpSolit@gmail.com> r=justdave
1 parent 5849118 commit 09f1940

2 files changed

Lines changed: 24 additions & 58 deletions

File tree

‎Bugzilla/BugMail.pm‎

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,14 @@ sub sendMail {
379379
if (Bugzilla->params->{'use_mailer_queue'}) {
380380
enqueue($vars);
381381
} else {
382-
MessageToMTA(_generate_bugmail($vars));
382+
MessageToMTA(generate_email(
383+
$vars,
384+
{
385+
header => 'email/bugmail-header.txt.tmpl',
386+
text => 'email/bugmail.txt.tmpl',
387+
html => 'email/bugmail.html.tmpl'
388+
}
389+
));
383390
}
384391

385392
return 1;
@@ -419,7 +426,14 @@ sub dequeue {
419426
}
420427
}
421428
# generate bugmail and send
422-
MessageToMTA(_generate_bugmail($vars), 1);
429+
MessageToMTA(generate_email(
430+
$vars,
431+
{
432+
header => 'email/bugmail-header.txt.tmpl',
433+
text => 'email/bugmail.txt.tmpl',
434+
html => 'email/bugmail.html.tmpl'
435+
}
436+
), 1);
423437
}
424438

425439
sub _flatten_object {
@@ -432,56 +446,6 @@ sub _flatten_object {
432446
return $cache->{$key} ||= $object->flatten_to_hash;
433447
}
434448

435-
sub _generate_bugmail {
436-
my ($vars) = @_;
437-
my $user = $vars->{to_user};
438-
my $template = Bugzilla->template_inner($user->setting('lang'));
439-
my ($msg_text, $msg_html, $msg_header);
440-
state $use_utf8 = Bugzilla->params->{'utf8'};
441-
442-
$template->process("email/bugmail-header.txt.tmpl", $vars, \$msg_header)
443-
|| ThrowTemplateError($template->error());
444-
$template->process("email/bugmail.txt.tmpl", $vars, \$msg_text)
445-
|| ThrowTemplateError($template->error());
446-
447-
my @parts = (
448-
Bugzilla::MIME->create(
449-
attributes => {
450-
content_type => 'text/plain',
451-
charset => $use_utf8 ? 'UTF-8' : 'iso-8859-1',
452-
encoding => 'quoted-printable',
453-
},
454-
body_str => $msg_text,
455-
encode_check => Encode::FB_DEFAULT
456-
)
457-
);
458-
if ($user->setting('email_format') eq 'html') {
459-
$template->process("email/bugmail.html.tmpl", $vars, \$msg_html)
460-
|| ThrowTemplateError($template->error());
461-
push @parts, Bugzilla::MIME->create(
462-
attributes => {
463-
content_type => 'text/html',
464-
charset => $use_utf8 ? 'UTF-8' : 'iso-8859-1',
465-
encoding => 'quoted-printable',
466-
},
467-
body_str => $msg_html,
468-
encode_check => Encode::FB_DEFAULT
469-
);
470-
}
471-
472-
my $email = Bugzilla::MIME->new($msg_header);
473-
474-
# If there's only one part, we don't need to set the overall content type
475-
# because Email::MIME will automatically take it from that part (bug 1657496)
476-
if (scalar(@parts) > 1) {
477-
$email->content_type_set('multipart/alternative');
478-
# Some mail clients need same encoding for each part, even empty ones.
479-
$email->charset_set('UTF-8') if $use_utf8;
480-
}
481-
$email->parts_set(\@parts);
482-
return $email;
483-
}
484-
485449
sub _get_diffs {
486450
my ($bug, $end, $user_cache) = @_;
487451
my $dbh = Bugzilla->dbh;

‎Bugzilla/Mailer.pm‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ use Bugzilla::Sender::Transport::Sendmail;
3030

3131
sub generate_email {
3232
my ($vars, $templates) = @_;
33+
my $user = $vars->{to_user};
3334
my ($lang, $email_format, $msg_text, $msg_html, $msg_header);
3435
state $use_utf8 = Bugzilla->params->{'utf8'};
3536

36-
if ($vars->{to_user}) {
37-
$lang = $vars->{to_user}->setting('lang');
38-
$email_format = $vars->{to_user}->setting('email_format');
37+
if ($user) {
38+
$lang = $user->setting('lang');
39+
$email_format = $user->setting('email_format');
3940
} else {
4041
# If there are users in the CC list who don't have an account,
4142
# use the default language for email notifications.
@@ -80,9 +81,10 @@ sub generate_email {
8081
}
8182

8283
my $email = Bugzilla::MIME->new($msg_header);
83-
if (scalar(@parts) == 1) {
84-
$email->content_type_set($parts[0]->content_type);
85-
} else {
84+
85+
# If there's only one part, we don't need to set the overall content type
86+
# because Email::MIME will automatically take it from that part (bug 1657496)
87+
if (scalar(@parts) > 1) {
8688
$email->content_type_set('multipart/alternative');
8789
# Some mail clients need same encoding for each part, even empty ones.
8890
$email->charset_set('UTF-8') if $use_utf8;

0 commit comments

Comments
 (0)