From bc6d37194251affda1603d842df2ab04fc37f3a5 Mon Sep 17 00:00:00 2001 From: Cameron Date: Thu, 15 Dec 2022 22:11:05 -0800 Subject: [PATCH] Closes #4924 - sendEmail() updated. --- e107_handlers/mail.php | 21 ++++++++++---- e107_tests/tests/unit/e107EmailTest.php | 37 +++++++++++++++++++++---- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/e107_handlers/mail.php b/e107_handlers/mail.php index 9df0ffc54..590b2f610 100644 --- a/e107_handlers/mail.php +++ b/e107_handlers/mail.php @@ -988,8 +988,17 @@ class e107Email extends PHPMailer $identifier = deftrue('MAIL_IDENTIFIER', 'X-e107-id'); if (isset($eml['SMTPDebug'])) { $this->SMTPDebug = $eml['SMTPDebug']; } // 'false' is a valid value! - if (!empty($eml['sender_email'])) { $this->From = $eml['sender_email']; } - if (!empty($eml['sender_name'])) { $this->FromName = $eml['sender_name']; } + if (!empty($eml['sender_email']) && !empty($eml['sender_email'])) + { + $this->setFrom($eml['sender_email'], $eml['sender_name']); + } + else + { + if (!empty($eml['sender_email'])) { $this->From = $eml['sender_email']; } + if (!empty($eml['sender_name'])) { $this->FromName = $eml['sender_name']; } + } + + if (!empty($eml['replyto'])) { $this->AddAddressList('replyto',$eml['replyto'],vartrue($eml['replytonames'],'')); } if (isset($eml['html'])) { $this->allow_html = $eml['html']; } // 'false' is a valid value! if (isset($eml['html_header'])) { $this->add_HTML_header = $eml['html_header']; } // 'false' is a valid value! @@ -1354,7 +1363,8 @@ class e107Email extends PHPMailer $this->isHTML(true); - $this->Body = $message; + // $this->Body = $message; + $this->Body = static::normalizeBreaks($message); //print_a($message); $textMsg = str_replace("\n", "", $message); $textMsg = str_replace('', "\t", $textMsg); @@ -1362,7 +1372,7 @@ class e107Email extends PHPMailer $textMsg = str_replace(array('
', '
'), "\n", $textMsg); // Modified to make sure newlines carried through $textMsg = preg_replace('#^.*?#', '', $textMsg); // Knock off everything up to and including the body statement (if present) $textMsg = preg_replace('#.*$#', '', $textMsg); // Knock off everything after and including the (if present) - $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$textMsg))); + // $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$textMsg))); if($this->debug) { @@ -1372,8 +1382,7 @@ class e107Email extends PHPMailer if(!empty($textMsg)) // Always set it, even if AltBody is empty. { - $this->AltBody = html_entity_decode($textMsg); - + $this->AltBody = static::normalizeBreaks($this->html2text($textMsg, $advanced)); } if(empty($this->AltBody)) diff --git a/e107_tests/tests/unit/e107EmailTest.php b/e107_tests/tests/unit/e107EmailTest.php index 58bacd680..293343193 100644 --- a/e107_tests/tests/unit/e107EmailTest.php +++ b/e107_tests/tests/unit/e107EmailTest.php @@ -132,10 +132,6 @@ } - public function testPreview() - { - - } public function testAddInlineImages() { @@ -164,7 +160,7 @@ Admin
$this->eml->MsgHTML($html); $result = json_encode($this->eml->AltBody); - $expected = '"Hi Joe\\nCheck out https:\\/\\/e107.org\\n\\nThanks,\\nAdmin\\n\\nWebsite:\\thttps:\\/\\/e107.org\\t\\nGithub:\\thttps:\\/\\/github.com\\/e107inc\\/"'; + $expected = '"Hi Joe\r\nCheck out https:\/\/e107.org\r\n\r\nThanks,\r\nAdmin\r\n\r\nWebsite:\thttps:\/\/e107.org\t\r\nGithub:\thttps:\/\/github.com\/e107inc\/"'; $this->assertSame($expected, $result); } @@ -211,5 +207,36 @@ Admin
*/ + function testSentMimeMessage() + { + $eml = array( + 'subject' => "[PREVIEW]", + 'sender_email' => "noreply@test.com", + 'sender_name' => "Test Person", + 'replyto' => "someone@else.com", + 'html' => true, + 'priority' => 1, + // 'template' => 'default', + 'body' => "Hi,
This is the body text", + 'cc' => '', + 'shortcodes' => [ + 'NAME' => "TestName", + 'DATE' => 'Jan 1st, 2020' + ], + ); + + $this->eml->arraySet($eml); + $this->eml->AddAddressList('to','recipient@example.com',"Example Recipient"); + $this->eml->preSend(); + + $result = $this->eml->getSentMIMEMessage(); + + $this->assertStringContainsString('Content-Type: text/plain;', $result); + $this->assertStringContainsString('This is the body text', $result); + + $this->assertStringContainsString('Content-Type: text/html;', $result); + $this->assertStringContainsString('Hi,
This is the body text', $result); + + } }