PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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