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

https://gitlab.com/rmoshiur81/Larave-Grading · PHP · 525 lines · 226 code · 66 blank · 233 comment · 18 complexity · b27008446771443b2b12a3ab83202a8c MD5 · raw file

  1. <?php
  2. namespace Illuminate\Mail;
  3. use ReflectionClass;
  4. use ReflectionProperty;
  5. use BadMethodCallException;
  6. use Illuminate\Support\Str;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Container\Container;
  9. use Illuminate\Contracts\Queue\Factory as Queue;
  10. use Illuminate\Contracts\Mail\Mailer as MailerContract;
  11. use Illuminate\Contracts\Mail\Mailable as MailableContract;
  12. class Mailable implements MailableContract
  13. {
  14. /**
  15. * The person the message is from.
  16. *
  17. * @var array
  18. */
  19. protected $from = [];
  20. /**
  21. * The "to" recipients of the message.
  22. *
  23. * @var array
  24. */
  25. protected $to = [];
  26. /**
  27. * The "cc" recipients of the message.
  28. *
  29. * @var array
  30. */
  31. protected $cc = [];
  32. /**
  33. * The "bcc" recipients of the message.
  34. *
  35. * @var array
  36. */
  37. protected $bcc = [];
  38. /**
  39. * The "reply to" recipients of the message.
  40. *
  41. * @var array
  42. */
  43. protected $replyTo = [];
  44. /**
  45. * The subject of the message.
  46. *
  47. * @var string
  48. */
  49. protected $subject;
  50. /**
  51. * The view to use for the message.
  52. *
  53. * @var string
  54. */
  55. protected $view;
  56. /**
  57. * The plain text view to use for the message.
  58. *
  59. * @var string
  60. */
  61. protected $textView;
  62. /**
  63. * The view data for the message.
  64. *
  65. * @var array
  66. */
  67. protected $viewData = [];
  68. /**
  69. * The attachments for the message.
  70. *
  71. * @var array
  72. */
  73. protected $attachments = [];
  74. /**
  75. * The raw attachments for the message.
  76. *
  77. * @var array
  78. */
  79. protected $rawAttachments = [];
  80. /**
  81. * The callbacks for the message.
  82. *
  83. * @var array
  84. */
  85. protected $callbacks = [];
  86. /**
  87. * Send the message using the given mailer.
  88. *
  89. * @param \Illuminate\Contracts\Mail\Mailer $mailer
  90. * @return void
  91. */
  92. public function send(MailerContract $mailer)
  93. {
  94. Container::getInstance()->call([$this, 'build']);
  95. $mailer->send($this->buildView(), $this->buildViewData(), function ($message) {
  96. $this->buildFrom($message)
  97. ->buildRecipients($message)
  98. ->buildSubject($message)
  99. ->buildAttachments($message)
  100. ->runCallbacks($message);
  101. });
  102. }
  103. /**
  104. * Queue the message for sending.
  105. *
  106. * @param \Illuminate\Contracts\Queue\Factory $queue
  107. * @return mixed
  108. */
  109. public function queue(Queue $queue)
  110. {
  111. $connection = property_exists($this, 'connection') ? $this->connection : null;
  112. $queueName = property_exists($this, 'queue') ? $this->queue : null;
  113. if ($queueName) {
  114. return $queue->connection($connection)->pushOn(
  115. $queueName, new SendQueuedMailable($this)
  116. );
  117. } else {
  118. return $queue->connection($connection)->push(
  119. new SendQueuedMailable($this)
  120. );
  121. }
  122. }
  123. /**
  124. * Deliver the queued message after the given delay.
  125. *
  126. * @param \DateTime|int $delay
  127. * @param Queue $queue
  128. * @return mixed
  129. */
  130. public function later($delay, Queue $queue)
  131. {
  132. $connection = property_exists($this, 'connection') ? $this->connection : null;
  133. $queueName = property_exists($this, 'queue') ? $this->queue : null;
  134. if ($queueName) {
  135. return $queue->connection($connection)->laterOn(
  136. $queueName, $delay, new SendQueuedMailable($this)
  137. );
  138. } else {
  139. return $queue->connection($connection)->later(
  140. $delay, new SendQueuedMailable($this)
  141. );
  142. }
  143. }
  144. /**
  145. * Build the view for the message.
  146. *
  147. * @return array|string
  148. */
  149. protected function buildView()
  150. {
  151. if (isset($this->view, $this->textView)) {
  152. return [$this->view, $this->textView];
  153. } elseif (isset($this->textView)) {
  154. return ['text' => $this->textView];
  155. } else {
  156. return $this->view;
  157. }
  158. }
  159. /**
  160. * Build the view data for the message.
  161. *
  162. * @return array
  163. */
  164. protected function buildViewData()
  165. {
  166. $data = $this->viewData;
  167. foreach ((new ReflectionClass($this))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
  168. $data[$property->getName()] = $property->getValue($this);
  169. }
  170. return $data;
  171. }
  172. /**
  173. * Add the sender to the message.
  174. *
  175. * @param \Illuminate\Mail\Message $message
  176. * @return $this
  177. */
  178. protected function buildFrom($message)
  179. {
  180. if (! empty($this->from)) {
  181. $message->from($this->from[0]['address'], $this->from[0]['name']);
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Add all of the recipients to the message.
  187. *
  188. * @param \Illuminate\Mail\Message $message
  189. * @return $this
  190. */
  191. protected function buildRecipients($message)
  192. {
  193. foreach (['to', 'cc', 'bcc', 'replyTo'] as $type) {
  194. foreach ($this->{$type} as $recipient) {
  195. $message->{$type}($recipient['address'], $recipient['name']);
  196. }
  197. }
  198. return $this;
  199. }
  200. /**
  201. * Set the subject for the message.
  202. *
  203. * @param \Illuminate\Mail\Message $message
  204. * @return $this
  205. */
  206. protected function buildSubject($message)
  207. {
  208. if ($this->subject) {
  209. $message->subject($this->subject);
  210. } else {
  211. $message->subject(Str::title(Str::snake(class_basename($this), ' ')));
  212. }
  213. return $this;
  214. }
  215. /**
  216. * Add all of the attachments to the message.
  217. *
  218. * @param \Illuminate\Mail\Message $message
  219. * @return $this
  220. */
  221. protected function buildAttachments($message)
  222. {
  223. foreach ($this->attachments as $attachment) {
  224. $message->attach($attachment['file'], $attachment['options']);
  225. }
  226. foreach ($this->rawAttachments as $attachment) {
  227. $message->attachData(
  228. $attachment['data'], $attachment['name'], $attachment['options']
  229. );
  230. }
  231. return $this;
  232. }
  233. /**
  234. * Run the callbacks for the message.
  235. *
  236. * @param \Illuminate\Mail\Message $message
  237. * @return $this
  238. */
  239. protected function runCallbacks($message)
  240. {
  241. foreach ($this->callbacks as $callback) {
  242. $callback($message->getSwiftMessage());
  243. }
  244. return $this;
  245. }
  246. /**
  247. * Set the priority of this message.
  248. *
  249. * The value is an integer where 1 is the highest priority and 5 is the lowest.
  250. *
  251. * @param int $level
  252. * @return $this
  253. */
  254. public function priority($level = 3)
  255. {
  256. $this->callbacks[] = function ($message) use ($level) {
  257. $message->setPriority($level);
  258. };
  259. return $this;
  260. }
  261. /**
  262. * Set the sender of the message.
  263. *
  264. * @param object|array|string $address
  265. * @param string|null $name
  266. * @return $this
  267. */
  268. public function from($address, $name = null)
  269. {
  270. return $this->setAddress($address, $name, 'from');
  271. }
  272. /**
  273. * Set the recipients of the message.
  274. *
  275. * @param object|array|string $address
  276. * @param string|null $name
  277. * @return $this
  278. */
  279. public function to($address, $name = null)
  280. {
  281. return $this->setAddress($address, $name, 'to');
  282. }
  283. /**
  284. * Set the recipients of the message.
  285. *
  286. * @param object|array|string $address
  287. * @param string|null $name
  288. * @return $this
  289. */
  290. public function cc($address, $name = null)
  291. {
  292. return $this->setAddress($address, $name, 'cc');
  293. }
  294. /**
  295. * Set the recipients of the message.
  296. *
  297. * @param object|array|string $address
  298. * @param string|null $name
  299. * @return $this
  300. */
  301. public function bcc($address, $name = null)
  302. {
  303. return $this->setAddress($address, $name, 'bcc');
  304. }
  305. /**
  306. * Set the "reply to" address of the message.
  307. *
  308. * @param object|array|string $address
  309. * @param string|null $name
  310. * @return $this
  311. */
  312. public function replyTo($address, $name = null)
  313. {
  314. return $this->setAddress($address, $name, 'replyTo');
  315. }
  316. /**
  317. * Set the recipients of the message.
  318. *
  319. * @param object|array|string $address
  320. * @param string|null $name
  321. * @param string $property
  322. * @return $this
  323. */
  324. protected function setAddress($address, $name = null, $property = 'to')
  325. {
  326. if (is_object($address) && ! $address instanceof Collection) {
  327. $address = [$address];
  328. }
  329. if ($address instanceof Collection || is_array($address)) {
  330. foreach ($address as $user) {
  331. $user = $this->parseUser($user);
  332. $this->{$property}($user->email, isset($user->name) ? $user->name : null);
  333. }
  334. } else {
  335. $this->{$property}[] = compact('address', 'name');
  336. }
  337. return $this;
  338. }
  339. /**
  340. * Parse the given user into an object.
  341. *
  342. * @param mixed $user
  343. * @return object
  344. */
  345. protected function parseUser($user)
  346. {
  347. if (is_array($user)) {
  348. return (object) $user;
  349. } elseif (is_string($user)) {
  350. return (object) ['email' => $user];
  351. }
  352. return $user;
  353. }
  354. /**
  355. * Set the subject of the message.
  356. *
  357. * @param string $subject
  358. * @return $this
  359. */
  360. public function subject($subject)
  361. {
  362. $this->subject = $subject;
  363. return $this;
  364. }
  365. /**
  366. * Set the view and view data for the message.
  367. *
  368. * @param string $view
  369. * @param array $data
  370. * @return $this
  371. */
  372. public function view($view, array $data = [])
  373. {
  374. $this->view = $view;
  375. $this->viewData = $data;
  376. return $this;
  377. }
  378. /**
  379. * Set the plain text view for the message.
  380. *
  381. * @param string $textView
  382. * @param array $data
  383. * @return $this
  384. */
  385. public function text($textView, array $data = [])
  386. {
  387. $this->textView = $textView;
  388. $this->viewData = $data;
  389. return $this;
  390. }
  391. /**
  392. * Set the view data for the message.
  393. *
  394. * @param string|array $key
  395. * @param mixed $value
  396. * @return $this
  397. */
  398. public function with($key, $value = null)
  399. {
  400. if (is_array($key)) {
  401. $this->viewData = array_merge($this->viewData, $key);
  402. } else {
  403. $this->viewData[$key] = $value;
  404. }
  405. return $this;
  406. }
  407. /**
  408. * Attach a file to the message.
  409. *
  410. * @param string $file
  411. * @param array $options
  412. * @return $this
  413. */
  414. public function attach($file, array $options = [])
  415. {
  416. $this->attachments[] = compact('file', 'options');
  417. return $this;
  418. }
  419. /**
  420. * Attach in-memory data as an attachment.
  421. *
  422. * @param string $data
  423. * @param string $name
  424. * @param array $options
  425. * @return $this
  426. */
  427. public function attachData($data, $name, array $options = [])
  428. {
  429. $this->rawAttachments[] = compact('data', 'name', 'options');
  430. return $this;
  431. }
  432. /**
  433. * Register a callback to be called with the Swift message instance.
  434. *
  435. * @param callable $callback
  436. * @return $this
  437. */
  438. public function withSwiftMessage($callback)
  439. {
  440. $this->callbacks[] = $callback;
  441. return $this;
  442. }
  443. /**
  444. * Dynamically bind parameters to the message.
  445. *
  446. * @param string $method
  447. * @param array $parameters
  448. * @return $this
  449. *
  450. * @throws \BadMethodCallException
  451. */
  452. public function __call($method, $parameters)
  453. {
  454. if (Str::startsWith($method, 'with')) {
  455. return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
  456. }
  457. throw new BadMethodCallException("Method [$method] does not exist on mailable.");
  458. }
  459. }