Mailer.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. return $this->_swiftMailer;
  105. }
  106. /**
  107. * @param array|\Swift_Transport $transport
  108. * @throws InvalidConfigException on invalid argument.
  109. */
  110. public function setTransport($transport)
  111. {
  112. if (!is_array($transport) && !is_object($transport)) {
  113. throw new InvalidConfigException('"' . get_class($this) . '::transport" should be either object or array, "' . gettype($transport) . '" given.');
  114. }
  115. $this->_transport = $transport;
  116. $this->_swiftMailer = null;
  117. }
  118. /**
  119. * @return array|\Swift_Transport
  120. */
  121. public function getTransport()
  122. {
  123. if (!is_object($this->_transport)) {
  124. $this->_transport = $this->createTransport($this->_transport);
  125. }
  126. return $this->_transport;
  127. }
  128. /**
  129. * @inheritdoc
  130. */
  131. protected function sendMessage($message)
  132. {
  133. /* @var $message Message */
  134. $address = $message->getTo();
  135. if (is_array($address)) {
  136. $address = implode(', ', array_keys($address));
  137. }
  138. Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
  139. return $this->getSwiftMailer()->send($message->getSwiftMessage()) > 0;
  140. }
  141. /**
  142. * Creates Swift mailer instance.
  143. * @return \Swift_Mailer mailer instance.
  144. */
  145. protected function createSwiftMailer()
  146. {
  147. return new \Swift_Mailer($this->getTransport());
  148. }
  149. /**
  150. * Creates email transport instance by its array configuration.
  151. * @param array $config transport configuration.
  152. * @throws \yii\base\InvalidConfigException on invalid transport configuration.
  153. * @return \Swift_Transport transport instance.
  154. */
  155. protected function createTransport(array $config)
  156. {
  157. if (!isset($config['class'])) {
  158. $config['class'] = 'Swift_SendmailTransport';
  159. }
  160. if (isset($config['plugins'])) {
  161. $plugins = $config['plugins'];
  162. unset($config['plugins']);
  163. } else {
  164. $plugins = [];
  165. }
  166. if ($this->enableSwiftMailerLogging) {
  167. $plugins[] = [
  168. 'class' => 'Swift_Plugins_LoggerPlugin',
  169. 'constructArgs' => [
  170. [
  171. 'class' => 'yii\swiftmailer\Logger'
  172. ]
  173. ],
  174. ];
  175. }
  176. /* @var $transport \Swift_Transport */
  177. $transport = $this->createSwiftObject($config);
  178. if (!empty($plugins)) {
  179. foreach ($plugins as $plugin) {
  180. if (is_array($plugin) && isset($plugin['class'])) {
  181. $plugin = $this->createSwiftObject($plugin);
  182. }
  183. $transport->registerPlugin($plugin);
  184. }
  185. }
  186. return $transport;
  187. }
  188. /**
  189. * Creates Swift library object, from given array configuration.
  190. * @param array $config object configuration
  191. * @return Object created object
  192. * @throws \yii\base\InvalidConfigException on invalid configuration.
  193. */
  194. protected function createSwiftObject(array $config)
  195. {
  196. if (isset($config['class'])) {
  197. $className = $config['class'];
  198. unset($config['class']);
  199. } else {
  200. throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
  201. }
  202. if (isset($config['constructArgs'])) {
  203. $args = [];
  204. foreach ($config['constructArgs'] as $arg) {
  205. if (is_array($arg) && isset($arg['class'])) {
  206. $args[] = $this->createSwiftObject($arg);
  207. } else {
  208. $args[] = $arg;
  209. }
  210. }
  211. unset($config['constructArgs']);
  212. $object = Yii::createObject($className, $args);
  213. } else {
  214. $object = Yii::createObject($className);
  215. }
  216. if (!empty($config)) {
  217. $reflection = new \ReflectionObject($object);
  218. foreach ($config as $name => $value) {
  219. if ($reflection->hasProperty($name) && $reflection->getProperty($name)->isPublic()) {
  220. $object->$name = $value;
  221. } else {
  222. $setter = 'set' . $name;
  223. if ($reflection->hasMethod($setter) || $reflection->hasMethod('__call')) {
  224. $object->$setter($value);
  225. } else {
  226. throw new InvalidConfigException('Setting unknown property: ' . $className . '::' . $name);
  227. }
  228. }
  229. }
  230. }
  231. return $object;
  232. }
  233. }