-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathSMTP.php
More file actions
147 lines (123 loc) · 5.13 KB
/
SMTP.php
File metadata and controls
147 lines (123 loc) · 5.13 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
<?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.
*/
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
) {
if (!\in_array($this->smtpSecure, ['', 'ssl', 'tls'])) {
throw new \InvalidArgumentException('Invalid SMTP secure prefix. Must be "", "ssl" or "tls"');
}
}
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());
$mail = new PHPMailer();
$mail->isSMTP();
$mail->XMailer = $this->xMailer;
$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->CharSet = 'UTF-8';
$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);
}
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) {
$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) {
$mail->addStringAttachment(
string: \file_get_contents($attachment->getPath()),
filename: $attachment->getName(),
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, $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();
}
}