|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- /**
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
-
- namespace yii\swiftmailer;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\mail\BaseMailer;
-
- /**
- * Mailer implements a mailer based on SwiftMailer.
- *
- * To use Mailer, you should configure it in the application configuration like the following,
- *
- * ~~~
- * 'components' => [
- * ...
- * 'mailer' => [
- * 'class' => 'yii\swiftmailer\Mailer',
- * 'transport' => [
- * 'class' => 'Swift_SmtpTransport',
- * 'host' => 'localhost',
- * 'username' => 'username',
- * 'password' => 'password',
- * 'port' => '587',
- * 'encryption' => 'tls',
- * ],
- * ],
- * ...
- * ],
- * ~~~
- *
- * You may also skip the configuration of the [[transport]] property. In that case, the default
- * PHP `mail()` function will be used to send emails.
- *
- * You specify the transport constructor arguments using 'constructArgs' key in the config.
- * You can also specify the list of plugins, which should be registered to the transport using
- * 'plugins' key. For example:
- *
- * ~~~
- * 'transport' => [
- * 'class' => 'Swift_SmtpTransport',
- * 'constructArgs' => ['localhost', 25]
- * 'plugins' => [
- * [
- * 'class' => 'Swift_Plugins_ThrottlerPlugin',
- * 'constructArgs' => [20],
- * ],
- * ],
- * ],
- * ~~~
- *
- * To send an email, you may use the following code:
- *
- * ~~~
- * Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
- * ->setFrom('from@domain.com')
- * ->setTo($form->email)
- * ->setSubject($form->subject)
- * ->send();
- * ~~~
- *
- * @see http://swiftmailer.org
- *
- * @property array|\Swift_Mailer $swiftMailer Swift mailer instance or array configuration. This property is
- * read-only.
- * @property array|\Swift_Transport $transport This property is read-only.
- *
- * @author Paul Klimov <klimov.paul@gmail.com>
- * @since 2.0
- */
- class Mailer extends BaseMailer
- {
- /**
- * @var string message default class name.
- */
- public $messageClass = 'yii\swiftmailer\Message';
- /**
- * @var boolean whether to enable writing of the SwiftMailer internal logs using Yii log mechanism.
- * If enabled [[Logger]] plugin will be attached to the [[transport]] for this purpose.
- * @see Logger
- */
- public $enableSwiftMailerLogging = false;
-
- /**
- * @var \Swift_Mailer Swift mailer instance.
- */
- private $_swiftMailer;
- /**
- * @var \Swift_Transport|array Swift transport instance or its array configuration.
- */
- private $_transport = [];
-
-
- /**
- * @return array|\Swift_Mailer Swift mailer instance or array configuration.
- */
- public function getSwiftMailer()
- {
- if (!is_object($this->_swiftMailer)) {
- $this->_swiftMailer = $this->createSwiftMailer();
- }
-
- return $this->_swiftMailer;
- }
-
- /**
- * @param array|\Swift_Transport $transport
- * @throws InvalidConfigException on invalid argument.
- */
- public function setTransport($transport)
- {
- if (!is_array($transport) && !is_object($transport)) {
- throw new InvalidConfigException('"' . get_class($this) . '::transport" should be either object or array, "' . gettype($transport) . '" given.');
- }
- $this->_transport = $transport;
- }
-
- /**
- * @return array|\Swift_Transport
- */
- public function getTransport()
- {
- if (!is_object($this->_transport)) {
- $this->_transport = $this->createTransport($this->_transport);
- }
-
- return $this->_transport;
- }
-
- /**
- * @inheritdoc
- */
- protected function sendMessage($message)
- {
- $address = $message->getTo();
- if (is_array($address)) {
- $address = implode(', ', array_keys($address));
- }
- Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
-
- return $this->getSwiftMailer()->send($message->getSwiftMessage()) > 0;
- }
-
- /**
- * Creates Swift mailer instance.
- * @return \Swift_Mailer mailer instance.
- */
- protected function createSwiftMailer()
- {
- return \Swift_Mailer::newInstance($this->getTransport());
- }
-
- /**
- * Creates email transport instance by its array configuration.
- * @param array $config transport configuration.
- * @throws \yii\base\InvalidConfigException on invalid transport configuration.
- * @return \Swift_Transport transport instance.
- */
- protected function createTransport(array $config)
- {
- if (!isset($config['class'])) {
- $config['class'] = 'Swift_MailTransport';
- }
- if (isset($config['plugins'])) {
- $plugins = $config['plugins'];
- unset($config['plugins']);
- } else {
- $plugins = [];
- }
-
- if ($this->enableSwiftMailerLogging) {
- $plugins[] = [
- 'class' => 'Swift_Plugins_LoggerPlugin',
- 'constructArgs' => [
- [
- 'class' => 'yii\swiftmailer\Logger'
- ]
- ],
- ];
- }
-
- /* @var $transport \Swift_MailTransport */
- $transport = $this->createSwiftObject($config);
- if (!empty($plugins)) {
- foreach ($plugins as $plugin) {
- if (is_array($plugin) && isset($plugin['class'])) {
- $plugin = $this->createSwiftObject($plugin);
- }
- $transport->registerPlugin($plugin);
- }
- }
-
- return $transport;
- }
-
- /**
- * Creates Swift library object, from given array configuration.
- * @param array $config object configuration
- * @return Object created object
- * @throws \yii\base\InvalidConfigException on invalid configuration.
- */
- protected function createSwiftObject(array $config)
- {
- if (isset($config['class'])) {
- $className = $config['class'];
- unset($config['class']);
- } else {
- throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
- }
-
- if (isset($config['constructArgs'])) {
- $args = [];
- foreach ($config['constructArgs'] as $arg) {
- if (is_array($arg) && isset($arg['class'])) {
- $args[] = $this->createSwiftObject($arg);
- } else {
- $args[] = $arg;
- }
- }
- unset($config['constructArgs']);
- $object = Yii::createObject($className, $args);
- } else {
- $object = Yii::createObject($className);
- }
-
- if (!empty($config)) {
- $reflection = new \ReflectionObject($object);
- foreach ($config as $name => $value) {
- if ($reflection->hasProperty($name) && $reflection->getProperty($name)->isPublic()) {
- $object->$name = $value;
- } else {
- $setter = 'set' . $name;
- if ($reflection->hasMethod($setter) || $reflection->hasMethod('__call')) {
- $object->$setter($value);
- } else {
- throw new InvalidConfigException('Setting unknown property: ' . $className . '::' . $name);
- }
- }
- }
- }
-
- return $object;
- }
- }
|