1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-09 16:32:39 +01:00

Update the "SES" and "sendmail" mailers for the new API; remove "encoding"

Summary: Ref T13222. Ref T920. This is the last of the upstream adapter updates.

Test Plan:
  - Sent mail with SES.
  - Sent mail with "sendmail". I don't have sendmail actually configured to an upstream MTA so I'm not 100% sure this worked, but the `sendmail` binary didn't complain and almost all of the code is shared with SES, so I'm reasonably confident this actually works.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13222, T920

Differential Revision: https://secure.phabricator.com/D19965
This commit is contained in:
epriestley 2019-01-14 07:50:20 -08:00
parent 43a6f34e7f
commit c3cafffed7
5 changed files with 136 additions and 114 deletions

View file

@ -42,6 +42,102 @@ if (version_compare(PHP_VERSION, '5.0.0', '<') ) exit("Sorry, this version of PH
class PHPMailerLite {
public static function newFromMessage(
PhabricatorMailExternalMessage $message) {
$mailer = new self($use_exceptions = true);
// By default, PHPMailerLite sends one mail per recipient. We handle
// combining or separating To and Cc higher in the stack, so tell it to
// send mail exactly like we ask.
$mailer->SingleTo = false;
$mailer->CharSet = 'utf-8';
$mailer->Encoding = 'base64';
$subject = $message->getSubject();
if ($subject !== null) {
$mailer->Subject = $subject;
}
$from_address = $message->getFromAddress();
if ($from_address) {
$mailer->SetFrom(
$from_address->getAddress(),
(string)$from_address->getDisplayName(),
$crazy_side_effects = false);
}
$reply_address = $message->getReplyToAddress();
if ($reply_address) {
$mailer->AddReplyTo(
$reply_address->getAddress(),
(string)$reply_address->getDisplayName());
}
$to_addresses = $message->getToAddresses();
if ($to_addresses) {
foreach ($to_addresses as $address) {
$mailer->AddAddress(
$address->getAddress(),
(string)$address->getDisplayName());
}
}
$cc_addresses = $message->getCCAddresses();
if ($cc_addresses) {
foreach ($cc_addresses as $address) {
$mailer->AddCC(
$address->getAddress(),
(string)$address->getDisplayName());
}
}
$headers = $message->getHeaders();
if ($headers) {
foreach ($headers as $header) {
$name = $header->getName();
$value = $header->getValue();
if (phutil_utf8_strtolower($name) === 'message-id') {
$mailer->MessageID = $value;
} else {
$mailer->AddCustomHeader("{$name}: {$value}");
}
}
}
$attachments = $message->getAttachments();
if ($attachments) {
foreach ($attachments as $attachment) {
$mailer->AddStringAttachment(
$attachment->getData(),
$attachment->getFilename(),
'base64',
$attachment->getMimeType());
}
}
$text_body = $message->getTextBody();
if ($text_body !== null) {
$mailer->Body = $text_body;
}
$html_body = $message->getHTMLBody();
if ($html_body !== null) {
$mailer->IsHTML(true);
$mailer->Body = $html_body;
if ($text_body !== null) {
$mailer->AltBody = $text_body;
}
}
return $mailer;
}
/////////////////////////////////////////////////
// PROPERTIES, PUBLIC
/////////////////////////////////////////////////

View file

@ -9222,7 +9222,7 @@ phutil_register_library_map(array(
'PhabricatorMacroTransactionType' => 'PhabricatorModularTransactionType',
'PhabricatorMacroViewController' => 'PhabricatorMacroController',
'PhabricatorMailAdapter' => 'Phobject',
'PhabricatorMailAmazonSESAdapter' => 'PhabricatorMailSendmailAdapter',
'PhabricatorMailAmazonSESAdapter' => 'PhabricatorMailAdapter',
'PhabricatorMailAttachment' => 'Phobject',
'PhabricatorMailConfigTestCase' => 'PhabricatorTestCase',
'PhabricatorMailEmailEngine' => 'PhabricatorMailMessageEngine',

View file

@ -23,14 +23,8 @@ abstract class PhabricatorMailAdapter
->execute();
}
/* abstract */ public function getSupportedMessageTypes() {
throw new PhutilMethodNotImplementedException();
}
/* abstract */ public function sendMessage(
PhabricatorMailExternalMessage $message) {
throw new PhutilMethodNotImplementedException();
}
abstract public function getSupportedMessageTypes();
abstract public function sendMessage(PhabricatorMailExternalMessage $message);
/**
* Return true if this adapter supports setting a "Message-ID" when sending

View file

@ -1,21 +1,17 @@
<?php
final class PhabricatorMailAmazonSESAdapter
extends PhabricatorMailSendmailAdapter {
extends PhabricatorMailAdapter {
const ADAPTERTYPE = 'ses';
private $message;
private $isHTML;
public function prepareForSend() {
parent::prepareForSend();
$this->mailer->Mailer = 'amazon-ses';
$this->mailer->customMailer = $this;
public function getSupportedMessageTypes() {
return array(
PhabricatorMailEmailMessage::MESSAGETYPE,
);
}
public function supportsMessageIDHeader() {
// Amazon SES will ignore any Message-ID we provide.
return false;
}
@ -26,7 +22,6 @@ final class PhabricatorMailAmazonSESAdapter
'access-key' => 'string',
'secret-key' => 'string',
'endpoint' => 'string',
'encoding' => 'string',
));
}
@ -35,10 +30,27 @@ final class PhabricatorMailAmazonSESAdapter
'access-key' => null,
'secret-key' => null,
'endpoint' => null,
'encoding' => 'base64',
);
}
/**
* @phutil-external-symbol class PHPMailerLite
*/
public function sendMessage(PhabricatorMailExternalMessage $message) {
$root = phutil_get_library_root('phabricator');
$root = dirname($root);
require_once $root.'/externals/phpmailer/class.phpmailer-lite.php';
$mailer = PHPMailerLite::newFromMessage($message);
$mailer->Mailer = 'amazon-ses';
$mailer->customMailer = $this;
$mailer->Send();
}
/**
* @phutil-external-symbol class SimpleEmailService
*/

View file

@ -1,16 +1,20 @@
<?php
/**
* TODO: Should be final, but inherited by SES.
*
* @concrete-extensible
*/
class PhabricatorMailSendmailAdapter
final class PhabricatorMailSendmailAdapter
extends PhabricatorMailAdapter {
const ADAPTERTYPE = 'sendmail';
protected $mailer;
public function getSupportedMessageTypes() {
return array(
PhabricatorMailEmailMessage::MESSAGETYPE,
);
}
public function supportsMessageIDHeader() {
return true;
}
protected function validateOptions(array $options) {
PhutilTypeSpec::checkMap(
@ -29,97 +33,13 @@ class PhabricatorMailSendmailAdapter
/**
* @phutil-external-symbol class PHPMailerLite
*/
public function prepareForSend() {
public function sendMessage(PhabricatorMailExternalMessage $message) {
$root = phutil_get_library_root('phabricator');
$root = dirname($root);
require_once $root.'/externals/phpmailer/class.phpmailer-lite.php';
$this->mailer = new PHPMailerLite($use_exceptions = true);
$this->mailer->CharSet = 'utf-8';
$encoding = $this->getOption('encoding');
$this->mailer->Encoding = $encoding;
// By default, PHPMailerLite sends one mail per recipient. We handle
// combining or separating To and Cc higher in the stack, so tell it to
// send mail exactly like we ask.
$this->mailer->SingleTo = false;
}
public function supportsMessageIDHeader() {
return true;
}
public function setFrom($email, $name = '') {
$this->mailer->SetFrom($email, $name, $crazy_side_effects = false);
return $this;
}
public function addReplyTo($email, $name = '') {
$this->mailer->AddReplyTo($email, $name);
return $this;
}
public function addTos(array $emails) {
foreach ($emails as $email) {
$this->mailer->AddAddress($email);
}
return $this;
}
public function addCCs(array $emails) {
foreach ($emails as $email) {
$this->mailer->AddCC($email);
}
return $this;
}
public function addAttachment($data, $filename, $mimetype) {
$this->mailer->AddStringAttachment(
$data,
$filename,
'base64',
$mimetype);
return $this;
}
public function addHeader($header_name, $header_value) {
if (strtolower($header_name) == 'message-id') {
$this->mailer->MessageID = $header_value;
} else {
$this->mailer->AddCustomHeader($header_name.': '.$header_value);
}
return $this;
}
public function setBody($body) {
$this->mailer->Body = $body;
$this->mailer->IsHTML(false);
return $this;
}
/**
* Note: phpmailer-lite does NOT support sending messages with mixed version
* (plaintext and html). So for now lets just use HTML if it's available.
* @param $html
*/
public function setHTMLBody($html_body) {
$this->mailer->Body = $html_body;
$this->mailer->IsHTML(true);
return $this;
}
public function setSubject($subject) {
$this->mailer->Subject = $subject;
return $this;
}
public function hasValidRecipients() {
return true;
}
public function send() {
return $this->mailer->Send();
$mailer = PHPMailerLite::newFromMessage($message);
$mailer->Send();
}
}