mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
MDL-61649 email: Send plain/html format emails based on user preference
This commit is contained in:
parent
414eca8923
commit
c7f88457cd
@ -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);
|
||||
}
|
||||
|
@ -3238,15 +3238,15 @@ class core_moodlelib_testcase extends advanced_testcase {
|
||||
|
||||
$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();
|
||||
@ -3274,11 +3274,13 @@ class core_moodlelib_testcase extends advanced_testcase {
|
||||
$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 @@ class core_moodlelib_testcase extends advanced_testcase {
|
||||
$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));
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ class core_login_lib_testcase extends advanced_testcase {
|
||||
$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 @@ class core_login_lib_testcase extends advanced_testcase {
|
||||
$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 @@ class core_login_lib_testcase extends advanced_testcase {
|
||||
$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 @@ class core_login_lib_testcase extends advanced_testcase {
|
||||
$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 @@ class core_login_lib_testcase extends advanced_testcase {
|
||||
$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 @@ class core_login_lib_testcase extends advanced_testcase {
|
||||
$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();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user