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:
@@ -16,7 +16,7 @@ use yii\web\View;
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
@@ -28,10 +28,6 @@ use yii\web\View;
|
||||
*/
|
||||
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.
|
||||
* 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()]]
|
||||
*/
|
||||
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
|
||||
@@ -172,6 +189,30 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
|
||||
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.
|
||||
*
|
||||
@@ -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.
|
||||
* This method will return the view file by prefixing the view name with [[viewPath]].
|
||||
|
||||
Reference in New Issue
Block a user