/
PostmarkApiTransport.php
122 lines (100 loc) · 4.06 KB
/
PostmarkApiTransport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Bridge\Postmark\Transport;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* @author Kevin Verschaeve
*/
class PostmarkApiTransport extends AbstractApiTransport
{
private const HOST = 'api.postmarkapp.com';
private $key;
public function __construct(string $key, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
$this->key = $key;
parent::__construct($client, $dispatcher, $logger);
}
public function __toString(): string
{
return sprintf('postmark+api://%s', $this->getEndpoint());
}
protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface
{
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/email', [
'headers' => [
'Accept' => 'application/json',
'X-Postmark-Server-Token' => $this->key,
],
'json' => $this->getPayload($email, $envelope),
]);
$result = $response->toArray(false);
if (200 !== $response->getStatusCode()) {
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %d).', $result['Message'], $result['ErrorCode']), $response);
}
$sentMessage->setMessageId($result['MessageID']);
return $response;
}
private function getPayload(Email $email, Envelope $envelope): array
{
$payload = [
'From' => $envelope->getSender()->toString(),
'To' => implode(',', $this->stringifyAddresses($this->getRecipients($email, $envelope))),
'Cc' => implode(',', $this->stringifyAddresses($email->getCc())),
'Bcc' => implode(',', $this->stringifyAddresses($email->getBcc())),
'ReplyTo' => implode(',', $this->stringifyAddresses($email->getReplyTo())),
'Subject' => $email->getSubject(),
'TextBody' => $email->getTextBody(),
'HtmlBody' => $email->getHtmlBody(),
'Attachments' => $this->getAttachments($email),
];
$headersToBypass = ['from', 'to', 'cc', 'bcc', 'subject', 'content-type', 'sender', 'reply-to'];
foreach ($email->getHeaders()->all() as $name => $header) {
if (\in_array($name, $headersToBypass, true)) {
continue;
}
$payload['Headers'][] = [
'Name' => $name,
'Value' => $header->getBodyAsString(),
];
}
return $payload;
}
private function getAttachments(Email $email): array
{
$attachments = [];
foreach ($email->getAttachments() as $attachment) {
$headers = $attachment->getPreparedHeaders();
$filename = $headers->getHeaderParameter('Content-Disposition', 'filename');
$disposition = $headers->getHeaderBody('Content-Disposition');
$att = [
'Name' => $filename,
'Content' => $attachment->bodyToString(),
'ContentType' => $headers->get('Content-Type')->getBody(),
];
if ('inline' === $disposition) {
$att['ContentID'] = 'cid:'.$filename;
}
$attachments[] = $att;
}
return $attachments;
}
private function getEndpoint(): ?string
{
return ($this->host ?: self::HOST).($this->port ? ':'.$this->port : '');
}
}