|
1 | 1 | <?php
|
| 2 | + |
2 | 3 | declare(strict_types=1);
|
3 | 4 |
|
4 | 5 | namespace SimonSchaufi\LaravelDKIM;
|
5 | 6 |
|
| 7 | +use Illuminate\Contracts\Mail\Mailable as MailableContract; |
| 8 | +use Illuminate\Mail\SentMessage; |
6 | 9 | use InvalidArgumentException;
|
7 |
| -use SimonSchaufi\LaravelDKIM\Exception\MissingConfigurationException; |
8 |
| -use Swift_SwiftException; |
| 10 | +use Symfony\Component\Mime\Crypto\DkimSigner; |
9 | 11 |
|
10 | 12 | class Mailer extends \Illuminate\Mail\Mailer
|
11 | 13 | {
|
12 | 14 | /**
|
13 |
| - * Create a new message instance. |
| 15 | + * Send a new message using a view. |
14 | 16 | *
|
15 |
| - * @return Message |
16 |
| - * @throws MissingConfigurationException |
17 |
| - * @throws Swift_SwiftException |
| 17 | + * @param MailableContract|string|array $view |
| 18 | + * @param array $data |
| 19 | + * @param \Closure|string|null $callback |
| 20 | + * @return SentMessage|null |
18 | 21 | */
|
19 |
| - protected function createMessage() |
| 22 | + public function send($view, array $data = [], $callback = null): ?SentMessage |
20 | 23 | {
|
21 |
| - $message = new Message($this->swift->createMessage('message')); |
22 |
| - |
23 |
| - // If a global from address has been specified we will set it on every message |
24 |
| - // instances so the developer does not have to repeat themselves every time |
25 |
| - // they create a new message. We will just go ahead and push the address. |
26 |
| - if (! empty($this->from['address'])) { |
27 |
| - $message->from($this->from['address'], $this->from['name']); |
| 24 | + if ($view instanceof MailableContract) { |
| 25 | + return $this->sendMailable($view); |
28 | 26 | }
|
29 | 27 |
|
30 |
| - // When a global reply address was specified we will set this on every message |
31 |
| - // instance so the developer does not have to repeat themselves every time |
32 |
| - // they create a new message. We will just go ahead and push this address. |
33 |
| - if (! empty($this->replyTo['address'])) { |
34 |
| - $message->replyTo($this->replyTo['address'], $this->replyTo['name']); |
| 28 | + // First we need to parse the view, which could either be a string or an array |
| 29 | + // containing both an HTML and plain text versions of the view which should |
| 30 | + // be used when sending an e-mail. We will extract both of them out here. |
| 31 | + [$view, $plain, $raw] = $this->parseView($view); |
| 32 | + |
| 33 | + $data['message'] = $message = $this->createMessage(); |
| 34 | + |
| 35 | + // Once we have retrieved the view content for the e-mail we will set the body |
| 36 | + // of this message using the HTML type, which will provide a simple wrapper |
| 37 | + // to creating view based emails that are able to receive arrays of data. |
| 38 | + if (! is_null($callback)) { |
| 39 | + $callback($message); |
35 | 40 | }
|
36 | 41 |
|
37 |
| - if (! empty($this->returnPath['address'])) { |
38 |
| - $message->returnPath($this->returnPath['address']); |
| 42 | + $this->addContent($message, $view, $plain, $raw, $data); |
| 43 | + |
| 44 | + // If a global "to" address has been set, we will set that address on the mail |
| 45 | + // message. This is primarily useful during local development in which each |
| 46 | + // message should be delivered into a single mail address for inspection. |
| 47 | + if (isset($this->to['address'])) { |
| 48 | + $this->setGlobalToAndRemoveCcAndBcc($message); |
39 | 49 | }
|
40 | 50 |
|
| 51 | + // Next we will determine if the message should be sent. We give the developer |
| 52 | + // one final chance to stop this message and then we will send it to all of |
| 53 | + // its recipients. We will then fire the sent event for the sent message. |
| 54 | + $symfonyMessage = $message->getSymfonyMessage(); |
| 55 | + |
41 | 56 | // PATCH START
|
42 | 57 | $privateKey = config('dkim.private_key');
|
43 | 58 | $selector = config('dkim.selector');
|
44 | 59 | $domain = config('dkim.domain');
|
45 | 60 | if (in_array(strtolower(config('mail.default')), ['smtp', 'sendmail', 'log'])) {
|
46 | 61 | if (empty($privateKey)) {
|
47 |
| - throw new MissingConfigurationException('No private key set.', 1588115551); |
| 62 | + throw new InvalidArgumentException('No private key set.', 1588115551); |
48 | 63 | }
|
49 | 64 | if (!file_exists($privateKey)) {
|
50 | 65 | throw new InvalidArgumentException('Private key file does not exist.', 1588115609);
|
51 | 66 | }
|
52 | 67 |
|
53 | 68 | if (empty($selector)) {
|
54 |
| - throw new MissingConfigurationException('No selector set.', 1588115373); |
| 69 | + throw new InvalidArgumentException('No selector set.', 1588115373); |
55 | 70 | }
|
56 | 71 | if (empty($domain)) {
|
57 |
| - throw new MissingConfigurationException('No domain set.', 1588115434); |
| 72 | + throw new InvalidArgumentException('No domain set.', 1588115434); |
58 | 73 | }
|
59 | 74 |
|
60 |
| - $message->attachDKIMSigner( |
61 |
| - file_get_contents($privateKey), |
62 |
| - $domain, |
63 |
| - $selector, |
64 |
| - config('dkim.passphrase') |
65 |
| - ); |
| 75 | + $signer = new DkimSigner(file_get_contents($privateKey), $domain, $selector, [], config('dkim.passphrase')); |
| 76 | + $signedEmail = $signer->sign($message->getSymfonyMessage()); |
| 77 | + $symfonyMessage->setHeaders($signedEmail->getHeaders()); |
66 | 78 | }
|
67 | 79 | // PATCH END
|
68 | 80 |
|
69 |
| - return $message; |
| 81 | + if ($this->shouldSendMessage($symfonyMessage, $data)) { |
| 82 | + $symfonySentMessage = $this->sendSymfonyMessage($symfonyMessage); |
| 83 | + |
| 84 | + if ($symfonySentMessage) { |
| 85 | + $sentMessage = new SentMessage($symfonySentMessage); |
| 86 | + |
| 87 | + $this->dispatchSentEvent($sentMessage, $data); |
| 88 | + |
| 89 | + return $sentMessage; |
| 90 | + } |
| 91 | + } |
70 | 92 | }
|
71 | 93 | }
|
0 commit comments