1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnimail; |
4
|
|
|
|
5
|
|
|
use Omnimail\Exception\EmailDeliveryException; |
6
|
|
|
use Omnimail\Exception\Exception; |
7
|
|
|
use Omnimail\Exception\InvalidRequestException; |
8
|
|
|
use Omnimail\Exception\UnauthorizedException; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Postmark\PostmarkClient; |
11
|
|
|
use Postmark\Models\PostmarkException; |
12
|
|
|
|
13
|
|
|
class Postmark implements EmailSenderInterface |
14
|
|
|
{ |
15
|
|
|
private $serverApiToken; |
16
|
|
|
private $logger; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $serverApiToken |
20
|
|
|
* @param LoggerInterface|null $logger |
21
|
|
|
*/ |
22
|
|
|
public function __construct($serverApiToken, LoggerInterface $logger = null) |
23
|
|
|
{ |
24
|
|
|
$this->serverApiToken = $serverApiToken; |
25
|
|
|
$this->logger = $logger; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function send(EmailInterface $email) |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
|
|
$client = new PostmarkClient($this->serverApiToken); |
32
|
|
|
$sendResult = $client->sendEmail( |
33
|
|
|
$this->mapEmail($email->getFrom()), |
34
|
|
|
$this->mapEmails($email->getTo()), |
35
|
|
|
$email->getSubject(), |
36
|
|
|
$email->getHtmlBody(), |
37
|
|
|
$email->getTextBody(), |
38
|
|
|
null, |
39
|
|
|
true, |
40
|
|
|
$this->mapEmails($email->getReplyTo()), |
41
|
|
|
$this->mapEmails($email->getCc()), |
42
|
|
|
$this->mapEmails($email->getBcc()), |
43
|
|
|
null, |
44
|
|
|
$this->mapAttachments($email->getAttachements()) |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
if (!isset($sendResult) || (!isset($sendResult[0]) && !isset($sendResult['ErrorCode']))) { |
48
|
|
|
throw new InvalidRequestException; |
49
|
|
|
} elseif (!isset($sendResult['ErrorCode']) && isset($sendResult[0])) { |
50
|
|
|
$sendResult = $sendResult[0]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ((int) $sendResult['ErrorCode'] !== 0) { |
54
|
|
|
$e = new PostmarkException(); |
55
|
|
|
$e->httpStatusCode = $sendResult['ErrorCode']; |
56
|
|
|
$e->message = $sendResult['Message']; |
57
|
|
|
$e->postmarkApiErrorCode = $sendResult['ErrorCode']; |
58
|
|
|
throw $e; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($this->logger) { |
62
|
|
|
$this->logger->info("Email sent: '{$email->getSubject()}'", $email); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} catch (Exception $e) { |
65
|
|
|
if ($this->logger) { |
66
|
|
|
$this->logger->info("Email error: '{$e->getMessage()}'", $email); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
throw $e; |
69
|
|
|
} catch (PostmarkException $e) { |
70
|
|
|
if ($this->logger) { |
71
|
|
|
$this->logger->info("Email error: '{$e->getMessage()}'", $email); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
switch ($e->postmarkApiErrorCode) { |
74
|
|
|
case 10: |
75
|
|
|
case 400: |
76
|
|
|
case 401: |
77
|
|
|
case 405: |
78
|
|
|
case 500: |
79
|
|
|
case 501: |
80
|
|
|
case 502: |
81
|
|
|
case 503: |
82
|
|
|
throw new UnauthorizedException($e->message, 602, $e); |
83
|
|
|
case 100: |
84
|
|
|
case 411: |
85
|
|
|
throw new EmailDeliveryException($e->message, 600, $e); |
86
|
|
|
default: |
87
|
|
|
throw new InvalidRequestException($e->message, 601, $e); |
88
|
|
|
} |
89
|
|
|
} catch (\Exception $e) { |
90
|
|
|
if ($this->logger) { |
91
|
|
|
$this->logger->info("Email error: '{$e->getMessage()}'", $email); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
throw new Exception($e->getMessage(), $e->getCode(), $e); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array|null $attachements |
99
|
|
|
* @return array|null |
100
|
|
|
*/ |
101
|
|
|
private function mapAttachments(array $attachements = null) |
102
|
|
|
{ |
103
|
|
|
if (null === $attachements || !is_array($attachements) || !count($attachements)) { |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$finalAttachements = []; |
108
|
|
|
foreach ($attachements as $attachement) { |
109
|
|
|
$finalAttachement = [ |
110
|
|
|
'ContentType' => $attachement->getMimeType(), |
111
|
|
|
'Name' => $attachement->getName() |
112
|
|
|
]; |
113
|
|
|
if (!$attachement->getPath() && $attachement->getContent()) { |
114
|
|
|
$finalAttachement['Content'] = base64_encode($attachement->getContent()); |
115
|
|
|
} elseif ($attachement->getPath()) { |
116
|
|
|
$finalAttachement['Content'] = base64_encode(file_get_contents($attachement->getPath())); |
117
|
|
|
} |
118
|
|
|
$finalAttachements[] = $finalAttachement; |
119
|
|
|
} |
120
|
|
|
return $finalAttachements; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array $emails |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
private function mapEmails(array $emails) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$returnValue = ''; |
130
|
|
|
foreach ($emails as $email) { |
131
|
|
|
$returnValue .= $this->mapEmail($email) . ', '; |
132
|
|
|
} |
133
|
|
|
return $returnValue ? substr($returnValue, 0, -2) : ''; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $email |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
private function mapEmail(array $email) |
141
|
|
|
{ |
142
|
|
|
return !empty($email['name']) ? "'{$email['name']}' <{$email['email']}>" : $email['email']; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
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: