From 774c4db860d6b2a4327dbc8502ff78892b248d19 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Tue, 29 Oct 2013 14:06:16 +0200 Subject: [PATCH] 'yii\mail\MessageInterface' file related methods interface adjusted to use options, embed file methods added. --- extensions/swiftmailer/yii/swiftmailer/Message.php | 56 +++++++++++++-- framework/yii/mail/BaseMessage.php | 18 ----- framework/yii/mail/MessageInterface.php | 36 ++++++++-- tests/unit/extensions/swiftmailer/MessageTest.php | 83 +++++++++++++++++++++- tests/unit/framework/mail/BaseMailerTest.php | 8 ++- 5 files changed, 170 insertions(+), 31 deletions(-) diff --git a/extensions/swiftmailer/yii/swiftmailer/Message.php b/extensions/swiftmailer/yii/swiftmailer/Message.php index 7fc908b..808fbf8 100644 --- a/extensions/swiftmailer/yii/swiftmailer/Message.php +++ b/extensions/swiftmailer/yii/swiftmailer/Message.php @@ -155,18 +155,66 @@ class Message extends BaseMessage /** * @inheritdoc */ - public function attachContentAsFile($content, $fileName, $contentType = 'application/octet-stream') + public function attachFile($fileName, array $options = []) { - if (empty($contentType)) { - $contentType = 'application/octet-stream'; + $attachment = \Swift_Attachment::fromPath($fileName); + 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); } /** * @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() { return $this->getSwiftMessage()->toString(); diff --git a/framework/yii/mail/BaseMessage.php b/framework/yii/mail/BaseMessage.php index b573b16..8c4daa8 100644 --- a/framework/yii/mail/BaseMessage.php +++ b/framework/yii/mail/BaseMessage.php @@ -53,24 +53,6 @@ abstract class BaseMessage extends Object implements MessageInterface /** * @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 = []) { return $this->getMailer()->render($view, $params); diff --git a/framework/yii/mail/MessageInterface.php b/framework/yii/mail/MessageInterface.php index cdf2d3d..0f3e5a4 100644 --- a/framework/yii/mail/MessageInterface.php +++ b/framework/yii/mail/MessageInterface.php @@ -84,18 +84,42 @@ interface MessageInterface /** * Attach specified content as file for the email message. * @param string $content attachment file content. - * @param string $fileName attachment file name. - * @param string $contentType MIME type of the attachment file, by default 'application/octet-stream' will be used. + * @param array $options options for embed file. Valid options are: + * - 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. * @param string $fileName full file name - * @param string $contentType MIME type of the attachment file, if empty it will be suggested automatically. - * @param string $attachFileName name, which should be used for attachment, if empty file base name will be used. + * @param array $options options for embed file. Valid options are: + * - 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. diff --git a/tests/unit/extensions/swiftmailer/MessageTest.php b/tests/unit/extensions/swiftmailer/MessageTest.php index d55ac3a..0a02b6d 100644 --- a/tests/unit/extensions/swiftmailer/MessageTest.php +++ b/tests/unit/extensions/swiftmailer/MessageTest.php @@ -3,6 +3,7 @@ namespace yiiunit\extensions\swiftmailer; use Yii; +use yii\helpers\FileHelper; use yii\swiftmailer\Mailer; use yii\swiftmailer\Message; use yiiunit\VendorTestCase; @@ -26,6 +27,26 @@ class MessageTest extends VendorTestCase '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 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 : public function testGetSwiftMessage() @@ -83,14 +124,52 @@ class MessageTest extends VendorTestCase /** * @depends testSend */ - public function testCreateAttachment() + public function testAttachContent() { $message = $this->createTestMessage(); $message->setTo($this->testEmailReceiver); $message->setFrom('someuser@somedomain.com'); $message->setSubject('Yii Swift Create Attachment Test'); $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: 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: pic'); + $this->assertTrue($message->send()); } diff --git a/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php index cded1e4..f135f51 100644 --- a/tests/unit/framework/mail/BaseMailerTest.php +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -186,7 +186,13 @@ class Message extends BaseMessage 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() {