1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coconuts\Mail; |
4
|
|
|
|
5
|
|
|
use Swift_Attachment; |
6
|
|
|
use Swift_Mime_SimpleMessage; |
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use Illuminate\Mail\Transport\Transport; |
9
|
|
|
|
10
|
|
|
class PostmarkTransport extends Transport |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Guzzle client instance. |
14
|
|
|
* |
15
|
|
|
* @var \GuzzleHttp\ClientInterface |
16
|
|
|
*/ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The Postmark API key. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $key; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The Postmark API end-point. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $url = 'https://api.postmarkapp.com/email'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new Postmark transport instance. |
35
|
|
|
* |
36
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
37
|
|
|
* @param string $key |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
4 |
|
public function __construct(ClientInterface $client, $key) |
42
|
|
|
{ |
43
|
4 |
|
$this->key = $key; |
44
|
4 |
|
$this->client = $client; |
45
|
4 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
1 |
|
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
51
|
|
|
{ |
52
|
1 |
|
$this->beforeSendPerformed($message); |
53
|
|
|
|
54
|
1 |
|
$response = $this->client->post($this->url, $this->payload($message)); |
55
|
|
|
|
56
|
1 |
|
$message->getHeaders()->addTextHeader( |
57
|
1 |
|
'X-PM-Message-Id', |
58
|
1 |
|
$this->getMessageId($response) |
59
|
|
|
); |
60
|
|
|
|
61
|
1 |
|
$this->sendPerformed($message); |
62
|
|
|
|
63
|
1 |
|
return $this->numberOfRecipients($message); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get all attachments for the given message. |
68
|
|
|
* |
69
|
|
|
* @param \Swift_Mime_SimpleMessage $message |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
2 |
|
protected function getAttachments(Swift_Mime_SimpleMessage $message) |
74
|
|
|
{ |
75
|
2 |
|
return collect($message->getChildren()) |
76
|
2 |
|
->filter(function ($child) { |
77
|
2 |
|
return $child instanceof Swift_Attachment; |
78
|
|
|
})->map(function ($child) { |
79
|
|
|
return [ |
80
|
2 |
|
'Name' => $child->getHeaders()->get('content-type')->getParameter('name'), |
81
|
2 |
|
'Content' => base64_encode($child->getBody()), |
82
|
2 |
|
'ContentType' => $child->getContentType() |
83
|
|
|
]; |
84
|
2 |
|
}); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Format the contacts for the API request. |
89
|
|
|
* |
90
|
|
|
* @param array $contacts |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
3 |
|
protected function getContacts($contacts) |
95
|
|
|
{ |
96
|
3 |
|
return collect($contacts) |
97
|
3 |
|
->map(function ($display, $address) { |
98
|
3 |
|
return $display ? $display." <{$address}>" : $address; |
99
|
3 |
|
}) |
100
|
3 |
|
->values() |
101
|
3 |
|
->implode(','); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the message ID from the response. |
106
|
|
|
* |
107
|
|
|
* @param \GuzzleHttp\Psr7\Response $response |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
1 |
|
protected function getMessageId($response) |
112
|
|
|
{ |
113
|
1 |
|
return object_get( |
114
|
1 |
|
json_decode($response->getBody()->getContents()), |
115
|
1 |
|
'MessageID' |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the HTTP payload for sending the Postmark message. |
121
|
|
|
* |
122
|
|
|
* @param \Swift_Mime_SimpleMessage $message |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
2 |
|
protected function payload(Swift_Mime_SimpleMessage $message) |
127
|
|
|
{ |
128
|
|
|
return [ |
129
|
|
|
'headers' => [ |
130
|
2 |
|
'Accept' => 'application/json', |
131
|
2 |
|
'X-Postmark-Server-Token' => $this->key, |
132
|
|
|
], |
133
|
|
|
'json' => [ |
134
|
2 |
|
'From' => $this->getContacts($message->getFrom()), |
135
|
2 |
|
'To' => $this->getContacts($message->getTo()), |
136
|
2 |
|
'Cc' => $this->getContacts($message->getCc()), |
137
|
2 |
|
'Bcc' => $this->getContacts($message->getBcc()), |
138
|
2 |
|
'Tag' => $message->getHeaders()->has('tag') ? $message->getHeaders()->get('tag')->getFieldBody() : '', |
139
|
2 |
|
'Subject' => $message->getSubject(), |
140
|
2 |
|
'HtmlBody' => $message->getBody(), |
141
|
2 |
|
'ReplyTo' => $this->getContacts($message->getReplyTo()), |
142
|
2 |
|
'Attachments' => $this->getAttachments($message), |
143
|
|
|
], |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|