Commit 774c4db8 by Paul Klimov

'yii\mail\MessageInterface' file related methods interface adjusted to use…

'yii\mail\MessageInterface' file related methods interface adjusted to use options, embed file methods added.
parent 35429fbd
No related merge requests found
...@@ -155,18 +155,66 @@ class Message extends BaseMessage ...@@ -155,18 +155,66 @@ class Message extends BaseMessage
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function attachContentAsFile($content, $fileName, $contentType = 'application/octet-stream') public function attachFile($fileName, array $options = [])
{ {
if (empty($contentType)) { $attachment = \Swift_Attachment::fromPath($fileName);
$contentType = 'application/octet-stream'; if (!empty($options['fileName'])) {
$attachment->setFilename($options['fileName']);
}
if (!empty($options['contentType'])) {
$attachment->setContentType($options['contentType']);
} }
$attachment = \Swift_Attachment::newInstance($content, $fileName, $contentType);
$this->getSwiftMessage()->attach($attachment); $this->getSwiftMessage()->attach($attachment);
} }
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function attachContent($content, array $options = [])
{
$attachment = \Swift_Attachment::newInstance($content);
if (!empty($options['fileName'])) {
$attachment->setFilename($options['fileName']);
}
if (!empty($options['contentType'])) {
$attachment->setContentType($options['contentType']);
}
$this->getSwiftMessage()->attach($attachment);
}
/**
* @inheritdoc
*/
public function embedFile($fileName, array $options = [])
{
$embedFile = \Swift_EmbeddedFile::fromPath($fileName);
if (!empty($options['fileName'])) {
$embedFile->setFilename($options['fileName']);
}
if (!empty($options['contentType'])) {
$embedFile->setContentType($options['contentType']);
}
return $this->getSwiftMessage()->embed($embedFile);
}
/**
* @inheritdoc
*/
public function embedContent($content, array $options = [])
{
$embedFile = \Swift_EmbeddedFile::newInstance($content);
if (!empty($options['fileName'])) {
$embedFile->setFilename($options['fileName']);
}
if (!empty($options['contentType'])) {
$embedFile->setContentType($options['contentType']);
}
return $this->getSwiftMessage()->embed($embedFile);
}
/**
* @inheritdoc
*/
public function __toString() public function __toString()
{ {
return $this->getSwiftMessage()->toString(); return $this->getSwiftMessage()->toString();
......
...@@ -53,24 +53,6 @@ abstract class BaseMessage extends Object implements MessageInterface ...@@ -53,24 +53,6 @@ abstract class BaseMessage extends Object implements MessageInterface
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function attachFile($fileName, $contentType = null, $attachFileName = null)
{
if (!file_exists($fileName)) {
throw new InvalidParamException('Unable to attach file "' . $fileName . '": file does not exists!');
}
if (empty($contentType)) {
$contentType = FileHelper::getMimeType($fileName);
}
if (empty($attachFileName)) {
$attachFileName = basename($fileName);
}
$content = file_get_contents($fileName);
$this->attachContentAsFile($content, $attachFileName, $contentType);
}
/**
* @inheritdoc
*/
public function render($view, $params = []) public function render($view, $params = [])
{ {
return $this->getMailer()->render($view, $params); return $this->getMailer()->render($view, $params);
......
...@@ -84,18 +84,42 @@ interface MessageInterface ...@@ -84,18 +84,42 @@ interface MessageInterface
/** /**
* Attach specified content as file for the email message. * Attach specified content as file for the email message.
* @param string $content attachment file content. * @param string $content attachment file content.
* @param string $fileName attachment file name. * @param array $options options for embed file. Valid options are:
* @param string $contentType MIME type of the attachment file, by default 'application/octet-stream' will be used. * - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
*/ */
public function attachContentAsFile($content, $fileName, $contentType = 'application/octet-stream'); public function attachContent($content, array $options = []);
/** /**
* Attaches existing file to the email message. * Attaches existing file to the email message.
* @param string $fileName full file name * @param string $fileName full file name
* @param string $contentType MIME type of the attachment file, if empty it will be suggested automatically. * @param array $options options for embed file. Valid options are:
* @param string $attachFileName name, which should be used for attachment, if empty file base name will be used. * - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
*/ */
public function attachFile($fileName, $contentType = null, $attachFileName = null); public function attachFile($fileName, array $options = []);
/**
* Attach a file and return it's CID source.
* This method should be used when embedding images or other data in a message.
* @param string $fileName file name.
* @param array $options options for embed file. Valid options are:
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
* @return string attachment CID.
*/
public function embedFile($fileName, array $options = []);
/**
* Attach a content as file and return it's CID source.
* This method should be used when embedding images or other data in a message.
* @param string $content attachment file content.
* @param array $options options for embed file. Valid options are:
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
* @return string attachment CID.
*/
public function embedContent($content, array $options = []);
/** /**
* Sends this email message. * Sends this email message.
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace yiiunit\extensions\swiftmailer; namespace yiiunit\extensions\swiftmailer;
use Yii; use Yii;
use yii\helpers\FileHelper;
use yii\swiftmailer\Mailer; use yii\swiftmailer\Mailer;
use yii\swiftmailer\Message; use yii\swiftmailer\Message;
use yiiunit\VendorTestCase; use yiiunit\VendorTestCase;
...@@ -26,6 +27,26 @@ class MessageTest extends VendorTestCase ...@@ -26,6 +27,26 @@ class MessageTest extends VendorTestCase
'mail' => $this->createTestEmailComponent() 'mail' => $this->createTestEmailComponent()
] ]
]); ]);
$filePath = $this->getTestFilePath();
if (!file_exists($filePath)) {
FileHelper::createDirectory($filePath);
}
}
public function tearDown()
{
$filePath = $this->getTestFilePath();
if (file_exists($filePath)) {
FileHelper::removeDirectory($filePath);
}
}
/**
* @return string test file path.
*/
protected function getTestFilePath()
{
return Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . basename(get_class($this)) . '_' . getmypid();
} }
/** /**
...@@ -45,6 +66,26 @@ class MessageTest extends VendorTestCase ...@@ -45,6 +66,26 @@ class MessageTest extends VendorTestCase
return Yii::$app->getComponent('mail')->createMessage(); return Yii::$app->getComponent('mail')->createMessage();
} }
/**
* Creates image file with given text.
* @param string $fileName file name.
* @param string $text text to be applied on image.
* @return string image file full name.
*/
protected function createImageFile($fileName = 'test.jpg', $text = 'Test Image')
{
if (!function_exists('imagecreatetruecolor')) {
$this->markTestSkipped('GD lib required.');
}
$fileFullName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $fileName;
$image = imagecreatetruecolor(120, 20);
$textColor = imagecolorallocate($image, 233, 14, 91);
imagestring($image, 1, 5, 5, $text, $textColor);
imagejpeg($image, $fileFullName);
imagedestroy($image);
return $fileFullName;
}
// Tests : // Tests :
public function testGetSwiftMessage() public function testGetSwiftMessage()
...@@ -83,14 +124,52 @@ class MessageTest extends VendorTestCase ...@@ -83,14 +124,52 @@ class MessageTest extends VendorTestCase
/** /**
* @depends testSend * @depends testSend
*/ */
public function testCreateAttachment() public function testAttachContent()
{ {
$message = $this->createTestMessage(); $message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver); $message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Create Attachment Test'); $message->setSubject('Yii Swift Create Attachment Test');
$message->setText('Yii Swift Create Attachment Test body'); $message->setText('Yii Swift Create Attachment Test body');
$message->attachContentAsFile('Test attachment content', 'test.txt'); $message->attachContent('Test attachment content', ['fileName' => 'test.txt']);
$this->assertTrue($message->send());
}
/**
* @depends testSend
*/
public function testEmbedFile()
{
$fileName = $this->createImageFile('embed_file.jpg', 'Embed Image File');
$message = $this->createTestMessage();
$cid = $message->embedFile($fileName);
$message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Embed File Test');
$message->setHtml('Embed image: <img src="' . $cid. '" alt="pic">');
$this->assertTrue($message->send());
}
/**
* @depends testSend
*/
public function testEmbedContent()
{
$fileName = $this->createImageFile('embed_file.jpg', 'Embed Image File');
$message = $this->createTestMessage();
$cid = $message->embedContent(file_get_contents($fileName), ['contentType' => 'image/jpeg']);
$message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Embed File Test');
$message->setHtml('Embed image: <img src="' . $cid. '" alt="pic">');
$this->assertTrue($message->send()); $this->assertTrue($message->send());
} }
......
...@@ -186,7 +186,13 @@ class Message extends BaseMessage ...@@ -186,7 +186,13 @@ class Message extends BaseMessage
public function addHtml($html) {} public function addHtml($html) {}
public function attachContentAsFile($content, $fileName, $contentType = 'application/octet-stream') {} public function attachContent($content, array $options = []) {}
public function attachFile($fileName, array $options = []) {}
public function embedFile($fileName, array $options = []) {}
public function embedContent($content, array $options = []) {}
public function __toString() public function __toString()
{ {
......
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