Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/adapter/PhabricatorMailMailgunAdapter.php
12256 views
1
<?php
2
3
/**
4
* Mail adapter that uses Mailgun's web API to deliver email.
5
*/
6
final class PhabricatorMailMailgunAdapter
7
extends PhabricatorMailAdapter {
8
9
const ADAPTERTYPE = 'mailgun';
10
11
public function getSupportedMessageTypes() {
12
return array(
13
PhabricatorMailEmailMessage::MESSAGETYPE,
14
);
15
}
16
17
public function supportsMessageIDHeader() {
18
return true;
19
}
20
21
protected function validateOptions(array $options) {
22
PhutilTypeSpec::checkMap(
23
$options,
24
array(
25
'api-key' => 'string',
26
'domain' => 'string',
27
'api-hostname' => 'string',
28
));
29
}
30
31
public function newDefaultOptions() {
32
return array(
33
'api-key' => null,
34
'domain' => null,
35
'api-hostname' => 'api.mailgun.net',
36
);
37
}
38
39
public function sendMessage(PhabricatorMailExternalMessage $message) {
40
$api_key = $this->getOption('api-key');
41
$domain = $this->getOption('domain');
42
$api_hostname = $this->getOption('api-hostname');
43
$params = array();
44
45
$subject = $message->getSubject();
46
if ($subject !== null) {
47
$params['subject'] = $subject;
48
}
49
50
$from_address = $message->getFromAddress();
51
if ($from_address) {
52
$params['from'] = (string)$from_address;
53
}
54
55
$to_addresses = $message->getToAddresses();
56
if ($to_addresses) {
57
$to = array();
58
foreach ($to_addresses as $address) {
59
$to[] = (string)$address;
60
}
61
$params['to'] = implode(', ', $to);
62
}
63
64
$cc_addresses = $message->getCCAddresses();
65
if ($cc_addresses) {
66
$cc = array();
67
foreach ($cc_addresses as $address) {
68
$cc[] = (string)$address;
69
}
70
$params['cc'] = implode(', ', $cc);
71
}
72
73
$reply_address = $message->getReplyToAddress();
74
if ($reply_address) {
75
$params['h:reply-to'] = (string)$reply_address;
76
}
77
78
$headers = $message->getHeaders();
79
if ($headers) {
80
foreach ($headers as $header) {
81
$name = $header->getName();
82
$value = $header->getValue();
83
$params['h:'.$name] = $value;
84
}
85
}
86
87
$text_body = $message->getTextBody();
88
if ($text_body !== null) {
89
$params['text'] = $text_body;
90
}
91
92
$html_body = $message->getHTMLBody();
93
if ($html_body !== null) {
94
$params['html'] = $html_body;
95
}
96
97
$mailgun_uri = urisprintf(
98
'https://%s/v2/%s/messages',
99
$api_hostname,
100
$domain);
101
102
$future = id(new HTTPSFuture($mailgun_uri, $params))
103
->setMethod('POST')
104
->setHTTPBasicAuthCredentials('api', new PhutilOpaqueEnvelope($api_key))
105
->setTimeout(60);
106
107
$attachments = $message->getAttachments();
108
foreach ($attachments as $attachment) {
109
$future->attachFileData(
110
'attachment',
111
$attachment->getData(),
112
$attachment->getFilename(),
113
$attachment->getMimeType());
114
}
115
116
list($body) = $future->resolvex();
117
118
$response = null;
119
try {
120
$response = phutil_json_decode($body);
121
} catch (PhutilJSONParserException $ex) {
122
throw new PhutilProxyException(
123
pht('Failed to JSON decode response.'),
124
$ex);
125
}
126
127
if (!idx($response, 'id')) {
128
$message = $response['message'];
129
throw new Exception(
130
pht(
131
'Request failed with errors: %s.',
132
$message));
133
}
134
}
135
136
}
137
138