Mailer.php 4.06 KB
Newer Older
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

Paul Klimov committed
8
namespace yii\swiftmailer;
9

10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\mail\BaseMailer;
13 14

/**
Qiang Xue committed
15 16 17
 * Mailer implements a mailer based on SwiftMailer.
 *
 * To use Mailer, you should configure it in the application configuration like the following,
18 19
 *
 * ~~~
Qiang Xue committed
20
 * 'components' => [
21
 *     ...
Qiang Xue committed
22
 *     'email' => [
23
 *         'class' => 'yii\swiftmailer\Mailer',
24 25 26 27 28 29 30 31
 *         'transport' => [
 *             'class' => 'Swift_SmtpTransport',
 *             'host' => 'localhost',
 *             'username' => 'username',
 *             'password' => 'password',
 *             'port' => '587',
 *             'encryption' => 'tls',
 *         ],
Qiang Xue committed
32
 *     ],
33
 *     ...
Qiang Xue committed
34
 * ],
35 36
 * ~~~
 *
Qiang Xue committed
37 38 39 40 41 42 43 44 45 46 47 48
 * You may also skip the configuration of the [[transport]] property. In that case, the default
 * PHP `mail()` function will be used to send emails.
 *
 * To send an email, you may use the following code:
 *
 * ~~~
 * Yii::$app->mail->compose('contact/html', ['contactForm' => $form])
 *     ->setFrom('from@domain.com')
 *     ->setTo($form->email)
 *     ->setSubject($form->subject)
 *     ->send();
 * ~~~
49
 *
Qiang Xue committed
50
 * @see http://swiftmailer.org
51
 *
52 53 54 55 56
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
class Mailer extends BaseMailer
{
57 58 59 60
	/**
	 * @var string message default class name.
	 */
	public $messageClass = 'yii\swiftmailer\Message';
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	/**
	 * @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
Qiang Xue committed
83
	 * @throws InvalidConfigException on invalid argument.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	 */
	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
	 */
107
	protected function sendMessage($message)
108
	{
Qiang Xue committed
109 110
		$address = $message->getTo();
		if (is_array($address)) {
111
			$address = implode(', ', array_keys($address));
Qiang Xue committed
112
		}
113
		Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
Qiang Xue committed
114
		return $this->getSwiftMailer()->send($message->getSwiftMessage()) > 0;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	}

	/**
	 * 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)
	{
134
		if (isset($config['class'])) {
135 136 137 138 139
			$className = $config['class'];
			unset($config['class']);
		} else {
			$className = 'Swift_MailTransport';
		}
140 141
		/** @var \Swift_MailTransport $transport */
		$transport = $className::newInstance();
142 143 144 145 146 147
		if (!empty($config)) {
			foreach ($config as $name => $value) {
				if (property_exists($transport, $name)) {
					$transport->$name = $value;
				} else {
					$setter = 'set' . $name;
148
					if (method_exists($transport, $setter) || method_exists($transport, '__call')) {
149 150 151 152 153 154 155 156 157
						$transport->$setter($value);
					} else {
						throw new InvalidConfigException('Setting unknown property: ' . get_class($transport) . '::' . $name);
					}
				}
			}
		}
		return $transport;
	}
158
}