Mailer.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\swiftmailer;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\mail\BaseMailer;
  11. /**
  12. * Mailer implements a mailer based on SwiftMailer.
  13. *
  14. * To use Mailer, you should configure it in the application configuration like the following:
  15. *
  16. * ```php
  17. * [
  18. * 'components' => [
  19. * 'mailer' => [
  20. * 'class' => 'yii\swiftmailer\Mailer',
  21. * 'transport' => [
  22. * 'class' => 'Swift_SmtpTransport',
  23. * 'host' => 'localhost',
  24. * 'username' => 'username',
  25. * 'password' => 'password',
  26. * 'port' => '587',
  27. * 'encryption' => 'tls',
  28. * ],
  29. * ],
  30. * // ...
  31. * ],
  32. * // ...
  33. * ],
  34. * ```
  35. *
  36. * You may also skip the configuration of the [[transport]] property. In that case, the default
  37. * `\Swift_SendmailTransport` transport will be used to send emails.
  38. *
  39. * You specify the transport constructor arguments using 'constructArgs' key in the config.
  40. * You can also specify the list of plugins, which should be registered to the transport using
  41. * 'plugins' key. For example:
  42. *
  43. * ```php
  44. * 'transport' => [
  45. * 'class' => 'Swift_SmtpTransport',
  46. * 'constructArgs' => ['localhost', 25]
  47. * 'plugins' => [
  48. * [
  49. * 'class' => 'Swift_Plugins_ThrottlerPlugin',
  50. * 'constructArgs' => [20],
  51. * ],
  52. * ],
  53. * ],
  54. * ```
  55. *
  56. * To send an email, you may use the following code:
  57. *
  58. * ```php
  59. * Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
  60. * ->setFrom('from@domain.com')
  61. * ->setTo($form->email)
  62. * ->setSubject($form->subject)
  63. * ->send();
  64. * ```
  65. *
  66. * @see http://swiftmailer.org
  67. *
  68. * @property array|\Swift_Mailer $swiftMailer Swift mailer instance or array configuration. This property is
  69. * read-only.
  70. * @property array|\Swift_Transport $transport This property is read-only.
  71. *
  72. * @author Paul Klimov <klimov.paul@gmail.com>
  73. * @since 2.0
  74. */
  75. class Mailer extends BaseMailer
  76. {
  77. /**
  78. * @var string message default class name.
  79. */
  80. public $messageClass = 'yii\swiftmailer\Message';
  81. /**
  82. * @var bool whether to enable writing of the SwiftMailer internal logs using Yii log mechanism.
  83. * If enabled [[Logger]] plugin will be attached to the [[transport]] for this purpose.
  84. * @see Logger
  85. * @since 2.0.4
  86. */
  87. public $enableSwiftMailerLogging = false;
  88. /**
  89. * @var \Swift_Mailer Swift mailer instance.
  90. */
  91. private $_swiftMailer;
  92. /**
  93. * @var \Swift_Transport|array Swift transport instance or its array configuration.
  94. */
  95. private $_transport = [];
  96. /**
  97. * @return array|\Swift_Mailer Swift mailer instance or array configuration.
  98. */
  99. public function getSwiftMailer()
  100. {
  101. if (!is_object($this->_swiftMailer)) {
  102. $this->_swiftMailer = $this->createSwiftMailer();
  103. }
  104. if (!$this->_transport->ping()) {
  105. $this->_transport->stop();
  106. $this->_transport->start();
  107. }
  108. return $this->_swiftMailer;
  109. }
  110. /**
  111. * @param array|\Swift_Transport $transport
  112. * @throws InvalidConfigException on invalid argument.
  113. */
  114. public function setTransport($transport)
  115. {
  116. if (!is_array($transport) && !is_object($transport)) {
  117. throw new InvalidConfigException('"' . get_class($this) . '::transport" should be either object or array, "' . gettype($transport) . '" given.');
  118. }
  119. $this->_transport = $transport;
  120. $this->_swiftMailer = null;
  121. }
  122. /**
  123. * @return array|\Swift_Transport
  124. */
  125. public function getTransport()
  126. {
  127. if (!is_object($this->_transport)) {
  128. $this->_transport = $this->createTransport($this->_transport);
  129. }
  130. return $this->_transport;
  131. }
  132. /**
  133. * @inheritdoc
  134. */
  135. protected function sendMessage($message)
  136. {
  137. /* @var $message Message */
  138. $address = $message->getTo();
  139. if (is_array($address)) {
  140. $address = implode(', ', array_keys($address));
  141. }
  142. Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
  143. return $this->getSwiftMailer()->send($message->getSwiftMessage()) > 0;
  144. }
  145. /**
  146. * Creates Swift mailer instance.
  147. * @return \Swift_Mailer mailer instance.
  148. */
  149. protected function createSwiftMailer()
  150. {
  151. return new \Swift_Mailer($this->getTransport());
  152. }
  153. /**
  154. * Creates email transport instance by its array configuration.
  155. * @param array $config transport configuration.
  156. * @throws \yii\base\InvalidConfigException on invalid transport configuration.
  157. * @return \Swift_Transport transport instance.
  158. */
  159. protected function createTransport(array $config)
  160. {
  161. if (!isset($config['class'])) {
  162. $config['class'] = 'Swift_SendmailTransport';
  163. }
  164. if (isset($config['plugins'])) {
  165. $plugins = $config['plugins'];
  166. unset($config['plugins']);
  167. } else {
  168. $plugins = [];
  169. }
  170. if ($this->enableSwiftMailerLogging) {
  171. $plugins[] = [
  172. 'class' => 'Swift_Plugins_LoggerPlugin',
  173. 'constructArgs' => [
  174. [
  175. 'class' => 'yii\swiftmailer\Logger'
  176. ]
  177. ],
  178. ];
  179. }
  180. /* @var $transport \Swift_Transport */
  181. $transport = $this->createSwiftObject($config);
  182. if (!empty($plugins)) {
  183. foreach ($plugins as $plugin) {
  184. if (is_array($plugin) && isset($plugin['class'])) {
  185. $plugin = $this->createSwiftObject($plugin);
  186. }
  187. $transport->registerPlugin($plugin);
  188. }
  189. }
  190. return $transport;
  191. }
  192. /**
  193. * Creates Swift library object, from given array configuration.
  194. * @param array $config object configuration
  195. * @return Object created object
  196. * @throws \yii\base\InvalidConfigException on invalid configuration.
  197. */
  198. protected function createSwiftObject(array $config)
  199. {
  200. if (isset($config['class'])) {
  201. $className = $config['class'];
  202. unset($config['class']);
  203. } else {
  204. throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
  205. }
  206. if (isset($config['constructArgs'])) {
  207. $args = [];
  208. foreach ($config['constructArgs'] as $arg) {
  209. if (is_array($arg) && isset($arg['class'])) {
  210. $args[] = $this->createSwiftObject($arg);
  211. } else {
  212. $args[] = $arg;
  213. }
  214. }
  215. unset($config['constructArgs']);
  216. $object = Yii::createObject($className, $args);
  217. } else {
  218. $object = Yii::createObject($className);
  219. }
  220. if (!empty($config)) {
  221. $reflection = new \ReflectionObject($object);
  222. foreach ($config as $name => $value) {
  223. if ($reflection->hasProperty($name) && $reflection->getProperty($name)->isPublic()) {
  224. $object->$name = $value;
  225. } else {
  226. $setter = 'set' . $name;
  227. if ($reflection->hasMethod($setter) || $reflection->hasMethod('__call')) {
  228. $object->$setter($value);
  229. } else {
  230. throw new InvalidConfigException('Setting unknown property: ' . $className . '::' . $name);
  231. }
  232. }
  233. }
  234. }
  235. return $object;
  236. }
  237. }