diff --git a/extensions/swiftmailer/yii/swiftmailer/Message.php b/extensions/swiftmailer/yii/swiftmailer/Message.php index 808fbf8..42d25e7 100644 --- a/extensions/swiftmailer/yii/swiftmailer/Message.php +++ b/extensions/swiftmailer/yii/swiftmailer/Message.php @@ -125,7 +125,7 @@ class Message extends BaseMessage */ public function setText($text) { - $this->getSwiftMessage()->setBody($text, 'text/plain'); + $this->setBody($text, 'text/plain'); } /** @@ -133,23 +133,51 @@ class Message extends BaseMessage */ public function setHtml($html) { - $this->getSwiftMessage()->setBody($html, 'text/html'); + $this->setBody($html, 'text/html'); } /** - * @inheritdoc + * Sets the message body. + * If body is already set and its content type matches given one, it will + * be overridden, if content type miss match the multipart message will be composed. + * @param string $body body content. + * @param string $contentType body content type. */ - public function addText($text) + protected function setBody($body, $contentType) { - $this->getSwiftMessage()->addPart($text, 'text/plain'); - } - - /** - * @inheritdoc - */ - public function addHtml($html) - { - $this->getSwiftMessage()->addPart($html, 'text/html'); + $message = $this->getSwiftMessage(); + $oldBody = $message->getBody(); + if (empty($oldBody)) { + $parts = $message->getChildren(); + $partFound = false; + foreach ($parts as $key => $part) { + if (!($part instanceof \Swift_Mime_Attachment)) { + /* @var $part \Swift_Mime_MimePart */ + if ($part->getContentType() == $contentType) { + unset($parts[$key]); + $partFound = true; + break; + } + } + } + if ($partFound) { + reset($parts); + $message->setChildren($parts); + $message->addPart($body, $contentType); + } else { + $message->setBody($body, $contentType); + } + } else { + $oldContentType = $message->getContentType(); + if ($oldContentType == $contentType) { + $message->setBody($body, $contentType); + } else { + $message->setBody(null); + $message->setContentType(null); + $message->addPart($oldBody, $oldContentType); + $message->addPart($body, $contentType); + } + } } /** diff --git a/framework/yii/mail/MessageInterface.php b/framework/yii/mail/MessageInterface.php index 0f3e5a4..e7de37b 100644 --- a/framework/yii/mail/MessageInterface.php +++ b/framework/yii/mail/MessageInterface.php @@ -70,18 +70,6 @@ interface MessageInterface public function setHtml($html); /** - * Add message plain text content part. - * @param string $text message plain text content. - */ - public function addText($text); - - /** - * Add message HTML content part. - * @param string $html message HTML content. - */ - public function addHtml($html); - - /** * Attach specified content as file for the email message. * @param string $content attachment file content. * @param array $options options for embed file. Valid options are: diff --git a/tests/unit/extensions/swiftmailer/MessageTest.php b/tests/unit/extensions/swiftmailer/MessageTest.php index 0a02b6d..2e45acb 100644 --- a/tests/unit/extensions/swiftmailer/MessageTest.php +++ b/tests/unit/extensions/swiftmailer/MessageTest.php @@ -182,8 +182,8 @@ class MessageTest extends VendorTestCase $message->setTo($this->testEmailReceiver); $message->setFrom('someuser@somedomain.com'); $message->setSubject('Yii Swift Alternative Body Test'); - $message->addHtml('Yii Swift test HTML body'); - $message->addText('Yii Swift test plain text body'); + $message->setHtml('Yii Swift test HTML body'); + $message->setText('Yii Swift test plain text body'); $this->assertTrue($message->send()); } } \ No newline at end of file diff --git a/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php index f135f51..ab70563 100644 --- a/tests/unit/framework/mail/BaseMailerTest.php +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -182,10 +182,6 @@ class Message extends BaseMessage public function setHtml($html) {} - public function addText($text) {} - - public function addHtml($html) {} - public function attachContent($content, array $options = []) {} public function attachFile($fileName, array $options = []) {}