-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathSMTP.php
More file actions
182 lines (154 loc) · 6.55 KB
/
SMTP.php
File metadata and controls
182 lines (154 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Utopia\Messaging\Adapter\Email;
use PHPMailer\PHPMailer\PHPMailer;
use Utopia\Messaging\Adapter\Email as EmailAdapter;
use Utopia\Messaging\Messages\Email as EmailMessage;
use Utopia\Messaging\Response;
class SMTP extends EmailAdapter
{
protected const NAME = 'SMTP';
/**
* @param string $host SMTP hosts. Either a single hostname or multiple semicolon-delimited hostnames. You can also specify a different port for each host by using this format: [hostname:port] (e.g. "smtp1.example.com:25;smtp2.example.com"). You can also specify encryption type, for example: (e.g. "tls://smtp1.example.com:587;ssl://smtp2.example.com:465"). Hosts will be tried in order.
* @param int $port The default SMTP server port.
* @param string $username Authentication username.
* @param string $password Authentication password.
* @param string $smtpSecure SMTP Secure prefix. Can be '', 'ssl' or 'tls'
* @param bool $smtpAutoTLS Enable/disable SMTP AutoTLS feature. Defaults to false.
* @param string $xMailer The value to use for the X-Mailer header.
* @param int $timeout SMTP timeout in seconds.
* @param bool $keepAlive Whether to reuse the SMTP connection across process() calls.
* @param int $timelimit SMTP command timelimit in seconds.
*/
public function __construct(
private string $host,
private int $port = 25,
private string $username = '',
private string $password = '',
private string $smtpSecure = '',
private bool $smtpAutoTLS = false,
private string $xMailer = '',
private int $timeout = 30,
private bool $keepAlive = false,
private int $timelimit = 30,
) {
if (!\in_array($this->smtpSecure, ['', 'ssl', 'tls'])) {
throw new \InvalidArgumentException('Invalid SMTP secure prefix. Must be "", "ssl" or "tls"');
}
}
private ?PHPMailer $mail = null;
public function getName(): string
{
return static::NAME;
}
public function getMaxMessagesPerRequest(): int
{
return 1000;
}
/**
* {@inheritdoc}
*/
protected function process(EmailMessage $message): array
{
$response = new Response($this->getType());
if ($this->keepAlive && $this->mail !== null) {
$mail = $this->mail;
$mail->clearAllRecipients();
$mail->clearReplyTos();
$mail->clearAttachments();
} else {
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = $this->host;
$mail->Port = $this->port;
$mail->SMTPAuth = !empty($this->username) && !empty($this->password);
$mail->Username = $this->username;
$mail->Password = $this->password;
$mail->SMTPSecure = $this->smtpSecure;
$mail->SMTPAutoTLS = $this->smtpAutoTLS;
$mail->Timeout = $this->timeout;
$mail->SMTPKeepAlive = $this->keepAlive;
if ($this->keepAlive) {
$this->mail = $mail;
}
}
$mail->XMailer = $this->xMailer;
$mail->CharSet = 'UTF-8';
$mail->getSMTPInstance()->Timelimit = $this->timelimit;
$mail->Subject = $message->getSubject();
$mail->Body = $message->getContent();
$mail->setFrom($message->getFromEmail(), $message->getFromName());
$mail->addReplyTo($message->getReplyToEmail(), $message->getReplyToName());
$mail->isHTML($message->isHtml());
// Strip tags misses style tags, so we use regex to remove them
$mail->AltBody = \preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', '', $mail->Body);
$mail->AltBody = \strip_tags($mail->AltBody);
$mail->AltBody = \trim($mail->AltBody);
foreach ($message->getTo() as $to) {
$mail->addAddress($to['email'], $to['name'] ?? '');
}
if (!empty($message->getCC())) {
foreach ($message->getCC() as $cc) {
$mail->addCC($cc['email'], $cc['name'] ?? '');
}
}
if (!empty($message->getBCC())) {
foreach ($message->getBCC() as $bcc) {
$mail->addBCC($bcc['email'], $bcc['name'] ?? '');
}
}
if (!empty($message->getAttachments())) {
$size = 0;
foreach ($message->getAttachments() as $attachment) {
if ($attachment->getContent() !== null) {
$size += \strlen($attachment->getContent());
} else {
$size += \filesize($attachment->getPath());
}
}
if ($size > self::MAX_ATTACHMENT_BYTES) {
throw new \Exception('Attachments size exceeds the maximum allowed size of 25MB');
}
foreach ($message->getAttachments() as $attachment) {
if ($attachment->getContent() !== null) {
$mail->addStringAttachment(
string: $attachment->getContent(),
filename: $attachment->getName(),
encoding: PHPMailer::ENCODING_BASE64,
type: $attachment->getType()
);
} else {
$mail->addStringAttachment(
string: \file_get_contents($attachment->getPath()),
filename: $attachment->getName(),
encoding: PHPMailer::ENCODING_BASE64,
type: $attachment->getType()
);
}
}
}
$sent = $mail->send();
if ($sent) {
$totalDelivered = \count($message->getTo()) + \count($message->getCC() ?: []) + \count($message->getBCC() ?: []);
$response->setDeliveredTo($totalDelivered);
}
foreach ($message->getTo() as $to) {
$error = empty($mail->ErrorInfo)
? 'Unknown error'
: $mail->ErrorInfo;
$response->addResult($to['email'], $sent ? '' : $error);
}
foreach ($message->getCC() ?? [] as $cc) {
$error = empty($mail->ErrorInfo)
? 'Unknown error'
: $mail->ErrorInfo;
$response->addResult($cc['email'], $sent ? '' : $error);
}
foreach ($message->getBCC() ?? [] as $bcc) {
$error = empty($mail->ErrorInfo)
? 'Unknown error'
: $mail->ErrorInfo;
$response->addResult($bcc['email'], $sent ? '' : $error);
}
return $response->toArray();
}
}