1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnimail; |
4
|
|
|
|
5
|
|
|
use Omnimail\Common\AbstractMailer; |
6
|
|
|
use Omnimail\Exception\Exception; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use SimpleEmailServiceMessage; |
9
|
|
|
use SimpleEmailService; |
10
|
|
|
|
11
|
|
|
class AmazonSES extends AbstractMailer implements MailerInterface |
12
|
|
|
{ |
13
|
|
|
const AWS_US_EAST_1 = 'email.us-east-1.amazonaws.com'; |
14
|
|
|
const AWS_US_WEST_2 = 'email.us-west-2.amazonaws.com'; |
15
|
|
|
const AWS_EU_WEST1 = 'email.eu-west-1.amazonaws.com'; |
16
|
|
|
|
17
|
|
|
protected $accessKey; |
18
|
|
|
protected $secretKey; |
19
|
|
|
protected $host; |
20
|
|
|
protected $logger; |
21
|
|
|
protected $verifyPeer; |
22
|
|
|
protected $verifyHost; |
23
|
|
|
protected $signatureVersion; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return null|string |
27
|
|
|
*/ |
28
|
|
|
public function getAccessKey() |
29
|
|
|
{ |
30
|
|
|
return $this->accessKey; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param null|string $accessKey |
35
|
|
|
*/ |
36
|
1 |
|
public function setAccessKey($accessKey) |
37
|
|
|
{ |
38
|
1 |
|
$this->accessKey = $accessKey; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return null|string |
43
|
|
|
*/ |
44
|
|
|
public function getSecretKey() |
45
|
|
|
{ |
46
|
|
|
return $this->secretKey; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param null|string $secretKey |
51
|
|
|
*/ |
52
|
1 |
|
public function setSecretKey($secretKey) |
53
|
|
|
{ |
54
|
1 |
|
$this->secretKey = $secretKey; |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getHost() |
61
|
|
|
{ |
62
|
|
|
return $this->host; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $host |
67
|
|
|
*/ |
68
|
|
|
public function setHost($host) |
69
|
|
|
{ |
70
|
|
|
$this->host = $host; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return null|\Psr\Log\LoggerInterface |
75
|
|
|
*/ |
76
|
|
|
public function getLogger() |
77
|
|
|
{ |
78
|
|
|
return $this->logger; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param null|\Psr\Log\LoggerInterface $logger |
83
|
|
|
*/ |
84
|
|
|
public function setLogger($logger) |
85
|
|
|
{ |
86
|
|
|
$this->logger = $logger; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return boolean |
91
|
|
|
*/ |
92
|
|
|
public function isVerifyPeer() |
93
|
|
|
{ |
94
|
|
|
return $this->verifyPeer; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param boolean $verifyPeer |
99
|
|
|
*/ |
100
|
|
|
public function setVerifyPeer($verifyPeer) |
101
|
|
|
{ |
102
|
|
|
$this->verifyPeer = $verifyPeer; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return boolean |
107
|
|
|
*/ |
108
|
|
|
public function isVerifyHost() |
109
|
|
|
{ |
110
|
|
|
return $this->verifyHost; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param boolean $verifyHost |
115
|
|
|
*/ |
116
|
|
|
public function setVerifyHost($verifyHost) |
117
|
|
|
{ |
118
|
|
|
$this->verifyHost = $verifyHost; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param boolean $signatureVersion |
123
|
|
|
*/ |
124
|
|
|
public function setSignatureVersion($signatureVersion) |
125
|
|
|
{ |
126
|
|
|
$this->signatureVersion = $signatureVersion; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $accessKey |
131
|
|
|
* @param string $secretKey |
132
|
|
|
* @param string $host |
133
|
|
|
* @param bool $verifyPeer |
134
|
|
|
* @param bool $verifyHost |
135
|
|
|
* @param LoggerInterface|null $logger |
136
|
|
|
*/ |
137
|
1 |
|
public function __construct($accessKey = null, $secretKey = null, $host = self::AWS_US_EAST_1, $verifyPeer = true, $verifyHost = true, LoggerInterface $logger = null, string $signatureVersion = SimpleEmailService::REQUEST_SIGNATURE_V4) |
138
|
|
|
{ |
139
|
1 |
|
$this->verifyPeer = $verifyPeer; |
140
|
1 |
|
$this->verifyHost = $verifyHost; |
141
|
1 |
|
$this->accessKey = $accessKey; |
142
|
1 |
|
$this->secretKey = $secretKey; |
143
|
1 |
|
$this->host = $host; |
144
|
1 |
|
$this->logger = $logger; |
145
|
1 |
|
$this->signatureVersion = $signatureVersion; |
146
|
1 |
|
} |
147
|
|
|
|
148
|
1 |
|
public function send(EmailInterface $email) |
149
|
|
|
{ |
150
|
1 |
|
$m = new SimpleEmailServiceMessage(); |
151
|
1 |
|
$m->addTo($this->mapEmails($email->getTos())); |
152
|
1 |
|
$m->setFrom($this->mapEmail($email->getFrom())); |
153
|
|
|
|
154
|
1 |
|
if ($email->getReplyTos()) { |
155
|
|
|
$m->addReplyTo($this->mapEmails($email->getReplyTos())); |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
if ($email->getCcs()) { |
159
|
|
|
$m->addCC($this->mapEmails($email->getCcs())); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
if ($email->getBccs()) { |
163
|
|
|
$m->addBCC($this->mapEmails($email->getBccs())); |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
$m->setSubject($email->getSubject()); |
167
|
1 |
|
$m->setMessageFromString($email->getTextBody(), $email->getHtmlBody()); |
168
|
|
|
|
169
|
1 |
|
if ($email->getAttachments()) { |
170
|
|
|
foreach ($email->getAttachments() as $attachment) { |
171
|
|
|
if (!$attachment->getPath() && $attachment->getContent()) { |
|
|
|
|
172
|
|
|
$m->addAttachmentFromData( |
173
|
|
|
$attachment->getName(), |
174
|
|
|
$attachment->getContent(), |
175
|
|
|
$attachment->getMimeType(), |
176
|
|
|
$attachment->getContentId(), |
177
|
|
|
$attachment->getContentId() ? 'inline' : 'attachment' |
178
|
|
|
); |
179
|
|
|
} elseif ($attachment->getPath()) { |
|
|
|
|
180
|
|
|
$m->addAttachmentFromFile( |
181
|
|
|
$attachment->getName(), |
182
|
|
|
$attachment->getPath(), |
183
|
|
|
$attachment->getMimeType(), |
184
|
|
|
$attachment->getContentId(), |
185
|
|
|
$attachment->getContentId() ? 'inline' : 'attachment' |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
$ses = new SimpleEmailService($this->accessKey, $this->secretKey, $this->host, false, $this->signatureVersion); |
|
|
|
|
192
|
1 |
|
$ses->setVerifyPeer($this->verifyPeer); |
193
|
1 |
|
$ses->setVerifyHost($this->verifyHost); |
194
|
1 |
|
$response = $ses->sendEmail($m, false, false); |
195
|
|
|
|
196
|
1 |
|
if (is_object($response) && isset($response->error)) { |
197
|
1 |
|
$message = isset($response->error['Error']['Message']) ? |
198
|
1 |
|
$response->error['Error']['Message'] : $response->error['message']; |
199
|
|
|
|
200
|
1 |
|
if ($this->logger) { |
201
|
|
|
$this->logger->error("Error: ", $message); |
202
|
|
|
} |
203
|
1 |
|
throw new Exception("Error: ".$message, 603); |
204
|
|
|
} elseif (empty($response['MessageId'])) { |
205
|
|
|
if ($this->logger) { |
206
|
|
|
$this->logger->error("Email error: Unknown error", $email->toArray()); |
207
|
|
|
} |
208
|
|
|
throw new Exception('Unknown error', 603); |
209
|
|
|
} else { |
210
|
|
|
if ($this->logger) { |
211
|
|
|
$this->logger->info("Email sent: '{$email->getSubject()}'", $email->toArray()); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param array $emails |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
1 |
|
private function mapEmails(array $emails) |
221
|
|
|
{ |
222
|
1 |
|
$returnValue = []; |
223
|
1 |
|
foreach ($emails as $email) { |
224
|
1 |
|
$returnValue[] = $this->mapEmail($email); |
225
|
|
|
} |
226
|
1 |
|
return $returnValue; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param array $email |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
1 |
|
private function mapEmail(array $email) |
234
|
|
|
{ |
235
|
1 |
|
return !empty($email['name']) ? "'{$email['name']}' <{$email['email']}>" : $email['email']; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: