PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/module/Core/src/Core/Mail.php

https://github.com/zc1415926/eva-engine
PHP | 280 lines | 228 code | 19 blank | 33 comment | 11 complexity | dcfc69e9cf1f07f78b544e3d9b9fad34 MD5 | raw file
  1. <?php
  2. /**
  3. * EvaEngine
  4. *
  5. * @link https://github.com/AlloVince/eva-engine
  6. * @copyright Copyright (c) 2012 AlloVince (http://avnpc.com/)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @author AlloVince
  9. */
  10. namespace Core;
  11. use Eva\Api;
  12. use Eva\Config\Config;
  13. use Eva\Mail\Message;
  14. use Zend\Mail\Transport;
  15. use Zend\Mail\Exception;
  16. use Zend\Di\Di;
  17. use Zend\Di\Config as DiConfig;
  18. /**
  19. * Core Mail
  20. *
  21. * @category Core
  22. * @package Core_Mail
  23. */
  24. class Mail
  25. {
  26. protected $message;
  27. protected $transports;
  28. protected $conflictTransports = array('sendmail', 'smtp', 'queue');
  29. protected $transportsClasses = array(
  30. 'sendmail' => 'Zend\Mail\Transport\Sendmail',
  31. 'smtp' => 'Zend\Mail\Transport\Smtp',
  32. 'file' => 'Zend\Mail\Transport\File',
  33. );
  34. public function setTransportsClasses(array $classes)
  35. {
  36. $this->transportsClasses = $classes;
  37. return $this;
  38. }
  39. public function setMessage(Message $message)
  40. {
  41. $this->message = $message;
  42. return $this;
  43. }
  44. public function getMessage()
  45. {
  46. return $this->message;
  47. }
  48. public function getTransport($type)
  49. {
  50. if(isset($this->transports[$type])) {
  51. return $this->transports[$type];
  52. }
  53. }
  54. public function send(Message $message = null)
  55. {
  56. $message = $message ? $message : $this->message;
  57. if(!$message){
  58. throw new Exception\InvalidArgumentException(sprintf(
  59. 'Mail message not set'
  60. ));
  61. }
  62. $transports = $this->transports;
  63. if(!$transports){
  64. throw new Exception\InvalidArgumentException(sprintf(
  65. 'Mail transport not set'
  66. ));
  67. }
  68. $conflictTransports = $this->conflictTransports;
  69. $transportTypes = array_keys($transports);
  70. if(count(array_intersect($conflictTransports, $transportTypes)) > 1){
  71. throw new Exception\InvalidArgumentException(sprintf(
  72. 'Mail transports conflicted by %s',
  73. implode(",", array_intersect($conflictTransports, $transportTypes))
  74. ));
  75. }
  76. foreach($transports as $transportType => $transport){
  77. $transport->send($message);
  78. }
  79. }
  80. public function __construct(array $config = array())
  81. {
  82. $defaultConfig = array(
  83. 'transports' => array('file'),
  84. 'message' => array(
  85. ),
  86. 'di' => array(
  87. 'definition' => array(
  88. 'class' => array(
  89. 'Zend\View\Resolver\AggregateResolver' => array(
  90. 'attach' => array(
  91. 'resolver' => array(
  92. 'required' => true,
  93. 'type' => 'Zend\View\Resolver\TemplatePathStack',
  94. ),
  95. ),
  96. ),
  97. 'Zend\Mail\Message' => array(
  98. 'setTo' => array(
  99. 'emailOrAddressList' => array(
  100. 'type' => false,
  101. 'required' => true
  102. ),
  103. 'name' => array(
  104. 'type' => false,
  105. 'required' => false
  106. ),
  107. ),
  108. 'addTo' => array(
  109. 'emailOrAddressList' => array(
  110. 'type' => false,
  111. 'required' => true
  112. ),
  113. 'name' => array(
  114. 'type' => false,
  115. 'required' => false
  116. ),
  117. ),
  118. 'setFrom' => array(
  119. 'emailOrAddressList' => array(
  120. 'type' => false,
  121. 'required' => true
  122. ),
  123. 'name' => array(
  124. 'type' => false,
  125. 'required' => false
  126. ),
  127. ),
  128. 'addFrom' => array(
  129. 'emailOrAddressList' => array(
  130. 'type' => false,
  131. 'required' => true
  132. ),
  133. 'name' => array(
  134. 'type' => false,
  135. 'required' => false
  136. ),
  137. ),
  138. 'setSender' => array(
  139. 'emailOrAddressList' => array(
  140. 'type' => false,
  141. 'required' => true
  142. ),
  143. 'name' => array(
  144. 'type' => false,
  145. 'required' => false
  146. ),
  147. ),
  148. ),
  149. ),
  150. ),
  151. 'instance' => array(
  152. 'Zend\View\Resolver\TemplatePathStack' => array(
  153. 'parameters' => array(
  154. 'paths' => array(
  155. Message::VIEW_PATH_NAME => EVA_ROOT_PATH . '/data/',
  156. ),
  157. ),
  158. ),
  159. 'Zend\View\Resolver\AggregateResolver' => array(
  160. 'injections' => array(
  161. 'Zend\View\Resolver\TemplatePathStack',
  162. ),
  163. ),
  164. 'Zend\View\Renderer\PhpRenderer' => array(
  165. 'parameters' => array(
  166. 'resolver' => 'Zend\View\Resolver\AggregateResolver',
  167. ),
  168. ),
  169. //Zend View Di Config End
  170. 'Eva\Mail\Message' => array(
  171. 'parameters' => array(
  172. 'headers' => 'Zend\Mail\Headers',
  173. 'view' => 'Zend\View\Renderer\PhpRenderer',
  174. 'viewModel' => 'Zend\View\Model\ViewModel',
  175. )
  176. ),
  177. 'Zend\Mail\Transport\FileOptions' => array(
  178. 'parameters' => array(
  179. 'path' => EVA_ROOT_PATH . '/data/mail',
  180. )
  181. ),
  182. 'Zend\Mail\Transport\File' => array(
  183. 'injections' => array(
  184. 'Zend\Mail\Transport\FileOptions'
  185. )
  186. ),
  187. /*
  188. 'Zend\Mail\Transport\SmtpOptions' => array(
  189. 'parameters' => array(
  190. 'name' => 'sendgrid',
  191. 'host' => 'smtp.sendgrid.net',
  192. 'port' => 25,
  193. 'connection_class' => 'login',
  194. 'connection_config' => array(
  195. 'username' => 'username',
  196. 'password' => 'password',
  197. ),
  198. )
  199. ),
  200. */
  201. 'Zend\Mail\Transport\Smtp' => array(
  202. 'injections' => array(
  203. 'Zend\Mail\Transport\SmtpOptions'
  204. )
  205. ),
  206. )
  207. ),
  208. );
  209. $globalConfig = Api::_()->getConfig();
  210. if(isset($globalConfig['mail'])){
  211. $config = Config::mergeArray($defaultConfig, $globalConfig['mail'], $config);
  212. //$config = array_merge($defaultConfig, $globalConfig['mail'], $config);
  213. } else {
  214. $config = Config::mergeArray($defaultConfig['mail'], $config);
  215. //$config = array_merge($defaultConfig['mail'], $config);
  216. }
  217. $diConfig = array();
  218. if($config['di']){
  219. $diConfig = $config['di'];
  220. }
  221. $di = new Di();
  222. $di->configure(new DiConfig($diConfig));
  223. $this->message = $di->get('Eva\Mail\Message');
  224. $allowTransports = $this->transportsClasses;
  225. $transportType = '';
  226. if(is_string($config['transports'])){
  227. $transportType = $config['transports'];
  228. $transportClass = isset($allowTransports[$transportType]) ? $allowTransports[$transportType] : null;
  229. if(!$transportClass){
  230. throw new Exception\InvalidArgumentException(sprintf(
  231. 'Unknow transport type %s in method %s"',
  232. $transportType,
  233. __METHOD__
  234. ));
  235. }
  236. $transport = $di->get($transportClass);
  237. //\Zend\Di\Display\Console::export($di);
  238. $this->transports[$transportType] = $transport;
  239. } elseif(is_array($config['transports'])){
  240. //$transports = array();
  241. $transportTypes = $config['transports'];
  242. foreach($transportTypes as $transportType) {
  243. $transportClass = isset($allowTransports[$transportType]) ? $allowTransports[$transportType] : null;
  244. if(!$transportClass){
  245. throw new Exception\InvalidArgumentException(sprintf(
  246. 'Unknow transport type %s in method %s"',
  247. $transportType,
  248. __METHOD__
  249. ));
  250. }
  251. $this->transports[$transportType] = $di->get($transportClass);
  252. }
  253. } else {
  254. throw new Exception\InvalidArgumentException(sprintf(
  255. '%s expects a string or array as transport config, "%s" received',
  256. __METHOD__,
  257. gettype($config['transports'])
  258. ));
  259. }
  260. }
  261. }