@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

+60 -86
+60 -86
src/applications/metamta/adapter/PhabricatorMailMailgunAdapter.php
··· 8 8 9 9 const ADAPTERTYPE = 'mailgun'; 10 10 11 - private $params = array(); 12 - private $attachments = array(); 13 - 14 - public function setFrom($email, $name = '') { 15 - $this->params['from'] = $email; 16 - $this->params['from-name'] = $name; 17 - return $this; 18 - } 19 - 20 - public function addReplyTo($email, $name = '') { 21 - if (empty($this->params['reply-to'])) { 22 - $this->params['reply-to'] = array(); 23 - } 24 - $this->params['reply-to'][] = $this->renderAddress($email, $name); 25 - return $this; 26 - } 27 - 28 - public function addTos(array $emails) { 29 - foreach ($emails as $email) { 30 - $this->params['tos'][] = $email; 31 - } 32 - return $this; 33 - } 34 - 35 - public function addCCs(array $emails) { 36 - foreach ($emails as $email) { 37 - $this->params['ccs'][] = $email; 38 - } 39 - return $this; 40 - } 41 - 42 - public function addAttachment($data, $filename, $mimetype) { 43 - $this->attachments[] = array( 44 - 'data' => $data, 45 - 'name' => $filename, 46 - 'type' => $mimetype, 11 + public function getSupportedMessageTypes() { 12 + return array( 13 + PhabricatorMailEmailMessage::MESSAGETYPE, 47 14 ); 48 - 49 - return $this; 50 - } 51 - 52 - public function addHeader($header_name, $header_value) { 53 - $this->params['headers'][] = array($header_name, $header_value); 54 - return $this; 55 - } 56 - 57 - public function setBody($body) { 58 - $this->params['body'] = $body; 59 - return $this; 60 - } 61 - 62 - public function setHTMLBody($html_body) { 63 - $this->params['html-body'] = $html_body; 64 - return $this; 65 - } 66 - 67 - public function setSubject($subject) { 68 - $this->params['subject'] = $subject; 69 - return $this; 70 15 } 71 16 72 17 public function supportsMessageIDHeader() { ··· 89 34 ); 90 35 } 91 36 92 - public function send() { 93 - $key = $this->getOption('api-key'); 37 + public function sendMessage(PhabricatorMailExternalMessage $message) { 38 + $api_key = $this->getOption('api-key'); 94 39 $domain = $this->getOption('domain'); 95 40 $params = array(); 96 41 97 - $params['to'] = implode(', ', idx($this->params, 'tos', array())); 98 - $params['subject'] = idx($this->params, 'subject'); 99 - $params['text'] = idx($this->params, 'body'); 42 + $subject = $message->getSubject(); 43 + if ($subject !== null) { 44 + $params['subject'] = $subject; 45 + } 100 46 101 - if (idx($this->params, 'html-body')) { 102 - $params['html'] = idx($this->params, 'html-body'); 47 + $from_address = $message->getFromAddress(); 48 + if ($from_address) { 49 + $params['from'] = (string)$from_address; 50 + } 51 + 52 + $to_addresses = $message->getToAddresses(); 53 + if ($to_addresses) { 54 + $to = array(); 55 + foreach ($to_addresses as $address) { 56 + $to[] = (string)$address; 57 + } 58 + $params['to'] = implode(', ', $to); 59 + } 60 + 61 + $cc_addresses = $message->getCCAddresses(); 62 + if ($cc_addresses) { 63 + $cc = array(); 64 + foreach ($cc_addresses as $address) { 65 + $cc[] = (string)$address; 66 + } 67 + $params['cc'] = implode(', ', $cc); 103 68 } 104 69 105 - $from = idx($this->params, 'from'); 106 - $from_name = idx($this->params, 'from-name'); 107 - $params['from'] = $this->renderAddress($from, $from_name); 70 + $reply_address = $message->getReplyToAddress(); 71 + if ($reply_address) { 72 + $params['h:reply-to'] = (string)$reply_address; 73 + } 108 74 109 - if (idx($this->params, 'reply-to')) { 110 - $replyto = $this->params['reply-to']; 111 - $params['h:reply-to'] = implode(', ', $replyto); 75 + $headers = $message->getHeaders(); 76 + if ($headers) { 77 + foreach ($headers as $header) { 78 + $name = $header->getName(); 79 + $value = $header->getValue(); 80 + $params['h:'.$name] = $value; 81 + } 112 82 } 113 83 114 - if (idx($this->params, 'ccs')) { 115 - $params['cc'] = implode(', ', $this->params['ccs']); 84 + $text_body = $message->getTextBody(); 85 + if ($text_body !== null) { 86 + $params['text'] = $text_body; 116 87 } 117 88 118 - foreach (idx($this->params, 'headers', array()) as $header) { 119 - list($name, $value) = $header; 120 - $params['h:'.$name] = $value; 89 + $html_body = $message->getHTMLBody(); 90 + if ($html_body !== null) { 91 + $params['html'] = $html_body; 121 92 } 122 93 123 - $future = new HTTPSFuture( 124 - "https://api:{$key}@api.mailgun.net/v2/{$domain}/messages", 125 - $params); 126 - $future->setMethod('POST'); 94 + $mailgun_uri = urisprintf( 95 + 'https://api.mailgun.net/v2/%s/messages', 96 + $domain); 127 97 128 - foreach ($this->attachments as $attachment) { 98 + $future = id(new HTTPSFuture($mailgun_uri, $params)) 99 + ->setMethod('POST') 100 + ->setHTTPBasicAuthCredentials('api', new PhutilOpaqueEnvelope($api_key)) 101 + ->setTimeout(60); 102 + 103 + $attachments = $message->getAttachments(); 104 + foreach ($attachments as $attachment) { 129 105 $future->attachFileData( 130 106 'attachment', 131 - $attachment['data'], 132 - $attachment['name'], 133 - $attachment['type']); 107 + $attachment->getData(), 108 + $attachment->getFilename(), 109 + $attachment->getMimeType()); 134 110 } 135 111 136 112 list($body) = $future->resolvex(); ··· 151 127 'Request failed with errors: %s.', 152 128 $message)); 153 129 } 154 - 155 - return true; 156 130 } 157 131 158 132 }