|
- <?php
-
-
- namespace yii\swiftmailer;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\mail\BaseMailer;
-
-
- class Mailer extends BaseMailer
- {
-
-
- public $messageClass = 'yii\swiftmailer\Message';
-
-
- public $enableSwiftMailerLogging = false;
-
-
-
- private $_swiftMailer;
-
-
- private $_transport = [];
-
-
-
-
- public function getSwiftMailer()
- {
- if (!is_object($this->_swiftMailer)) {
- $this->_swiftMailer = $this->createSwiftMailer();
- }
-
- return $this->_swiftMailer;
- }
-
-
-
- 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;
- }
-
-
-
- public function getTransport()
- {
- if (!is_object($this->_transport)) {
- $this->_transport = $this->createTransport($this->_transport);
- }
-
- return $this->_transport;
- }
-
-
-
- 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;
- }
-
-
-
- protected function createSwiftMailer()
- {
- return \Swift_Mailer::newInstance($this->getTransport());
- }
-
-
-
- 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'
- ]
- ],
- ];
- }
-
-
- $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;
- }
-
-
-
- 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;
- }
- }
|