Path: blob/master/src/applications/metamta/adapter/PhabricatorMailMailgunAdapter.php
12256 views
<?php12/**3* Mail adapter that uses Mailgun's web API to deliver email.4*/5final class PhabricatorMailMailgunAdapter6extends PhabricatorMailAdapter {78const ADAPTERTYPE = 'mailgun';910public function getSupportedMessageTypes() {11return array(12PhabricatorMailEmailMessage::MESSAGETYPE,13);14}1516public function supportsMessageIDHeader() {17return true;18}1920protected function validateOptions(array $options) {21PhutilTypeSpec::checkMap(22$options,23array(24'api-key' => 'string',25'domain' => 'string',26'api-hostname' => 'string',27));28}2930public function newDefaultOptions() {31return array(32'api-key' => null,33'domain' => null,34'api-hostname' => 'api.mailgun.net',35);36}3738public function sendMessage(PhabricatorMailExternalMessage $message) {39$api_key = $this->getOption('api-key');40$domain = $this->getOption('domain');41$api_hostname = $this->getOption('api-hostname');42$params = array();4344$subject = $message->getSubject();45if ($subject !== null) {46$params['subject'] = $subject;47}4849$from_address = $message->getFromAddress();50if ($from_address) {51$params['from'] = (string)$from_address;52}5354$to_addresses = $message->getToAddresses();55if ($to_addresses) {56$to = array();57foreach ($to_addresses as $address) {58$to[] = (string)$address;59}60$params['to'] = implode(', ', $to);61}6263$cc_addresses = $message->getCCAddresses();64if ($cc_addresses) {65$cc = array();66foreach ($cc_addresses as $address) {67$cc[] = (string)$address;68}69$params['cc'] = implode(', ', $cc);70}7172$reply_address = $message->getReplyToAddress();73if ($reply_address) {74$params['h:reply-to'] = (string)$reply_address;75}7677$headers = $message->getHeaders();78if ($headers) {79foreach ($headers as $header) {80$name = $header->getName();81$value = $header->getValue();82$params['h:'.$name] = $value;83}84}8586$text_body = $message->getTextBody();87if ($text_body !== null) {88$params['text'] = $text_body;89}9091$html_body = $message->getHTMLBody();92if ($html_body !== null) {93$params['html'] = $html_body;94}9596$mailgun_uri = urisprintf(97'https://%s/v2/%s/messages',98$api_hostname,99$domain);100101$future = id(new HTTPSFuture($mailgun_uri, $params))102->setMethod('POST')103->setHTTPBasicAuthCredentials('api', new PhutilOpaqueEnvelope($api_key))104->setTimeout(60);105106$attachments = $message->getAttachments();107foreach ($attachments as $attachment) {108$future->attachFileData(109'attachment',110$attachment->getData(),111$attachment->getFilename(),112$attachment->getMimeType());113}114115list($body) = $future->resolvex();116117$response = null;118try {119$response = phutil_json_decode($body);120} catch (PhutilJSONParserException $ex) {121throw new PhutilProxyException(122pht('Failed to JSON decode response.'),123$ex);124}125126if (!idx($response, 'id')) {127$message = $response['message'];128throw new Exception(129pht(130'Request failed with errors: %s.',131$message));132}133}134135}136137138