1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnimail; |
4
|
|
|
|
5
|
|
|
use Omnimail\Exception\InvalidRequestException; |
6
|
|
|
use Omnimail\Exception\UnauthorizedException; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use SendGrid\Email; |
|
|
|
|
9
|
|
|
use SendGrid\Content; |
10
|
|
|
use SendGrid\Mail; |
11
|
|
|
use SendGrid\Attachment; |
12
|
|
|
use SendGrid\Personalization; |
13
|
|
|
use SendGrid\Response; |
14
|
|
|
|
15
|
|
|
class Sendgrid implements EmailSenderInterface |
16
|
|
|
{ |
17
|
|
|
private $apiKey; |
18
|
|
|
private $logger; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $apiKey |
22
|
|
|
* @param LoggerInterface|null $logger |
23
|
|
|
*/ |
24
|
|
|
public function __construct($apiKey, LoggerInterface $logger = null) |
25
|
|
|
{ |
26
|
|
|
$this->apiKey = $apiKey; |
27
|
|
|
$this->logger = $logger; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function send(EmailInterface $email) |
31
|
|
|
{ |
32
|
|
|
$content = null; |
33
|
|
|
if ($email->getHtmlBody()) { |
34
|
|
|
$content = new Content("text/html", $email->getHtmlBody()); |
35
|
|
|
} elseif ($email->getTextBody()) { |
36
|
|
|
$content = new Content("text/plain", $email->getTextBody()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$mail = new Mail( |
40
|
|
|
$this->mapEmail($email->getFrom()), |
41
|
|
|
$email->getSubject(), |
42
|
|
|
null, |
43
|
|
|
$content |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
/** @var Personalization $personalization */ |
47
|
|
|
$personalization = $mail->personalization; |
48
|
|
|
|
49
|
|
|
foreach ($email->getTo() as $recipient) { |
50
|
|
|
$personalization->addTo($this->mapEmail($recipient)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($email->getReplyTo()) { |
54
|
|
|
foreach ($email->getReplyTo() as $recipient) { |
55
|
|
|
$mail->setReplyTo($this->mapEmail($recipient)); |
56
|
|
|
break; // only one reply to |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($email->getCc()) { |
61
|
|
|
foreach ($email->getCc() as $recipient) { |
62
|
|
|
$personalization->addCc($this->mapEmail($recipient)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($email->getBcc()) { |
67
|
|
|
foreach ($email->getBcc() as $recipient) { |
68
|
|
|
$personalization->addBcc($this->mapEmail($recipient)); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($email->getAttachements()) { |
73
|
|
|
foreach ($email->getAttachements() as $attachement) { |
74
|
|
|
$finalAttachment = new Attachment(); |
75
|
|
|
$finalAttachment->setType($attachement->getMimeType()); |
76
|
|
|
$finalAttachment->setFilename($attachement->getName()); |
77
|
|
|
if (!$attachement->getPath() && $attachement->getContent()) { |
78
|
|
|
$finalAttachment->setContent(base64_encode($attachement->getContent())); |
79
|
|
|
} elseif ($attachement->getPath()) { |
80
|
|
|
$finalAttachment->setContent(base64_encode(file_get_contents($attachement->getPath()))); |
81
|
|
|
} |
82
|
|
|
$mail->addAttachment($finalAttachment); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$sg = new \SendGrid($this->apiKey); |
87
|
|
|
/** @var Response $response */ |
88
|
|
|
$response = $sg->client->mail()->send()->post($mail); |
89
|
|
|
|
90
|
|
|
if ($response->statusCode() >= 200 && $response->statusCode() < 300) { |
91
|
|
|
if ($this->logger) { |
92
|
|
|
$this->logger->info("Email sent: '{$email->getSubject()}'", $email); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
switch ($response->statusCode()) { |
96
|
|
|
case 401: |
97
|
|
|
if ($this->logger) { |
98
|
|
|
$this->logger->info("Email error: 'unauthorized'", $email); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
throw new UnauthorizedException; |
101
|
|
|
default: |
102
|
|
|
if ($this->logger) { |
103
|
|
|
$this->logger->info("Email error: 'invalid request'", $email); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
throw new InvalidRequestException; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $email |
112
|
|
|
* @return Email |
113
|
|
|
*/ |
114
|
|
|
private function mapEmail($email) |
115
|
|
|
{ |
116
|
|
|
return new Email(isset($email['name']) ? $email['name'] : null, $email['email']); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: