Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/adapter/PhabricatorMailSMTPAdapter.php
12256 views
1
<?php
2
3
final class PhabricatorMailSMTPAdapter
4
extends PhabricatorMailAdapter {
5
6
const ADAPTERTYPE = 'smtp';
7
8
public function getSupportedMessageTypes() {
9
return array(
10
PhabricatorMailEmailMessage::MESSAGETYPE,
11
);
12
}
13
14
public function supportsMessageIDHeader() {
15
return $this->guessIfHostSupportsMessageID(
16
$this->getOption('message-id'),
17
$this->getOption('host'));
18
}
19
20
protected function validateOptions(array $options) {
21
PhutilTypeSpec::checkMap(
22
$options,
23
array(
24
'host' => 'string|null',
25
'port' => 'int',
26
'user' => 'string|null',
27
'password' => 'string|null',
28
'protocol' => 'string|null',
29
'message-id' => 'bool|null',
30
));
31
}
32
33
public function newDefaultOptions() {
34
return array(
35
'host' => null,
36
'port' => 25,
37
'user' => null,
38
'password' => null,
39
'protocol' => null,
40
'message-id' => null,
41
);
42
}
43
44
/**
45
* @phutil-external-symbol class PHPMailer
46
*/
47
public function sendMessage(PhabricatorMailExternalMessage $message) {
48
$root = phutil_get_library_root('phabricator');
49
$root = dirname($root);
50
require_once $root.'/externals/phpmailer/class.phpmailer.php';
51
$smtp = new PHPMailer($use_exceptions = true);
52
53
$smtp->CharSet = 'utf-8';
54
$smtp->Encoding = 'base64';
55
56
// By default, PHPMailer sends one mail per recipient. We handle
57
// combining or separating To and Cc higher in the stack, so tell it to
58
// send mail exactly like we ask.
59
$smtp->SingleTo = false;
60
61
$smtp->IsSMTP();
62
$smtp->Host = $this->getOption('host');
63
$smtp->Port = $this->getOption('port');
64
$user = $this->getOption('user');
65
if (strlen($user)) {
66
$smtp->SMTPAuth = true;
67
$smtp->Username = $user;
68
$smtp->Password = $this->getOption('password');
69
}
70
71
$protocol = $this->getOption('protocol');
72
if ($protocol) {
73
$protocol = phutil_utf8_strtolower($protocol);
74
$smtp->SMTPSecure = $protocol;
75
}
76
77
$subject = $message->getSubject();
78
if ($subject !== null) {
79
$smtp->Subject = $subject;
80
}
81
82
$from_address = $message->getFromAddress();
83
if ($from_address) {
84
$smtp->SetFrom(
85
$from_address->getAddress(),
86
(string)$from_address->getDisplayName(),
87
$crazy_side_effects = false);
88
}
89
90
$reply_address = $message->getReplyToAddress();
91
if ($reply_address) {
92
$smtp->AddReplyTo(
93
$reply_address->getAddress(),
94
(string)$reply_address->getDisplayName());
95
}
96
97
$to_addresses = $message->getToAddresses();
98
if ($to_addresses) {
99
foreach ($to_addresses as $address) {
100
$smtp->AddAddress(
101
$address->getAddress(),
102
(string)$address->getDisplayName());
103
}
104
}
105
106
$cc_addresses = $message->getCCAddresses();
107
if ($cc_addresses) {
108
foreach ($cc_addresses as $address) {
109
$smtp->AddCC(
110
$address->getAddress(),
111
(string)$address->getDisplayName());
112
}
113
}
114
115
$headers = $message->getHeaders();
116
if ($headers) {
117
$list = array();
118
foreach ($headers as $header) {
119
$name = $header->getName();
120
$value = $header->getValue();
121
122
if (phutil_utf8_strtolower($name) === 'message-id') {
123
$smtp->MessageID = $value;
124
} else {
125
$smtp->AddCustomHeader("{$name}: {$value}");
126
}
127
}
128
}
129
130
$text_body = $message->getTextBody();
131
if ($text_body !== null) {
132
$smtp->Body = $text_body;
133
}
134
135
$html_body = $message->getHTMLBody();
136
if ($html_body !== null) {
137
$smtp->IsHTML(true);
138
$smtp->Body = $html_body;
139
if ($text_body !== null) {
140
$smtp->AltBody = $text_body;
141
}
142
}
143
144
$attachments = $message->getAttachments();
145
if ($attachments) {
146
foreach ($attachments as $attachment) {
147
$smtp->AddStringAttachment(
148
$attachment->getData(),
149
$attachment->getFilename(),
150
'base64',
151
$attachment->getMimeType());
152
}
153
}
154
155
$smtp->Send();
156
}
157
158
}
159
160