mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 23:09:10 +08:00
Implemented file transport for mails.
This commit is contained in:
@@ -104,7 +104,7 @@ class Mailer extends BaseMailer
|
|||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
public function send($message)
|
protected function sendMessage($message)
|
||||||
{
|
{
|
||||||
$address = $message->getTo();
|
$address = $message->getTo();
|
||||||
if (is_array($address)) {
|
if (is_array($address)) {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ use yii\web\View;
|
|||||||
/**
|
/**
|
||||||
* BaseMailer serves as a base class that implements the basic functions required by [[MailerInterface]].
|
* BaseMailer serves as a base class that implements the basic functions required by [[MailerInterface]].
|
||||||
*
|
*
|
||||||
* Concrete child classes should may focus on implementing the [[send()]] method.
|
* Concrete child classes should may focus on implementing the [[sendMessage()]] method.
|
||||||
*
|
*
|
||||||
* @see BaseMessage
|
* @see BaseMessage
|
||||||
*
|
*
|
||||||
@@ -28,10 +28,6 @@ use yii\web\View;
|
|||||||
*/
|
*/
|
||||||
abstract class BaseMailer extends Component implements MailerInterface, ViewContextInterface
|
abstract class BaseMailer extends Component implements MailerInterface, ViewContextInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var \yii\base\View|array view instance or its array configuration.
|
|
||||||
*/
|
|
||||||
private $_view = [];
|
|
||||||
/**
|
/**
|
||||||
* @var string directory containing view files for this email messages.
|
* @var string directory containing view files for this email messages.
|
||||||
* This can be specified as an absolute path or path alias.
|
* This can be specified as an absolute path or path alias.
|
||||||
@@ -71,6 +67,27 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
|
|||||||
* @var string the default class name of the new message instances created by [[createMessage()]]
|
* @var string the default class name of the new message instances created by [[createMessage()]]
|
||||||
*/
|
*/
|
||||||
public $messageClass = 'yii\mail\BaseMessage';
|
public $messageClass = 'yii\mail\BaseMessage';
|
||||||
|
/**
|
||||||
|
* @var boolean whether to save email messages as files under [[fileTransportPath]] instead of sending them
|
||||||
|
* to the actual recipients. This is usually used during development for debugging purpose.
|
||||||
|
* @see fileTransportPath
|
||||||
|
*/
|
||||||
|
public $useFileTransport = false;
|
||||||
|
/**
|
||||||
|
* @var string the directory where the email messages are saved when [[useFileTransport]] is true.
|
||||||
|
*/
|
||||||
|
public $fileTransportPath = '@runtime/mail';
|
||||||
|
/**
|
||||||
|
* @var callback a PHP callback that will be called by [[send()]] when [[useFileTransport]] is true.
|
||||||
|
* The callback should return a file name which will be used to save the email message.
|
||||||
|
* If not set, the file name will be generated based on the current timestamp.
|
||||||
|
*/
|
||||||
|
public $fileTransportCallback;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \yii\base\View|array view instance or its array configuration.
|
||||||
|
*/
|
||||||
|
private $_view = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|View $view view instance or its array configuration that will be used to
|
* @param array|View $view view instance or its array configuration that will be used to
|
||||||
@@ -172,6 +189,30 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
|
|||||||
return Yii::createObject($config);
|
return Yii::createObject($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the given email message.
|
||||||
|
* This method will log a message about the email being sent.
|
||||||
|
* If [[useFileTransport]] is true, it will save the email as a file under [[fileTransportPath]].
|
||||||
|
* Otherwise, it will call [[sendMessage()]] to send the email to its recipient(s).
|
||||||
|
* Child classes should implement [[sendMessage()]] with the actual email sending logic.
|
||||||
|
* @param MessageInterface $message email message instance to be sent
|
||||||
|
* @return boolean whether the message has been sent successfully
|
||||||
|
*/
|
||||||
|
public function send($message)
|
||||||
|
{
|
||||||
|
$address = $message->getTo();
|
||||||
|
if (is_array($address)) {
|
||||||
|
$address = implode(', ', array_keys($address));
|
||||||
|
}
|
||||||
|
Yii::trace('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
|
||||||
|
|
||||||
|
if ($this->useFileTransport) {
|
||||||
|
return $this->saveMessage($message);
|
||||||
|
} else {
|
||||||
|
return $this->sendMessage($message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends multiple messages at once.
|
* Sends multiple messages at once.
|
||||||
*
|
*
|
||||||
@@ -211,6 +252,35 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the specified message.
|
||||||
|
* This method should be implemented by child classes with the actual email sending logic.
|
||||||
|
* @param MessageInterface $message the message to be sent
|
||||||
|
* @return boolean whether the message is sent successfully
|
||||||
|
*/
|
||||||
|
abstract protected function sendMessage($message);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the message as a file under [[fileTransportPath]].
|
||||||
|
* @param MessageInterface $message
|
||||||
|
* @return boolean whether the message is saved successfully
|
||||||
|
*/
|
||||||
|
protected function saveMessage($message)
|
||||||
|
{
|
||||||
|
$path = Yii::getAlias($this->fileTransportPath);
|
||||||
|
if (!is_dir(($path))) {
|
||||||
|
mkdir($path, 0777, true);
|
||||||
|
}
|
||||||
|
if ($this->fileTransportCallback !== null) {
|
||||||
|
$file = $path . '/' . call_user_func($this->fileTransportCallback);
|
||||||
|
} else {
|
||||||
|
$time = microtime(true);
|
||||||
|
$file = $path . '/' . date('Ymd-His-', $time) . sprintf('%04d', (int)(($time - (int)$time) * 10000)) . '-' . sprintf('%04d', mt_rand(0, 10000)) . '.txt';
|
||||||
|
}
|
||||||
|
file_put_contents($file, $message->toString());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds the view file corresponding to the specified relative view name.
|
* Finds the view file corresponding to the specified relative view name.
|
||||||
* This method will return the view file by prefixing the view name with [[viewPath]].
|
* This method will return the view file by prefixing the view name with [[viewPath]].
|
||||||
|
|||||||
@@ -208,6 +208,28 @@ class BaseMailerTest extends TestCase
|
|||||||
$this->assertEquals($htmlViewFileContent, $message->_htmlBody, 'Unable to render html by direct view!');
|
$this->assertEquals($htmlViewFileContent, $message->_htmlBody, 'Unable to render html by direct view!');
|
||||||
$this->assertEquals(strip_tags($htmlViewFileContent), $message->_textBody, 'Unable to render text by direct view!');
|
$this->assertEquals(strip_tags($htmlViewFileContent), $message->_textBody, 'Unable to render text by direct view!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testUseFileTransport()
|
||||||
|
{
|
||||||
|
$mailer = new Mailer();
|
||||||
|
$this->assertFalse($mailer->useFileTransport);
|
||||||
|
$this->assertEquals('@runtime/mail', $mailer->fileTransportPath);
|
||||||
|
|
||||||
|
$mailer->fileTransportPath = '@yiiunit/runtime/mail';
|
||||||
|
$mailer->useFileTransport = true;
|
||||||
|
$mailer->fileTransportCallback = function () {
|
||||||
|
return 'message.txt';
|
||||||
|
};
|
||||||
|
$message = $mailer->compose()
|
||||||
|
->setTo('to@example.com')
|
||||||
|
->setFrom('from@example.com')
|
||||||
|
->setSubject('test subject')
|
||||||
|
->setTextBody('text body' . microtime(true));
|
||||||
|
$this->assertTrue($mailer->send($message));
|
||||||
|
$file = Yii::getAlias($mailer->fileTransportPath) . '/message.txt';
|
||||||
|
$this->assertTrue(is_file($file));
|
||||||
|
$this->assertEquals($message->toString(), file_get_contents($file));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -218,7 +240,7 @@ class Mailer extends BaseMailer
|
|||||||
public $messageClass = 'yiiunit\framework\mail\Message';
|
public $messageClass = 'yiiunit\framework\mail\Message';
|
||||||
public $sentMessages = [];
|
public $sentMessages = [];
|
||||||
|
|
||||||
public function send($message)
|
protected function sendMessage($message)
|
||||||
{
|
{
|
||||||
$this->sentMessages[] = $message;
|
$this->sentMessages[] = $message;
|
||||||
}
|
}
|
||||||
@@ -340,6 +362,6 @@ class Message extends BaseMessage
|
|||||||
|
|
||||||
public function toString()
|
public function toString()
|
||||||
{
|
{
|
||||||
return get_class($this);
|
return var_export($this, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ class TestMailer extends BaseMailer
|
|||||||
public $messageClass = 'yiiunit\framework\mail\TestMessage';
|
public $messageClass = 'yiiunit\framework\mail\TestMessage';
|
||||||
public $sentMessages = array();
|
public $sentMessages = array();
|
||||||
|
|
||||||
public function send($message)
|
protected function sendMessage($message)
|
||||||
{
|
{
|
||||||
$this->sentMessages[] = $message;
|
$this->sentMessages[] = $message;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user