Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / metamta / adapter / PhabricatorMailMailgunAdapter.php
blob8223ee8102132f09db0b8bb2d963f7b3a6247c8a
1 <?php
3 /**
4 * Mail adapter that uses Mailgun's web API to deliver email.
5 */
6 final class PhabricatorMailMailgunAdapter
7 extends PhabricatorMailAdapter {
9 const ADAPTERTYPE = 'mailgun';
11 public function getSupportedMessageTypes() {
12 return array(
13 PhabricatorMailEmailMessage::MESSAGETYPE,
17 public function supportsMessageIDHeader() {
18 return true;
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 ));
31 public function newDefaultOptions() {
32 return array(
33 'api-key' => null,
34 'domain' => null,
35 'api-hostname' => 'api.mailgun.net',
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();
45 $subject = $message->getSubject();
46 if ($subject !== null) {
47 $params['subject'] = $subject;
50 $from_address = $message->getFromAddress();
51 if ($from_address) {
52 $params['from'] = (string)$from_address;
55 $to_addresses = $message->getToAddresses();
56 if ($to_addresses) {
57 $to = array();
58 foreach ($to_addresses as $address) {
59 $to[] = (string)$address;
61 $params['to'] = implode(', ', $to);
64 $cc_addresses = $message->getCCAddresses();
65 if ($cc_addresses) {
66 $cc = array();
67 foreach ($cc_addresses as $address) {
68 $cc[] = (string)$address;
70 $params['cc'] = implode(', ', $cc);
73 $reply_address = $message->getReplyToAddress();
74 if ($reply_address) {
75 $params['h:reply-to'] = (string)$reply_address;
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;
87 $text_body = $message->getTextBody();
88 if ($text_body !== null) {
89 $params['text'] = $text_body;
92 $html_body = $message->getHTMLBody();
93 if ($html_body !== null) {
94 $params['html'] = $html_body;
97 $mailgun_uri = urisprintf(
98 'https://%s/v2/%s/messages',
99 $api_hostname,
100 $domain);
102 $future = id(new HTTPSFuture($mailgun_uri, $params))
103 ->setMethod('POST')
104 ->setHTTPBasicAuthCredentials('api', new PhutilOpaqueEnvelope($api_key))
105 ->setTimeout(60);
107 $attachments = $message->getAttachments();
108 foreach ($attachments as $attachment) {
109 $future->attachFileData(
110 'attachment',
111 $attachment->getData(),
112 $attachment->getFilename(),
113 $attachment->getMimeType());
116 list($body) = $future->resolvex();
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);
127 if (!idx($response, 'id')) {
128 $message = $response['message'];
129 throw new Exception(
130 pht(
131 'Request failed with errors: %s.',
132 $message));