1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnimail; |
4
|
|
|
|
5
|
|
|
use Omnimail\Exception\Exception; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use SimpleEmailServiceMessage; |
8
|
|
|
use SimpleEmailService; |
9
|
|
|
|
10
|
|
|
class AmazonSES implements EmailSenderInterface |
11
|
|
|
{ |
12
|
|
|
const AWS_US_EAST_1 = 'email.us-east-1.amazonaws.com'; |
13
|
|
|
const AWS_US_WEST_2 = 'email.us-west-2.amazonaws.com'; |
14
|
|
|
const AWS_EU_WEST1 = 'email.eu-west-1.amazonaws.com'; |
15
|
|
|
|
16
|
|
|
private $accessKey; |
17
|
|
|
private $secretKey; |
18
|
|
|
private $host; |
19
|
|
|
private $logger; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $accessKey |
23
|
|
|
* @param string $secretKey |
24
|
|
|
* @param string $host |
25
|
|
|
* @param LoggerInterface|null $logger |
26
|
|
|
*/ |
27
|
|
|
public function __construct($accessKey, $secretKey, $host = self::AWS_US_EAST_1, LoggerInterface $logger = null) |
28
|
|
|
{ |
29
|
|
|
$this->accessKey = $accessKey; |
30
|
|
|
$this->secretKey = $secretKey; |
31
|
|
|
$this->host = $host; |
32
|
|
|
$this->logger = $logger; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function send(EmailInterface $email) |
36
|
|
|
{ |
37
|
|
|
$m = new SimpleEmailServiceMessage(); |
38
|
|
|
$m->addTo($this->mapEmails($email->getTo())); |
39
|
|
|
$m->setFrom($this->mapEmail($email->getFrom())); |
40
|
|
|
|
41
|
|
|
if ($email->getReplyTo()) { |
42
|
|
|
$m->addReplyTo($this->mapEmails($email->getReplyTo())); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($email->getCc()) { |
46
|
|
|
$m->addCC($this->mapEmails($email->getCc())); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($email->getBcc()) { |
50
|
|
|
$m->addBCC($this->mapEmails($email->getBcc())); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$m->setSubject($email->getSubject()); |
54
|
|
|
$m->setMessageFromString($email->getTextBody(), $email->getHtmlBody()); |
55
|
|
|
|
56
|
|
|
if ($email->getAttachements()) { |
57
|
|
|
foreach ($email->getAttachements() as $attachement) { |
58
|
|
|
if (!$attachement->getPath() && $attachement->getContent()) { |
59
|
|
|
$m->addAttachmentFromData( |
60
|
|
|
$attachement->getName(), |
61
|
|
|
$attachement->getContent(), |
62
|
|
|
$attachement->getMimeType() |
63
|
|
|
); |
64
|
|
|
} elseif ($attachement->getPath()) { |
65
|
|
|
$m->addAttachmentFromFile( |
66
|
|
|
$attachement->getName(), |
67
|
|
|
$attachement->getPath(), |
68
|
|
|
$attachement->getMimeType() |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$ses = new SimpleEmailService($this->accessKey, $this->secretKey, $this->host, false); |
75
|
|
|
$response = $ses->sendEmail($m, false, false); |
76
|
|
|
|
77
|
|
|
if (empty($response['MessageId'])) { |
78
|
|
|
if ($this->logger) { |
79
|
|
|
$this->logger->error("Email error: Unknown error", $email); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
throw new Exception('Unknown error', 603); |
82
|
|
|
} else { |
83
|
|
|
if ($this->logger) { |
84
|
|
|
$this->logger->info("Email sent: '{$email->getSubject()}'", $email); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $emails |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
private function mapEmails(array $emails) |
94
|
|
|
{ |
95
|
|
|
$returnValue = []; |
96
|
|
|
foreach ($emails as $email) { |
97
|
|
|
$returnValue[] = $this->mapEmail($email); |
98
|
|
|
} |
99
|
|
|
return $returnValue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array $email |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
private function mapEmail(array $email) |
107
|
|
|
{ |
108
|
|
|
return !empty($email['name']) ? "'{$email['name']}' <{$email['email']}>" : $email['email']; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: