1
|
|
|
<?php |
2
|
|
|
namespace Sichikawa\LaravelSendgridDriver\Transport; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\ClientInterface; |
5
|
|
|
use Illuminate\Mail\Transport\Transport; |
6
|
|
|
use Swift_Attachment; |
7
|
|
|
use Swift_Image; |
8
|
|
|
use Swift_Mime_Message; |
9
|
|
|
use Swift_MimePart; |
10
|
|
|
|
11
|
|
|
class SendgridTransport extends Transport |
12
|
|
|
{ |
13
|
|
|
const MAXIMUM_FILE_SIZE = 7340032; |
14
|
|
|
const SMTP_API_NAME = 'sendgrid/x-smtpapi'; |
15
|
|
|
|
16
|
|
|
private $client; |
17
|
|
|
private $options; |
18
|
|
|
|
19
|
|
|
public function __construct(ClientInterface $client, $api_key) |
20
|
|
|
{ |
21
|
|
|
$this->client = $client; |
22
|
|
|
$this->options = [ |
23
|
|
|
'headers' => ['Authorization' => 'Bearer ' . $api_key] |
24
|
|
|
]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
31
|
|
|
{ |
32
|
|
|
list($from, $fromName) = $this->getFromAddresses($message); |
33
|
|
|
$payload = $this->options; |
34
|
|
|
|
35
|
|
|
$data = [ |
36
|
|
|
'from' => $from, |
37
|
|
|
'fromname' => isset($fromName) ? $fromName : null, |
38
|
|
|
'subject' => $message->getSubject(), |
39
|
|
|
'html' => $message->getBody() |
40
|
|
|
]; |
41
|
|
|
$this->setTo($data, $message); |
42
|
|
|
$this->setCc($data, $message); |
43
|
|
|
$this->setBcc($data, $message); |
44
|
|
|
$this->setReplyTo($data, $message); |
45
|
|
|
$this->setText($data, $message); |
46
|
|
|
$this->setAttachment($data, $message); |
47
|
|
|
$this->setSmtpApi($data, $message); |
48
|
|
|
|
49
|
|
|
if (version_compare(ClientInterface::VERSION, '6') === 1) { |
50
|
|
|
$payload += ['form_params' => $data]; |
51
|
|
|
} else { |
52
|
|
|
$payload += ['body' => $data]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->client->post('https://api.sendgrid.com/api/mail.send.json', $payload); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $data |
60
|
|
|
* @param Swift_Mime_Message $message |
61
|
|
|
*/ |
62
|
|
|
protected function setTo(&$data, Swift_Mime_Message $message) |
63
|
|
|
{ |
64
|
|
|
if ($from = $message->getTo()) { |
65
|
|
|
$data['to'] = array_keys($from); |
66
|
|
|
$data['toname'] = array_values($from); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $data |
72
|
|
|
* @param Swift_Mime_Message $message |
73
|
|
|
*/ |
74
|
|
|
protected function setCc(&$data, Swift_Mime_Message $message) |
75
|
|
|
{ |
76
|
|
|
if ($cc = $message->getCc()) { |
77
|
|
|
$data['cc'] = array_keys($cc); |
78
|
|
|
$data['ccname'] = array_values($cc); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $data |
84
|
|
|
* @param Swift_Mime_Message $message |
85
|
|
|
*/ |
86
|
|
|
protected function setBcc(&$data, Swift_Mime_Message $message) |
87
|
|
|
{ |
88
|
|
|
if ($bcc = $message->getBcc()) { |
89
|
|
|
$data['bcc'] = array_keys($bcc); |
90
|
|
|
$data['bccname'] = array_values($bcc); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $data |
96
|
|
|
* @param Swift_Mime_Message $message |
97
|
|
|
*/ |
98
|
|
|
protected function setReplyTo(&$data, Swift_Mime_Message $message) |
99
|
|
|
{ |
100
|
|
|
if ($replyTo = $message->getReplyTo()) { |
101
|
|
|
$data['replyto'] = key($replyTo); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get From Addresses. |
107
|
|
|
* |
108
|
|
|
* @param Swift_Mime_Message $message |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
protected function getFromAddresses(Swift_Mime_Message $message) |
112
|
|
|
{ |
113
|
|
|
if ($message->getFrom()) { |
114
|
|
|
foreach ($message->getFrom() as $address => $name) { |
115
|
|
|
return [$address, $name]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return []; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set text contents. |
123
|
|
|
* |
124
|
|
|
* @param $data |
125
|
|
|
* @param Swift_Mime_Message $message |
126
|
|
|
*/ |
127
|
|
|
protected function setText(&$data, Swift_Mime_Message $message) |
128
|
|
|
{ |
129
|
|
|
foreach ($message->getChildren() as $attachment) { |
130
|
|
|
if (!$attachment instanceof Swift_MimePart) { |
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
$data['text'] = $attachment->getBody(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set Attachment Files. |
139
|
|
|
* |
140
|
|
|
* @param $data |
141
|
|
|
* @param Swift_Mime_Message $message |
142
|
|
|
*/ |
143
|
|
|
protected function setAttachment(&$data, Swift_Mime_Message $message) |
144
|
|
|
{ |
145
|
|
|
foreach ($message->getChildren() as $attachment) { |
146
|
|
|
if (!$attachment instanceof Swift_Attachment || !strlen($attachment->getBody()) > self::MAXIMUM_FILE_SIZE) { |
147
|
|
|
continue; |
148
|
|
|
} |
149
|
|
|
$handler = tmpfile(); |
150
|
|
|
fwrite($handler, $attachment->getBody()); |
151
|
|
|
$data['files[' . $attachment->getFilename() . ']'] = $handler; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set Sendgrid SMTP API |
157
|
|
|
* |
158
|
|
|
* @param $data |
159
|
|
|
* @param Swift_Mime_Message $message |
160
|
|
|
*/ |
161
|
|
|
protected function setSmtpApi(&$data, Swift_Mime_Message $message) |
162
|
|
|
{ |
163
|
|
View Code Duplication |
foreach ($message->getChildren() as $attachment) { |
|
|
|
|
164
|
|
|
if (!$attachment instanceof Swift_Image |
165
|
|
|
|| !in_array(self::SMTP_API_NAME, [$attachment->getFilename(), $attachment->getContentType()]) |
166
|
|
|
) { |
167
|
|
|
continue; |
168
|
|
|
} |
169
|
|
|
$data['x-smtpapi'] = json_encode($attachment->getBody()); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.