Skip to content

Commit

Permalink
MDL-61649 email: Send plain/html format emails based on user preference
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed Aug 7, 2019
1 parent 414eca8 commit c7f8845
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
20 changes: 5 additions & 15 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 7 additions & 5 deletions lib/tests/moodlelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<b>message text 2</b>';

// Close the default email sink.
$sink = $this->redirectEmails();
Expand Down Expand Up @@ -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()');
Expand Down Expand Up @@ -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));
}
}
12 changes: 6 additions & 6 deletions login/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand Down

0 comments on commit c7f8845

Please sign in to comment.