|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Clarification\MailDrivers\Sparkpost\Transport; |
| 4 | + |
| 5 | +use Swift_Mime_Message; |
| 6 | +use GuzzleHttp\ClientInterface; |
| 7 | +use Illuminate\Mail\Transport\Transport; |
| 8 | + |
| 9 | +class SparkpostTransport extends Transport |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Guzzle client instance. |
| 13 | + * |
| 14 | + * @var \GuzzleHttp\ClientInterface |
| 15 | + */ |
| 16 | + protected $client; |
| 17 | + |
| 18 | + /** |
| 19 | + * The SparkPost API key. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $key; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a new SparkPost transport instance. |
| 27 | + * |
| 28 | + * @param \GuzzleHttp\ClientInterface $client |
| 29 | + * @param string $key |
| 30 | + */ |
| 31 | + public function __construct(ClientInterface $client, $key) |
| 32 | + { |
| 33 | + $this->client = $client; |
| 34 | + $this->key = $key; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritdoc} |
| 39 | + */ |
| 40 | + public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
| 41 | + { |
| 42 | + $this->beforeSendPerformed($message); |
| 43 | + $recipients = $this->getRecipients($message); |
| 44 | + $message->setBcc([]); |
| 45 | + $options = [ |
| 46 | + 'headers' => [ |
| 47 | + 'Authorization' => $this->key, |
| 48 | + ], |
| 49 | + 'json' => [ |
| 50 | + 'recipients' => $recipients, |
| 51 | + 'content' => [ |
| 52 | + 'html' => $message->getBody(), |
| 53 | + 'from' => $this->getFrom($message), |
| 54 | + 'subject' => $message->getSubject(), |
| 55 | + ], |
| 56 | + ], |
| 57 | + ]; |
| 58 | + return $this->client->post('https://api.sparkpost.com/api/v1/transmissions', $options); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get all the addresses this message should be sent to. |
| 63 | + * |
| 64 | + * Note that SparkPost still respects CC, BCC headers in raw message itself. |
| 65 | + * |
| 66 | + * @param \Swift_Mime_Message $message |
| 67 | + * @return array |
| 68 | + */ |
| 69 | + protected function getRecipients(Swift_Mime_Message $message) |
| 70 | + { |
| 71 | + $to = $bcc = []; |
| 72 | + if ($message->getTo()) { |
| 73 | + $to = array_merge($to, array_keys($message->getTo())); |
| 74 | + } |
| 75 | + if ($message->getCc()) { |
| 76 | + $to = array_merge($to, array_keys($message->getCc())); |
| 77 | + } |
| 78 | + if ($message->getBcc()) { |
| 79 | + $to = array_merge($bcc, array_keys($message->getBcc())); |
| 80 | + } |
| 81 | + $recipients = array_map(function ($address) { |
| 82 | + return ['address' => ['email' => $address, 'header_to' => $address]]; |
| 83 | + }, $to); |
| 84 | + return $recipients; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the "from" contacts in the format required by SparkPost. |
| 89 | + * |
| 90 | + * @param Swift_Mime_Message $message |
| 91 | + * @return array |
| 92 | + */ |
| 93 | + protected function getFrom(Swift_Mime_Message $message) |
| 94 | + { |
| 95 | + return array_map(function ($email, $name) { |
| 96 | + return compact('name', 'email'); |
| 97 | + }, array_keys($message->getFrom()), $message->getFrom())[0]; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get the API key being used by the transport. |
| 102 | + * |
| 103 | + * @return string |
| 104 | + */ |
| 105 | + public function getKey() |
| 106 | + { |
| 107 | + return $this->key; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Set the API key being used by the transport. |
| 112 | + * |
| 113 | + * @param string $key |
| 114 | + * @return string |
| 115 | + */ |
| 116 | + public function setKey($key) |
| 117 | + { |
| 118 | + return $this->key = $key; |
| 119 | + } |
| 120 | +} |
0 commit comments