From aed2c13f5271956b3c0abc45061f49a078af4f3d Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 5 Jul 2010 11:56:33 -0700 Subject: [PATCH] Fix double body encoding when sending emails. Added tests. Fixes bug #11672. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Mail/Send.pm | 9 +++++---- t/Mail/Send.t | 42 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index ab95dc0da..d588ceb28 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -4,6 +4,7 @@ - fixed #11696: WebGUI 7.9.8 gotcha - fixed #11698: Trash dies on missing or bad workflow - fixed #11692: Dates not imported correctly into Thingy + - fixed #11672: UTF-Error in message body (from DataForm) 7.9.8 - fixed #11651: First Day of Week is a string... diff --git a/lib/WebGUI/Mail/Send.pm b/lib/WebGUI/Mail/Send.pm index 2563f9a6c..260ad31be 100644 --- a/lib/WebGUI/Mail/Send.pm +++ b/lib/WebGUI/Mail/Send.pm @@ -98,8 +98,9 @@ Macros in the footer will be evaluated. sub addFooter { my $self = shift; return if $self->{_footerAdded}; - my $text = "\n\n".$self->session->setting->get("mailFooter"); - WebGUI::Macro::process($self->session, \$text); + my $footer = "\n\n".$self->session->setting->get("mailFooter"); + WebGUI::Macro::process($self->session, \$footer); + my $text = encode("utf8", $footer); $self->{_footerAdded} = 1; my @parts = $self->getMimeEntity->parts(); ##No parts yet, add one with the footer content. @@ -117,7 +118,7 @@ sub addFooter { Charset => "UTF-8", Encoding => "quoted-printable", Type => 'text/plain', - Data => encode('utf8', $body_content), + Data => $body_content, ); shift @parts; unshift @parts, $new_part; @@ -130,7 +131,7 @@ sub addFooter { Charset => "UTF-8", Encoding => "quoted-printable", Type => 'text/html', - Data => encode('utf8', $body_content), + Data => $body_content, ); shift @parts; unshift @parts, $new_part; diff --git a/t/Mail/Send.t b/t/Mail/Send.t index b4fb8f0fe..126ff1b15 100644 --- a/t/Mail/Send.t +++ b/t/Mail/Send.t @@ -42,7 +42,7 @@ if ( $@ ) { diag( "Can't prepare mail server: $@" ) } #---------------------------------------------------------------------------- # Tests -plan tests => 34; # Increment this number for each test you create +plan tests => 38; # Increment this number for each test you create WebGUI::Test->addToCleanup(SQL => 'delete from mailQueue'); @@ -177,6 +177,46 @@ is($dbMail->getMimeEntity->head->get('Subject'), "=?UTF-8?Q?H=C3=84ufige=20Frage ok(defined $result && $result, 'by default, we make multipart messages'); } +{ + ##Disable the footer for easy processing + my $origFooter = $session->setting->get('mailFooter'); + $session->setting->set('mailFooter', ""); + my $textMail = WebGUI::Mail::Send->create( $session ); + $textMail->addText("H\x{00C4}ufige Fragen"); + $textMail->addFooter(); + is $textMail->getMimeEntity->parts(0)->bodyhandle->as_string, + encode('utf-8', "H\x{00C4}ufige Fragen\n\n"), + 'check that adding a footer does not double encode the body when it is text'; + my $htmlMail = WebGUI::Mail::Send->create( $session ); + $htmlMail->addHtml("__H\x{00C4}ufige Fragen__"); + $htmlMail->addFooter(); + my ($encoded_segment) = $htmlMail->getMimeEntity->parts(0)->bodyhandle->as_string =~ /__([^_]+)__/; + is $encoded_segment, + encode('utf-8', "H\x{00C4}ufige Fragen"), + '... similarly with an html body'; + $session->setting->set('mailFooter', $origFooter); +} + +{ + ##Set the footer to contain UTF-8 characters + my $origFooter = $session->setting->get('mailFooter'); + $session->setting->set('mailFooter', "Not beta: \x{00DF} "); + my $textMail = WebGUI::Mail::Send->create( $session ); + $textMail->addText(""); + $textMail->addFooter(); + is $textMail->getMimeEntity->parts(0)->bodyhandle->as_string, + encode('utf-8', "\n\nNot beta: \x{00DF} "), + 'check that footer is encoded as UTF-8 for a text body'; + my $htmlMail = WebGUI::Mail::Send->create( $session ); + $htmlMail->addHtml(""); + $htmlMail->addFooter(); + my ($encoded_segment) = $htmlMail->getMimeEntity->parts(0)->bodyhandle->as_string =~ /Not beta: (\S+)/; + is $encoded_segment, + encode('utf-8', "\x{00DF}"), + '... similarly with an html body'; + $session->setting->set('mailFooter', $origFooter); +} + my $smtpServerOk = 0; #----------------------------------------------------------------------------