Browse Source

'yii\mail\MessageInterface' file related methods interface adjusted to use options, embed file methods added.

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
774c4db860
  1. 56
      extensions/swiftmailer/yii/swiftmailer/Message.php
  2. 18
      framework/yii/mail/BaseMessage.php
  3. 36
      framework/yii/mail/MessageInterface.php
  4. 83
      tests/unit/extensions/swiftmailer/MessageTest.php
  5. 8
      tests/unit/framework/mail/BaseMailerTest.php

56
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();

18
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);

36
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.

83
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: <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());
}

8
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()
{

Loading…
Cancel
Save