Page MenuHomePhorge

No OneTemporary

diff --git a/src/applications/metamta/adapter/PhabricatorMailMailgunAdapter.php b/src/applications/metamta/adapter/PhabricatorMailMailgunAdapter.php
index 5a295812eb..9eb478efc5 100644
--- a/src/applications/metamta/adapter/PhabricatorMailMailgunAdapter.php
+++ b/src/applications/metamta/adapter/PhabricatorMailMailgunAdapter.php
@@ -1,158 +1,132 @@
<?php
/**
* Mail adapter that uses Mailgun's web API to deliver email.
*/
final class PhabricatorMailMailgunAdapter
extends PhabricatorMailAdapter {
const ADAPTERTYPE = 'mailgun';
- private $params = array();
- private $attachments = array();
-
- 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,
+ public function getSupportedMessageTypes() {
+ return array(
+ PhabricatorMailEmailMessage::MESSAGETYPE,
);
-
- 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() {
return true;
}
protected function validateOptions(array $options) {
PhutilTypeSpec::checkMap(
$options,
array(
'api-key' => 'string',
'domain' => 'string',
));
}
public function newDefaultOptions() {
return array(
'api-key' => null,
'domain' => null,
);
}
- public function send() {
- $key = $this->getOption('api-key');
+ public function sendMessage(PhabricatorMailExternalMessage $message) {
+ $api_key = $this->getOption('api-key');
$domain = $this->getOption('domain');
$params = array();
- $params['to'] = implode(', ', idx($this->params, 'tos', array()));
- $params['subject'] = idx($this->params, 'subject');
- $params['text'] = idx($this->params, 'body');
+ $subject = $message->getSubject();
+ if ($subject !== null) {
+ $params['subject'] = $subject;
+ }
- if (idx($this->params, 'html-body')) {
- $params['html'] = idx($this->params, 'html-body');
+ $from_address = $message->getFromAddress();
+ if ($from_address) {
+ $params['from'] = (string)$from_address;
}
- $from = idx($this->params, 'from');
- $from_name = idx($this->params, 'from-name');
- $params['from'] = $this->renderAddress($from, $from_name);
+ $to_addresses = $message->getToAddresses();
+ if ($to_addresses) {
+ $to = array();
+ foreach ($to_addresses as $address) {
+ $to[] = (string)$address;
+ }
+ $params['to'] = implode(', ', $to);
+ }
- if (idx($this->params, 'reply-to')) {
- $replyto = $this->params['reply-to'];
- $params['h:reply-to'] = implode(', ', $replyto);
+ $cc_addresses = $message->getCCAddresses();
+ if ($cc_addresses) {
+ $cc = array();
+ foreach ($cc_addresses as $address) {
+ $cc[] = (string)$address;
+ }
+ $params['cc'] = implode(', ', $cc);
}
- if (idx($this->params, 'ccs')) {
- $params['cc'] = implode(', ', $this->params['ccs']);
+ $reply_address = $message->getReplyToAddress();
+ if ($reply_address) {
+ $params['h:reply-to'] = (string)$reply_address;
}
- foreach (idx($this->params, 'headers', array()) as $header) {
- list($name, $value) = $header;
- $params['h:'.$name] = $value;
+ $headers = $message->getHeaders();
+ if ($headers) {
+ foreach ($headers as $header) {
+ $name = $header->getName();
+ $value = $header->getValue();
+ $params['h:'.$name] = $value;
+ }
}
- $future = new HTTPSFuture(
- "https://api:{$key}@api.mailgun.net/v2/{$domain}/messages",
- $params);
- $future->setMethod('POST');
+ $text_body = $message->getTextBody();
+ if ($text_body !== null) {
+ $params['text'] = $text_body;
+ }
+
+ $html_body = $message->getHTMLBody();
+ if ($html_body !== null) {
+ $params['html'] = $html_body;
+ }
+
+ $mailgun_uri = urisprintf(
+ 'https://api.mailgun.net/v2/%s/messages',
+ $domain);
- foreach ($this->attachments as $attachment) {
+ $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(
'attachment',
- $attachment['data'],
- $attachment['name'],
- $attachment['type']);
+ $attachment->getData(),
+ $attachment->getFilename(),
+ $attachment->getMimeType());
}
list($body) = $future->resolvex();
$response = null;
try {
$response = phutil_json_decode($body);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Failed to JSON decode response.'),
$ex);
}
if (!idx($response, 'id')) {
$message = $response['message'];
throw new Exception(
pht(
'Request failed with errors: %s.',
$message));
}
-
- return true;
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Jan 19, 12:22 (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1124512
Default Alt Text
(6 KB)

Event Timeline