forked from Stampie/Stampie
/
Mandrill.php
168 lines (147 loc) · 5.15 KB
/
Mandrill.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace Stampie\Mailer;
use Stampie\Mailer;
use Stampie\Message\MetadataAwareInterface;
use Stampie\MessageInterface;
use Stampie\Message\TaggableInterface;
use Stampie\Message\AttachmentsInterface;
use Stampie\Adapter\ResponseInterface;
use Stampie\AttachmentInterface;
use Stampie\Exception\HttpException;
use Stampie\Exception\ApiException;
use Stampie\Util\AttachmentUtils;
/**
* Sends emails to Mandrill server
*
* @author Christophe Coevoet <stof@notk.org>
*/
class Mandrill extends Mailer
{
/**
* {@inheritdoc}
*/
protected function getEndpoint()
{
return 'https://mandrillapp.com/api/1.0/messages/send.json';
}
/**
* {@inheritdoc}
*/
protected function getHeaders()
{
return array(
'Content-Type' => 'application/json',
);
}
/**
* {@inheritdoc}
*/
protected function format(MessageInterface $message)
{
$headers = array_filter(array_merge(
$message->getHeaders(),
array('Reply-To' => $message->getReplyTo())
));
$from = $this->normalizeIdentity($message->getFrom());
$to = array();
foreach ($this->normalizeIdentities($message->getTo()) as $recipient) {
$to[] = array('email' => $recipient->getEmail(), 'name' => $recipient->getName(), 'type' => 'to');
}
foreach ($this->normalizeIdentities($message->getCc()) as $recipient) {
$to[] = array('email' => $recipient->getEmail(), 'name' => $recipient->getName(), 'type' => 'cc');
}
foreach ($this->normalizeIdentities($message->getBcc()) as $recipient) {
$to[] = array('email' => $recipient->getEmail(), 'name' => $recipient->getName(), 'type' => 'bcc');
}
$tags = array();
if ($message instanceof TaggableInterface) {
$tags = (array) $message->getTag();
}
$metadata = array();
if ($message instanceof MetadataAwareInterface) {
$metadata = array_filter($message->getMetadata());
}
$images = array();
$attachments = array();
if ($message instanceof AttachmentsInterface) {
list($attachments, $images) = $this->processAttachments($message->getAttachments());
}
$parameters = array(
'key' => $this->getServerToken(),
'message' => array_filter(array(
'from_email' => $from->getEmail(),
'from_name' => $from->getName(),
'to' => $to,
'subject' => $message->getSubject(),
'headers' => $headers,
'text' => $message->getText(),
'html' => $message->getHtml(),
'tags' => $tags,
'metadata' => $metadata,
'attachments' => $attachments,
'images' => $images,
)),
);
return json_encode($parameters);
}
/**
* {@inheritdoc}
*
* "You can consider any non-200 HTTP response code an error - the returned data will contain more detailed information"
*/
protected function handle(ResponseInterface $response)
{
$httpException = new HttpException($response->getStatusCode(), $response->getStatusText());
$error = json_decode($response->getContent());
throw new ApiException($error->message, $httpException, $error->code);
}
/**
* @param AttachmentInterface[] $attachments
* @return array
* First element: Attachments – an array containing arrays of the following format
* array(
* 'type' => type,
* 'name' => name,
* 'content' => base64-encoded content,
* )
*
* Second element: Inline images – an array containing arrays of the following format
* array(
* 'type' => type,
* 'name' => id,
* 'content' => base64-encoded content,
* )
*/
protected function processAttachments(array $attachments)
{
$attachments = AttachmentUtils::processAttachments($attachments);
$processedAttachments = array();
$images = array();
foreach ($attachments as $name => $attachment) {
$type = $attachment->getType();
$item = array(
'type' => $type,
'name' => $name,
'content' => base64_encode($this->getAttachmentContent($attachment)),
);
$id = $attachment->getID();
if (strpos($type, 'image/') === 0 && isset($id)) {
// Inline image
$item['name'] = $id;
$images[] = $item;
} else {
// Attached
$processedAttachments[] = $item;
}
}
return array($processedAttachments, $images);
}
/**
* @param AttachmentInterface $attachment
* @return string
*/
protected function getAttachmentContent(AttachmentInterface $attachment)
{
return file_get_contents($attachment->getPath());
}
}