From c7f88457cd974483fd0e793a7f27679638258444 Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Tue, 23 Jul 2019 17:12:45 +0800 Subject: [PATCH] MDL-61649 email: Send plain/html format emails based on user preference --- lib/moodlelib.php | 20 +++++--------------- lib/tests/moodlelib_test.php | 12 +++++++----- login/tests/lib_test.php | 12 ++++++------ 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 46496c57b6d92..5b2f3d1acff66 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -6127,24 +6127,16 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', if (!empty($user->mailformat) && $user->mailformat == 1) { // Only process html templates if the user preferences allow html email. - if ($messagehtml) { - // If html has been given then pass it through the template. - $context['body'] = $messagehtml; - $messagehtml = $renderer->render_from_template('core/email_html', $context); - - } else { + if (!$messagehtml) { // If no html has been given, BUT there is an html wrapping template then // auto convert the text to html and then wrap it. - $autohtml = trim(text_to_html($messagetext)); - $context['body'] = $autohtml; - $temphtml = $renderer->render_from_template('core/email_html', $context); - if ($autohtml != $temphtml) { - $messagehtml = $temphtml; - } + $messagehtml = trim(text_to_html($messagetext)); } + $context['body'] = $messagehtml; + $messagehtml = $renderer->render_from_template('core/email_html', $context); } - $context['body'] = $messagetext; + $context['body'] = html_to_text(nl2br($messagetext)); $mail->Subject = $renderer->render_from_template('core/email_subject', $context); $mail->FromName = $renderer->render_from_template('core/email_fromname', $context); $messagetext = $renderer->render_from_template('core/email_text', $context); @@ -6431,8 +6423,6 @@ function send_confirmation_email($user, $confirmationurl = null) { $message = get_string('emailconfirmation', '', $data); $messagehtml = text_to_html(get_string('emailconfirmation', '', $data), false, false, true); - $user->mailformat = 1; // Always send HTML version as well. - // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber. return email_to_user($user, $supportuser, $subject, $message, $messagehtml); } diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index a3083ffcca886..86a97c5bee772 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -3238,15 +3238,15 @@ public function test_email_to_user() { $this->resetAfterTest(); - $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1)); - $user2 = $this->getDataGenerator()->create_user(array('maildisplay' => 1)); + $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1, 'mailformat' => 0)); + $user2 = $this->getDataGenerator()->create_user(array('maildisplay' => 1, 'mailformat' => 1)); $user3 = $this->getDataGenerator()->create_user(array('maildisplay' => 0)); set_config('allowedemaildomains', "example.com\r\nmoodle.org"); $subject = 'subject'; $messagetext = 'message text'; $subject2 = 'subject 2'; - $messagetext2 = 'message text 2'; + $messagetext2 = 'message text 2'; // Close the default email sink. $sink = $this->redirectEmails(); @@ -3274,11 +3274,13 @@ public function test_email_to_user() { $this->assertSame($messagetext, trim($result[0]->body)); $this->assertSame($user1->email, $result[0]->to); $this->assertSame($user2->email, $result[0]->from); + $this->assertContains('Content-Type: text/plain', $result[0]->header); $this->assertSame($subject2, $result[1]->subject); - $this->assertSame($messagetext2, trim($result[1]->body)); + $this->assertContains($messagetext2, quoted_printable_decode($result[1]->body)); $this->assertSame($user2->email, $result[1]->to); $this->assertSame($user1->email, $result[1]->from); + $this->assertNotContains('Content-Type: text/plain', $result[1]->header); email_to_user($user1, $user2, $subject, $messagetext); $this->assertDebuggingCalled('Unit tests must not send real emails! Use $this->redirectEmails()'); @@ -4453,6 +4455,6 @@ public function test_send_password_change_info() { $result = $sink->get_messages(); $sink->close(); - $this->assertContains('passwords cannot be reset on this site', $result[0]->body); + $this->assertContains('passwords cannot be reset on this site', quoted_printable_decode($result[0]->body)); } } diff --git a/login/tests/lib_test.php b/login/tests/lib_test.php index 160d9def97755..a32941c6390c8 100644 --- a/login/tests/lib_test.php +++ b/login/tests/lib_test.php @@ -53,7 +53,7 @@ public function test_core_login_process_password_reset_one_time_without_username $this->assertSame($user->email, $email->to); $this->assertNotEmpty($email->header); $this->assertNotEmpty($email->body); - $this->assertRegExp('/A password reset was requested for your account/', $email->body); + $this->assertRegExp('/A password reset was requested for your account/', quoted_printable_decode($email->body)); $sink->clear(); } @@ -77,7 +77,7 @@ public function test_core_login_process_password_reset_two_consecutive_times_wit $this->assertSame($user->email, $email->to); $this->assertNotEmpty($email->header); $this->assertNotEmpty($email->body); - $this->assertRegExp('/A password reset was requested for your account/', $email->body); + $this->assertRegExp('/A password reset was requested for your account/', quoted_printable_decode($email->body)); $sink->clear(); } @@ -119,7 +119,7 @@ public function test_core_login_process_password_reset_one_time_with_username_pr $this->assertSame($user->email, $email->to); $this->assertNotEmpty($email->header); $this->assertNotEmpty($email->body); - $this->assertRegExp('/A password reset was requested for your account/', $email->body); + $this->assertRegExp('/A password reset was requested for your account/', quoted_printable_decode($email->body)); $sink->clear(); } @@ -152,7 +152,7 @@ public function test_core_login_process_password_reset_with_preexisting_expired_ $this->assertSame($user->email, $email->to); $this->assertNotEmpty($email->header); $this->assertNotEmpty($email->body); - $this->assertRegExp('/A password reset was requested for your account/', $email->body); + $this->assertRegExp('/A password reset was requested for your account/', quoted_printable_decode($email->body)); $sink->clear(); } @@ -169,7 +169,7 @@ public function test_core_login_process_password_reset_disabled_auth() { $this->assertSame($user->email, $email->to); $this->assertNotEmpty($email->header); $this->assertNotEmpty($email->body); - $this->assertRegExp('/Unfortunately your account on this site is disabled/', $email->body); + $this->assertRegExp('/Unfortunately your account on this site is disabled/', quoted_printable_decode($email->body)); $sink->clear(); } @@ -189,7 +189,7 @@ public function test_core_login_process_password_reset_auth_not_supporting_email $this->assertSame($user->email, $email->to); $this->assertNotEmpty($email->header); $this->assertNotEmpty($email->body); - $this->assertRegExp('/Unfortunately passwords cannot be reset on this site/', $email->body); + $this->assertRegExp('/Unfortunately passwords cannot be reset on this site/', quoted_printable_decode($email->body)); $sink->clear(); }