Commit 1d0891f0 by Paul Klimov

'yii\mail\MessageInterface' methods 'addText()' and 'addHtml()' removed.

parent 774c4db8
......@@ -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);
}
}
}
/**
......
......@@ -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:
......
......@@ -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('<b>Yii Swift</b> test HTML body');
$message->addText('Yii Swift test plain text body');
$message->setHtml('<b>Yii Swift</b> test HTML body');
$message->setText('Yii Swift test plain text body');
$this->assertTrue($message->send());
}
}
\ No newline at end of file
......@@ -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 = []) {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment