PageRenderTime 21ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 504 lines | 190 code | 61 blank | 253 comment | 11 complexity | 41a76bb1af1e95179069a0bbaab4b95f MD5 | raw file
  1. <?php namespace Illuminate\Mail;
  2. use Closure;
  3. use Swift_Mailer;
  4. use Swift_Message;
  5. use Illuminate\Log\Writer;
  6. use Illuminate\View\Factory;
  7. use Illuminate\Events\Dispatcher;
  8. use Illuminate\Queue\QueueManager;
  9. use Illuminate\Container\Container;
  10. use Illuminate\Support\SerializableClosure;
  11. class Mailer {
  12. /**
  13. * The view factory instance.
  14. *
  15. * @var \Illuminate\View\Factory
  16. */
  17. protected $views;
  18. /**
  19. * The Swift Mailer instance.
  20. *
  21. * @var \Swift_Mailer
  22. */
  23. protected $swift;
  24. /**
  25. * The event dispatcher instance.
  26. *
  27. * @var \Illuminate\Events\Dispatcher
  28. */
  29. protected $events;
  30. /**
  31. * The global from address and name.
  32. *
  33. * @var array
  34. */
  35. protected $from;
  36. /**
  37. * The log writer instance.
  38. *
  39. * @var \Illuminate\Log\Writer
  40. */
  41. protected $logger;
  42. /**
  43. * The IoC container instance.
  44. *
  45. * @var \Illuminate\Container\Container
  46. */
  47. protected $container;
  48. /*
  49. * The QueueManager instance.
  50. *
  51. * @var \Illuminate\Queue\QueueManager
  52. */
  53. protected $queue;
  54. /**
  55. * Indicates if the actual sending is disabled.
  56. *
  57. * @var bool
  58. */
  59. protected $pretending = false;
  60. /**
  61. * Array of failed recipients.
  62. *
  63. * @var array
  64. */
  65. protected $failedRecipients = array();
  66. /**
  67. * Array of parsed views containing html and text view name.
  68. *
  69. * @var array
  70. */
  71. protected $parsedViews = array();
  72. /**
  73. * Create a new Mailer instance.
  74. *
  75. * @param \Illuminate\View\Factory $views
  76. * @param \Swift_Mailer $swift
  77. * @param \Illuminate\Events\Dispatcher $events
  78. * @return void
  79. */
  80. public function __construct(Factory $views, Swift_Mailer $swift, Dispatcher $events = null)
  81. {
  82. $this->views = $views;
  83. $this->swift = $swift;
  84. $this->events = $events;
  85. }
  86. /**
  87. * Set the global from address and name.
  88. *
  89. * @param string $address
  90. * @param string $name
  91. * @return void
  92. */
  93. public function alwaysFrom($address, $name = null)
  94. {
  95. $this->from = compact('address', 'name');
  96. }
  97. /**
  98. * Send a new message when only a plain part.
  99. *
  100. * @param string $view
  101. * @param array $data
  102. * @param mixed $callback
  103. * @return int
  104. */
  105. public function plain($view, array $data, $callback)
  106. {
  107. return $this->send(array('text' => $view), $data, $callback);
  108. }
  109. /**
  110. * Send a new message using a view.
  111. *
  112. * @param string|array $view
  113. * @param array $data
  114. * @param \Closure|string $callback
  115. * @return void
  116. */
  117. public function send($view, array $data, $callback)
  118. {
  119. // First we need to parse the view, which could either be a string or an array
  120. // containing both an HTML and plain text versions of the view which should
  121. // be used when sending an e-mail. We will extract both of them out here.
  122. list($view, $plain) = $this->parseView($view);
  123. $data['message'] = $message = $this->createMessage();
  124. $this->callMessageBuilder($callback, $message);
  125. // Once we have retrieved the view content for the e-mail we will set the body
  126. // of this message using the HTML type, which will provide a simple wrapper
  127. // to creating view based emails that are able to receive arrays of data.
  128. $this->addContent($message, $view, $plain, $data);
  129. $message = $message->getSwiftMessage();
  130. $this->sendSwiftMessage($message);
  131. }
  132. /**
  133. * Queue a new e-mail message for sending.
  134. *
  135. * @param string|array $view
  136. * @param array $data
  137. * @param \Closure|string $callback
  138. * @param string $queue
  139. * @return mixed
  140. */
  141. public function queue($view, array $data, $callback, $queue = null)
  142. {
  143. $callback = $this->buildQueueCallable($callback);
  144. return $this->queue->push('mailer@handleQueuedMessage', compact('view', 'data', 'callback'), $queue);
  145. }
  146. /**
  147. * Queue a new e-mail message for sending on the given queue.
  148. *
  149. * @param string $queue
  150. * @param string|array $view
  151. * @param array $data
  152. * @param \Closure|string $callback
  153. * @return mixed
  154. */
  155. public function queueOn($queue, $view, array $data, $callback)
  156. {
  157. return $this->queue($view, $data, $callback, $queue);
  158. }
  159. /**
  160. * Queue a new e-mail message for sending after (n) seconds.
  161. *
  162. * @param int $delay
  163. * @param string|array $view
  164. * @param array $data
  165. * @param \Closure|string $callback
  166. * @param string $queue
  167. * @return mixed
  168. */
  169. public function later($delay, $view, array $data, $callback, $queue = null)
  170. {
  171. $callback = $this->buildQueueCallable($callback);
  172. return $this->queue->later($delay, 'mailer@handleQueuedMessage', compact('view', 'data', 'callback'), $queue);
  173. }
  174. /**
  175. * Queue a new e-mail message for sending after (n) seconds on the given queue.
  176. *
  177. * @param string $queue
  178. * @param int $delay
  179. * @param string|array $view
  180. * @param array $data
  181. * @param \Closure|string $callback
  182. * @return mixed
  183. */
  184. public function laterOn($queue, $delay, $view, array $data, $callback)
  185. {
  186. return $this->later($delay, $view, $data, $callback, $queue);
  187. }
  188. /**
  189. * Build the callable for a queued e-mail job.
  190. *
  191. * @param mixed $callback
  192. * @return mixed
  193. */
  194. protected function buildQueueCallable($callback)
  195. {
  196. if ( ! $callback instanceof Closure) return $callback;
  197. return serialize(new SerializableClosure($callback));
  198. }
  199. /**
  200. * Handle a queued e-mail message job.
  201. *
  202. * @param \Illuminate\Queue\Jobs\Job $job
  203. * @param array $data
  204. * @return void
  205. */
  206. public function handleQueuedMessage($job, $data)
  207. {
  208. $this->send($data['view'], $data['data'], $this->getQueuedCallable($data));
  209. $job->delete();
  210. }
  211. /**
  212. * Get the true callable for a queued e-mail message.
  213. *
  214. * @param array $data
  215. * @return mixed
  216. */
  217. protected function getQueuedCallable(array $data)
  218. {
  219. if (str_contains($data['callback'], 'SerializableClosure'))
  220. {
  221. return with(unserialize($data['callback']))->getClosure();
  222. }
  223. return $data['callback'];
  224. }
  225. /**
  226. * Add the content to a given message.
  227. *
  228. * @param \Illuminate\Mail\Message $message
  229. * @param string $view
  230. * @param string $plain
  231. * @param array $data
  232. * @return void
  233. */
  234. protected function addContent($message, $view, $plain, $data)
  235. {
  236. if (isset($view))
  237. {
  238. $message->setBody($this->getView($view, $data), 'text/html');
  239. }
  240. if (isset($plain))
  241. {
  242. $message->addPart($this->getView($plain, $data), 'text/plain');
  243. }
  244. }
  245. /**
  246. * Parse the given view name or array.
  247. *
  248. * @param string|array $view
  249. * @return array
  250. *
  251. * @throws \InvalidArgumentException
  252. */
  253. protected function parseView($view)
  254. {
  255. if (is_string($view)) return array($view, null);
  256. // If the given view is an array with numeric keys, we will just assume that
  257. // both a "pretty" and "plain" view were provided, so we will return this
  258. // array as is, since must should contain both views with numeric keys.
  259. if (is_array($view) && isset($view[0]))
  260. {
  261. return $view;
  262. }
  263. // If the view is an array, but doesn't contain numeric keys, we will assume
  264. // the the views are being explicitly specified and will extract them via
  265. // named keys instead, allowing the developers to use one or the other.
  266. elseif (is_array($view))
  267. {
  268. return array(
  269. array_get($view, 'html'), array_get($view, 'text')
  270. );
  271. }
  272. throw new \InvalidArgumentException("Invalid view.");
  273. }
  274. /**
  275. * Send a Swift Message instance.
  276. *
  277. * @param \Swift_Message $message
  278. * @return void
  279. */
  280. protected function sendSwiftMessage($message)
  281. {
  282. if ($this->events)
  283. {
  284. $this->events->fire('mailer.sending', array($message));
  285. }
  286. if ( ! $this->pretending)
  287. {
  288. $this->swift->send($message, $this->failedRecipients);
  289. }
  290. elseif (isset($this->logger))
  291. {
  292. $this->logMessage($message);
  293. }
  294. }
  295. /**
  296. * Log that a message was sent.
  297. *
  298. * @param \Swift_Message $message
  299. * @return void
  300. */
  301. protected function logMessage($message)
  302. {
  303. $emails = implode(', ', array_keys((array) $message->getTo()));
  304. $this->logger->info("Pretending to mail message to: {$emails}");
  305. }
  306. /**
  307. * Call the provided message builder.
  308. *
  309. * @param \Closure|string $callback
  310. * @param \Illuminate\Mail\Message $message
  311. * @return mixed
  312. *
  313. * @throws \InvalidArgumentException
  314. */
  315. protected function callMessageBuilder($callback, $message)
  316. {
  317. if ($callback instanceof Closure)
  318. {
  319. return call_user_func($callback, $message);
  320. }
  321. elseif (is_string($callback))
  322. {
  323. return $this->container[$callback]->mail($message);
  324. }
  325. throw new \InvalidArgumentException("Callback is not valid.");
  326. }
  327. /**
  328. * Create a new message instance.
  329. *
  330. * @return \Illuminate\Mail\Message
  331. */
  332. protected function createMessage()
  333. {
  334. $message = new Message(new Swift_Message);
  335. // If a global from address has been specified we will set it on every message
  336. // instances so the developer does not have to repeat themselves every time
  337. // they create a new message. We will just go ahead and push the address.
  338. if (isset($this->from['address']))
  339. {
  340. $message->from($this->from['address'], $this->from['name']);
  341. }
  342. return $message;
  343. }
  344. /**
  345. * Render the given view.
  346. *
  347. * @param string $view
  348. * @param array $data
  349. * @return \Illuminate\View\View
  350. */
  351. protected function getView($view, $data)
  352. {
  353. return $this->views->make($view, $data)->render();
  354. }
  355. /**
  356. * Tell the mailer to not really send messages.
  357. *
  358. * @param bool $value
  359. * @return void
  360. */
  361. public function pretend($value = true)
  362. {
  363. $this->pretending = $value;
  364. }
  365. /**
  366. * Check if the mailer is pretending to send messages.
  367. *
  368. * @return bool
  369. */
  370. public function isPretending()
  371. {
  372. return $this->pretending;
  373. }
  374. /**
  375. * Get the view factory instance.
  376. *
  377. * @return \Illuminate\View\Factory
  378. */
  379. public function getViewFactory()
  380. {
  381. return $this->views;
  382. }
  383. /**
  384. * Get the Swift Mailer instance.
  385. *
  386. * @return \Swift_Mailer
  387. */
  388. public function getSwiftMailer()
  389. {
  390. return $this->swift;
  391. }
  392. /**
  393. * Get the array of failed recipients.
  394. *
  395. * @return array
  396. */
  397. public function failures()
  398. {
  399. return $this->failedRecipients;
  400. }
  401. /**
  402. * Set the Swift Mailer instance.
  403. *
  404. * @param \Swift_Mailer $swift
  405. * @return void
  406. */
  407. public function setSwiftMailer($swift)
  408. {
  409. $this->swift = $swift;
  410. }
  411. /**
  412. * Set the log writer instance.
  413. *
  414. * @param \Illuminate\Log\Writer $logger
  415. * @return $this
  416. */
  417. public function setLogger(Writer $logger)
  418. {
  419. $this->logger = $logger;
  420. return $this;
  421. }
  422. /**
  423. * Set the queue manager instance.
  424. *
  425. * @param \Illuminate\Queue\QueueManager $queue
  426. * @return $this
  427. */
  428. public function setQueue(QueueManager $queue)
  429. {
  430. $this->queue = $queue;
  431. return $this;
  432. }
  433. /**
  434. * Set the IoC container instance.
  435. *
  436. * @param \Illuminate\Container\Container $container
  437. * @return void
  438. */
  439. public function setContainer(Container $container)
  440. {
  441. $this->container = $container;
  442. }
  443. }