mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-30 06:29:02 +08:00
Interface 'MailerInterface' extracted.
Method 'MailerInterface::createMessage()' added.
This commit is contained in:
@@ -20,13 +20,11 @@ use Yii;
|
||||
*
|
||||
* @property \yii\base\View|array $view view instance or its array configuration.
|
||||
* @property \yii\mail\ViewResolver|array $viewResolver view resolver instance or its array configuration.
|
||||
* @property array $defaultMessageConfig configuration, which should be applied by default to any
|
||||
* new created email message instance.
|
||||
*
|
||||
* @author Paul Klimov <klimov.paul@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
abstract class BaseMailer extends Component
|
||||
abstract class BaseMailer extends Component implements MailerInterface
|
||||
{
|
||||
/**
|
||||
* @var \yii\base\View|array view instance or its array configuration.
|
||||
@@ -49,6 +47,10 @@ abstract class BaseMailer extends Component
|
||||
* ~~~
|
||||
*/
|
||||
public $messageConfig = [];
|
||||
/**
|
||||
* @var string message default class name.
|
||||
*/
|
||||
public $messageClass = 'yii\mail\BaseMessage';
|
||||
|
||||
/**
|
||||
* @param array|\yii\base\View $view view instance or its array configuration.
|
||||
@@ -123,11 +125,20 @@ abstract class BaseMailer extends Component
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the given email message.
|
||||
* @param object $message email message instance
|
||||
* @return boolean whether the message has been sent.
|
||||
* Creates new message instance from given configuration.
|
||||
* Message configuration will be merged with [[messageConfig]].
|
||||
* If 'class' parameter is omitted [[messageClass]], will be used.
|
||||
* @param array $config message configuration.
|
||||
* @return MessageInterface message instance.
|
||||
*/
|
||||
abstract public function send($message);
|
||||
public function createMessage(array $config = [])
|
||||
{
|
||||
$config = array_merge($this->messageConfig, $config);
|
||||
if (!array_key_exists('class', $config)) {
|
||||
$config['class'] = $this->messageClass;
|
||||
}
|
||||
return Yii::createObject($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a couple of messages at once.
|
||||
@@ -135,9 +146,10 @@ abstract class BaseMailer extends Component
|
||||
* saving resources, for example on open/close connection operations,
|
||||
* they may override this method to create their specific implementation.
|
||||
* @param array $messages list of email messages, which should be sent.
|
||||
* @return integer number of successfull sends
|
||||
* @return integer number of successful sends.
|
||||
*/
|
||||
public function sendMultiple(array $messages) {
|
||||
public function sendMultiple(array $messages)
|
||||
{
|
||||
$successCount = 0;
|
||||
foreach ($messages as $message) {
|
||||
if ($this->send($message)) {
|
||||
|
||||
@@ -39,17 +39,7 @@ abstract class BaseMessage extends Object implements MessageInterface
|
||||
*/
|
||||
public function getMailer()
|
||||
{
|
||||
return Yii::$app->getComponent('email');
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the object.
|
||||
* This method is invoked at the end of the constructor after the object is initialized with the
|
||||
* given configuration.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
Yii::configure($this, $this->getMailer()->messageConfig);
|
||||
return Yii::$app->getComponent('mail');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
40
framework/yii/mail/MailerInterface.php
Normal file
40
framework/yii/mail/MailerInterface.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\mail;
|
||||
|
||||
/**
|
||||
* MailerInterface is an interface, which any mailer should apply.
|
||||
*
|
||||
* @author Paul Klimov <klimov.paul@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
interface MailerInterface
|
||||
{
|
||||
/**
|
||||
* Creates new message instance from given configuration.
|
||||
* @param array $config message configuration.
|
||||
* @return MessageInterface message instance.
|
||||
*/
|
||||
public function createMessage(array $config = []);
|
||||
|
||||
/**
|
||||
* Sends the given email message.
|
||||
* @param object $message email message instance
|
||||
* @return boolean whether the message has been sent.
|
||||
*/
|
||||
public function send($message);
|
||||
|
||||
/**
|
||||
* Sends a couple of messages at once.
|
||||
* Note: some particular mailers may benefit from sending messages as batch,
|
||||
* saving resources, for example on open/close connection operations.
|
||||
* @param array $messages list of email messages, which should be sent.
|
||||
* @return integer number of successful sends.
|
||||
*/
|
||||
public function sendMultiple(array $messages);
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\mail;
|
||||
|
||||
use yii\swiftmailer\Message as SwiftMessage;
|
||||
|
||||
/**
|
||||
* Message provides the email message sending functionality.
|
||||
*
|
||||
* Usage:
|
||||
* ~~~
|
||||
* $email = new Message();
|
||||
* $email->from = 'sender@domain.com';
|
||||
* $email->to = 'receiver@domain.com';
|
||||
* $email->subject = 'Message Subject';
|
||||
* $email->text = 'Message Content';
|
||||
* $email->send();
|
||||
* ~~~
|
||||
*
|
||||
* You can use message object to render view, which can be used to compose the message content:
|
||||
* ~~~
|
||||
* $email = new Message();
|
||||
* $email->from = $contactForm->email;
|
||||
* $email->to = 'admin@domain.com';
|
||||
* $email->subject = $email->render('contact/subject', ['form' => $contactForm]);
|
||||
* $email->addHtml($email->render('contact/html', ['form' => $contactForm]));
|
||||
* $email->addText($email->render('contact/text', ['form' => $contactForm]));
|
||||
* $email->send();
|
||||
* ~~~
|
||||
*
|
||||
* This particular class uses 'SwiftMailer' library to perform the message sending.
|
||||
* Note: you can replace usage of this class by your own one, using [[Yii::$classMap]]:
|
||||
* ~~~
|
||||
* Yii::$classMap['yii\mail\Message'] = '/path/to/my/email/Message.php'
|
||||
* ~~~
|
||||
*
|
||||
* @author Paul Klimov <klimov.paul@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class Message extends SwiftMessage {}
|
||||
@@ -8,7 +8,7 @@
|
||||
namespace yii\mail;
|
||||
|
||||
/**
|
||||
* Class MessageInterface
|
||||
* MessageInterface is an interface, which email message should apply.
|
||||
*
|
||||
* @author Paul Klimov <klimov.paul@gmail.com>
|
||||
* @since 2.0
|
||||
|
||||
Reference in New Issue
Block a user