You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
7.4KB

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