Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/adapter/PhabricatorMailAdapter.php
12256 views
1
<?php
2
3
abstract class PhabricatorMailAdapter
4
extends Phobject {
5
6
private $key;
7
private $priority;
8
private $media;
9
private $options = array();
10
11
private $supportsInbound = true;
12
private $supportsOutbound = true;
13
private $mediaMap;
14
15
final public function getAdapterType() {
16
return $this->getPhobjectClassConstant('ADAPTERTYPE');
17
}
18
19
final public static function getAllAdapters() {
20
return id(new PhutilClassMapQuery())
21
->setAncestorClass(__CLASS__)
22
->setUniqueMethod('getAdapterType')
23
->execute();
24
}
25
26
abstract public function getSupportedMessageTypes();
27
abstract public function sendMessage(PhabricatorMailExternalMessage $message);
28
29
/**
30
* Return true if this adapter supports setting a "Message-ID" when sending
31
* email.
32
*
33
* This is an ugly implementation detail because mail threading is a horrible
34
* mess, implemented differently by every client in existence.
35
*/
36
public function supportsMessageIDHeader() {
37
return false;
38
}
39
40
final public function supportsMessageType($message_type) {
41
if ($this->mediaMap === null) {
42
$media_map = $this->getSupportedMessageTypes();
43
$media_map = array_fuse($media_map);
44
45
if ($this->media) {
46
$config_map = $this->media;
47
$config_map = array_fuse($config_map);
48
49
$media_map = array_intersect_key($media_map, $config_map);
50
}
51
52
$this->mediaMap = $media_map;
53
}
54
55
return isset($this->mediaMap[$message_type]);
56
}
57
58
final public function setMedia(array $media) {
59
$native_map = $this->getSupportedMessageTypes();
60
$native_map = array_fuse($native_map);
61
62
foreach ($media as $medium) {
63
if (!isset($native_map[$medium])) {
64
throw new Exception(
65
pht(
66
'Adapter ("%s") is configured for medium "%s", but this is not '.
67
'a supported delivery medium. Supported media are: %s.',
68
get_class($this),
69
$medium,
70
implode(', ', $native_map)));
71
}
72
}
73
74
$this->media = $media;
75
$this->mediaMap = null;
76
return $this;
77
}
78
79
final public function getMedia() {
80
return $this->media;
81
}
82
83
final public function setKey($key) {
84
$this->key = $key;
85
return $this;
86
}
87
88
final public function getKey() {
89
return $this->key;
90
}
91
92
final public function setPriority($priority) {
93
$this->priority = $priority;
94
return $this;
95
}
96
97
final public function getPriority() {
98
return $this->priority;
99
}
100
101
final public function setSupportsInbound($supports_inbound) {
102
$this->supportsInbound = $supports_inbound;
103
return $this;
104
}
105
106
final public function getSupportsInbound() {
107
return $this->supportsInbound;
108
}
109
110
final public function setSupportsOutbound($supports_outbound) {
111
$this->supportsOutbound = $supports_outbound;
112
return $this;
113
}
114
115
final public function getSupportsOutbound() {
116
return $this->supportsOutbound;
117
}
118
119
final public function getOption($key) {
120
if (!array_key_exists($key, $this->options)) {
121
throw new Exception(
122
pht(
123
'Mailer ("%s") is attempting to access unknown option ("%s").',
124
get_class($this),
125
$key));
126
}
127
128
return $this->options[$key];
129
}
130
131
final public function setOptions(array $options) {
132
$this->validateOptions($options);
133
$this->options = $options;
134
return $this;
135
}
136
137
abstract protected function validateOptions(array $options);
138
139
abstract public function newDefaultOptions();
140
141
final protected function guessIfHostSupportsMessageID($config, $host) {
142
// See T13265. Mailers like "SMTP" and "sendmail" usually allow us to
143
// set the "Message-ID" header to a value we choose, but we may not be
144
// able to if the mailer is being used as API glue and the outbound
145
// pathway ends up routing to a service with an SMTP API that selects
146
// its own "Message-ID" header, like Amazon SES.
147
148
// If users configured a behavior explicitly, use that behavior.
149
if ($config !== null) {
150
return $config;
151
}
152
153
// If the server we're connecting to is part of a service that we know
154
// does not support "Message-ID", guess that we don't support "Message-ID".
155
if ($host !== null) {
156
$host_blocklist = array(
157
'/\.amazonaws\.com\z/',
158
'/\.postmarkapp\.com\z/',
159
'/\.sendgrid\.net\z/',
160
);
161
162
$host = phutil_utf8_strtolower($host);
163
foreach ($host_blocklist as $regexp) {
164
if (preg_match($regexp, $host)) {
165
return false;
166
}
167
}
168
}
169
170
return true;
171
}
172
173
174
}
175
176