選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Mailer.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 \Swift_Mailer Swift mailer instance.
  81. */
  82. private $_swiftMailer;
  83. /**
  84. * @var \Swift_Transport|array Swift transport instance or its array configuration.
  85. */
  86. private $_transport = [];
  87. /**
  88. * @return array|\Swift_Mailer Swift mailer instance or array configuration.
  89. */
  90. public function getSwiftMailer()
  91. {
  92. if (!is_object($this->_swiftMailer)) {
  93. $this->_swiftMailer = $this->createSwiftMailer();
  94. }
  95. return $this->_swiftMailer;
  96. }
  97. /**
  98. * @param array|\Swift_Transport $transport
  99. * @throws InvalidConfigException on invalid argument.
  100. */
  101. public function setTransport($transport)
  102. {
  103. if (!is_array($transport) && !is_object($transport)) {
  104. throw new InvalidConfigException('"' . get_class($this) . '::transport" should be either object or array, "' . gettype($transport) . '" given.');
  105. }
  106. $this->_transport = $transport;
  107. }
  108. /**
  109. * @return array|\Swift_Transport
  110. */
  111. public function getTransport()
  112. {
  113. if (!is_object($this->_transport)) {
  114. $this->_transport = $this->createTransport($this->_transport);
  115. }
  116. return $this->_transport;
  117. }
  118. /**
  119. * @inheritdoc
  120. */
  121. protected function sendMessage($message)
  122. {
  123. $address = $message->getTo();
  124. if (is_array($address)) {
  125. $address = implode(', ', array_keys($address));
  126. }
  127. Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
  128. return $this->getSwiftMailer()->send($message->getSwiftMessage()) > 0;
  129. }
  130. /**
  131. * Creates Swift mailer instance.
  132. * @return \Swift_Mailer mailer instance.
  133. */
  134. protected function createSwiftMailer()
  135. {
  136. return \Swift_Mailer::newInstance($this->getTransport());
  137. }
  138. /**
  139. * Creates email transport instance by its array configuration.
  140. * @param array $config transport configuration.
  141. * @throws \yii\base\InvalidConfigException on invalid transport configuration.
  142. * @return \Swift_Transport transport instance.
  143. */
  144. protected function createTransport(array $config)
  145. {
  146. if (!isset($config['class'])) {
  147. $config['class'] = 'Swift_MailTransport';
  148. }
  149. if (isset($config['plugins'])) {
  150. $plugins = $config['plugins'];
  151. unset($config['plugins']);
  152. }
  153. /* @var $transport \Swift_MailTransport */
  154. $transport = $this->createSwiftObject($config);
  155. if (isset($plugins)) {
  156. foreach ($plugins as $plugin) {
  157. if (is_array($plugin) && isset($plugin['class'])) {
  158. $plugin = $this->createSwiftObject($plugin);
  159. }
  160. $transport->registerPlugin($plugin);
  161. }
  162. }
  163. return $transport;
  164. }
  165. /**
  166. * Creates Swift library object, from given array configuration.
  167. * @param array $config object configuration
  168. * @return Object created object
  169. * @throws \yii\base\InvalidConfigException on invalid configuration.
  170. */
  171. protected function createSwiftObject(array $config)
  172. {
  173. if (isset($config['class'])) {
  174. $className = $config['class'];
  175. unset($config['class']);
  176. } else {
  177. throw new InvalidConfigException('Object configuration must be an array containing a "class" element.');
  178. }
  179. if (isset($config['constructArgs'])) {
  180. $args = [];
  181. foreach ($config['constructArgs'] as $arg) {
  182. if (is_array($arg) && isset($arg['class'])) {
  183. $args[] = $this->createSwiftObject($arg);
  184. } else {
  185. $args[] = $arg;
  186. }
  187. }
  188. unset($config['constructArgs']);
  189. $object = Yii::createObject($className, $args);
  190. } else {
  191. $object = Yii::createObject($className);
  192. }
  193. if (!empty($config)) {
  194. foreach ($config as $name => $value) {
  195. if (property_exists($object, $name)) {
  196. $object->$name = $value;
  197. } else {
  198. $setter = 'set' . $name;
  199. if (method_exists($object, $setter) || method_exists($object, '__call')) {
  200. $object->$setter($value);
  201. } else {
  202. throw new InvalidConfigException('Setting unknown property: ' . $className . '::' . $name);
  203. }
  204. }
  205. }
  206. }
  207. return $object;
  208. }
  209. }