1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-29 18:22:41 +01:00

Update Mailgun adapter for the new mail adapter API

Summary: Ref T920. Ref T5969. Update the Mailgun adapter for the API changes and add a timeout.

Test Plan: Configured Mailgun as a mailer, sent mail with subject/to/cc/headers/html/attachments using `bin/mail send-test`.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T5969, T920

Differential Revision: https://secure.phabricator.com/D19959
This commit is contained in:
epriestley 2019-01-05 04:53:49 -08:00
parent bc97a7d755
commit d7da3560ec

View file

@ -8,65 +8,10 @@ final class PhabricatorMailMailgunAdapter
const ADAPTERTYPE = 'mailgun'; const ADAPTERTYPE = 'mailgun';
private $params = array(); public function getSupportedMessageTypes() {
private $attachments = array(); return array(
PhabricatorMailEmailMessage::MESSAGETYPE,
public function setFrom($email, $name = '') {
$this->params['from'] = $email;
$this->params['from-name'] = $name;
return $this;
}
public function addReplyTo($email, $name = '') {
if (empty($this->params['reply-to'])) {
$this->params['reply-to'] = array();
}
$this->params['reply-to'][] = $this->renderAddress($email, $name);
return $this;
}
public function addTos(array $emails) {
foreach ($emails as $email) {
$this->params['tos'][] = $email;
}
return $this;
}
public function addCCs(array $emails) {
foreach ($emails as $email) {
$this->params['ccs'][] = $email;
}
return $this;
}
public function addAttachment($data, $filename, $mimetype) {
$this->attachments[] = array(
'data' => $data,
'name' => $filename,
'type' => $mimetype,
); );
return $this;
}
public function addHeader($header_name, $header_value) {
$this->params['headers'][] = array($header_name, $header_value);
return $this;
}
public function setBody($body) {
$this->params['body'] = $body;
return $this;
}
public function setHTMLBody($html_body) {
$this->params['html-body'] = $html_body;
return $this;
}
public function setSubject($subject) {
$this->params['subject'] = $subject;
return $this;
} }
public function supportsMessageIDHeader() { public function supportsMessageIDHeader() {
@ -89,48 +34,79 @@ final class PhabricatorMailMailgunAdapter
); );
} }
public function send() { public function sendMessage(PhabricatorMailExternalMessage $message) {
$key = $this->getOption('api-key'); $api_key = $this->getOption('api-key');
$domain = $this->getOption('domain'); $domain = $this->getOption('domain');
$params = array(); $params = array();
$params['to'] = implode(', ', idx($this->params, 'tos', array())); $subject = $message->getSubject();
$params['subject'] = idx($this->params, 'subject'); if ($subject !== null) {
$params['text'] = idx($this->params, 'body'); $params['subject'] = $subject;
if (idx($this->params, 'html-body')) {
$params['html'] = idx($this->params, 'html-body');
} }
$from = idx($this->params, 'from'); $from_address = $message->getFromAddress();
$from_name = idx($this->params, 'from-name'); if ($from_address) {
$params['from'] = $this->renderAddress($from, $from_name); $params['from'] = (string)$from_address;
if (idx($this->params, 'reply-to')) {
$replyto = $this->params['reply-to'];
$params['h:reply-to'] = implode(', ', $replyto);
} }
if (idx($this->params, 'ccs')) { $to_addresses = $message->getToAddresses();
$params['cc'] = implode(', ', $this->params['ccs']); if ($to_addresses) {
$to = array();
foreach ($to_addresses as $address) {
$to[] = (string)$address;
}
$params['to'] = implode(', ', $to);
} }
foreach (idx($this->params, 'headers', array()) as $header) { $cc_addresses = $message->getCCAddresses();
list($name, $value) = $header; if ($cc_addresses) {
$cc = array();
foreach ($cc_addresses as $address) {
$cc[] = (string)$address;
}
$params['cc'] = implode(', ', $cc);
}
$reply_address = $message->getReplyToAddress();
if ($reply_address) {
$params['h:reply-to'] = (string)$reply_address;
}
$headers = $message->getHeaders();
if ($headers) {
foreach ($headers as $header) {
$name = $header->getName();
$value = $header->getValue();
$params['h:'.$name] = $value; $params['h:'.$name] = $value;
} }
}
$future = new HTTPSFuture( $text_body = $message->getTextBody();
"https://api:{$key}@api.mailgun.net/v2/{$domain}/messages", if ($text_body !== null) {
$params); $params['text'] = $text_body;
$future->setMethod('POST'); }
foreach ($this->attachments as $attachment) { $html_body = $message->getHTMLBody();
if ($html_body !== null) {
$params['html'] = $html_body;
}
$mailgun_uri = urisprintf(
'https://api.mailgun.net/v2/%s/messages',
$domain);
$future = id(new HTTPSFuture($mailgun_uri, $params))
->setMethod('POST')
->setHTTPBasicAuthCredentials('api', new PhutilOpaqueEnvelope($api_key))
->setTimeout(60);
$attachments = $message->getAttachments();
foreach ($attachments as $attachment) {
$future->attachFileData( $future->attachFileData(
'attachment', 'attachment',
$attachment['data'], $attachment->getData(),
$attachment['name'], $attachment->getFilename(),
$attachment['type']); $attachment->getMimeType());
} }
list($body) = $future->resolvex(); list($body) = $future->resolvex();
@ -151,8 +127,6 @@ final class PhabricatorMailMailgunAdapter
'Request failed with errors: %s.', 'Request failed with errors: %s.',
$message)); $message));
} }
return true;
} }
} }