Path: blob/master/src/applications/metamta/adapter/PhabricatorMailSMTPAdapter.php
12256 views
<?php12final class PhabricatorMailSMTPAdapter3extends PhabricatorMailAdapter {45const ADAPTERTYPE = 'smtp';67public function getSupportedMessageTypes() {8return array(9PhabricatorMailEmailMessage::MESSAGETYPE,10);11}1213public function supportsMessageIDHeader() {14return $this->guessIfHostSupportsMessageID(15$this->getOption('message-id'),16$this->getOption('host'));17}1819protected function validateOptions(array $options) {20PhutilTypeSpec::checkMap(21$options,22array(23'host' => 'string|null',24'port' => 'int',25'user' => 'string|null',26'password' => 'string|null',27'protocol' => 'string|null',28'message-id' => 'bool|null',29));30}3132public function newDefaultOptions() {33return array(34'host' => null,35'port' => 25,36'user' => null,37'password' => null,38'protocol' => null,39'message-id' => null,40);41}4243/**44* @phutil-external-symbol class PHPMailer45*/46public function sendMessage(PhabricatorMailExternalMessage $message) {47$root = phutil_get_library_root('phabricator');48$root = dirname($root);49require_once $root.'/externals/phpmailer/class.phpmailer.php';50$smtp = new PHPMailer($use_exceptions = true);5152$smtp->CharSet = 'utf-8';53$smtp->Encoding = 'base64';5455// By default, PHPMailer sends one mail per recipient. We handle56// combining or separating To and Cc higher in the stack, so tell it to57// send mail exactly like we ask.58$smtp->SingleTo = false;5960$smtp->IsSMTP();61$smtp->Host = $this->getOption('host');62$smtp->Port = $this->getOption('port');63$user = $this->getOption('user');64if (strlen($user)) {65$smtp->SMTPAuth = true;66$smtp->Username = $user;67$smtp->Password = $this->getOption('password');68}6970$protocol = $this->getOption('protocol');71if ($protocol) {72$protocol = phutil_utf8_strtolower($protocol);73$smtp->SMTPSecure = $protocol;74}7576$subject = $message->getSubject();77if ($subject !== null) {78$smtp->Subject = $subject;79}8081$from_address = $message->getFromAddress();82if ($from_address) {83$smtp->SetFrom(84$from_address->getAddress(),85(string)$from_address->getDisplayName(),86$crazy_side_effects = false);87}8889$reply_address = $message->getReplyToAddress();90if ($reply_address) {91$smtp->AddReplyTo(92$reply_address->getAddress(),93(string)$reply_address->getDisplayName());94}9596$to_addresses = $message->getToAddresses();97if ($to_addresses) {98foreach ($to_addresses as $address) {99$smtp->AddAddress(100$address->getAddress(),101(string)$address->getDisplayName());102}103}104105$cc_addresses = $message->getCCAddresses();106if ($cc_addresses) {107foreach ($cc_addresses as $address) {108$smtp->AddCC(109$address->getAddress(),110(string)$address->getDisplayName());111}112}113114$headers = $message->getHeaders();115if ($headers) {116$list = array();117foreach ($headers as $header) {118$name = $header->getName();119$value = $header->getValue();120121if (phutil_utf8_strtolower($name) === 'message-id') {122$smtp->MessageID = $value;123} else {124$smtp->AddCustomHeader("{$name}: {$value}");125}126}127}128129$text_body = $message->getTextBody();130if ($text_body !== null) {131$smtp->Body = $text_body;132}133134$html_body = $message->getHTMLBody();135if ($html_body !== null) {136$smtp->IsHTML(true);137$smtp->Body = $html_body;138if ($text_body !== null) {139$smtp->AltBody = $text_body;140}141}142143$attachments = $message->getAttachments();144if ($attachments) {145foreach ($attachments as $attachment) {146$smtp->AddStringAttachment(147$attachment->getData(),148$attachment->getFilename(),149'base64',150$attachment->getMimeType());151}152}153154$smtp->Send();155}156157}158159160