Skip to content

Commit df54ba5

Browse files
authored
Merge pull request #76 from utopia-php/feat-smtp-adapter
Add SMTP adapter
2 parents ee731b9 + b7d0e44 commit df54ba5

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

  • src/Utopia/Messaging/Adapter/Email
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Utopia\Messaging\Adapter\Email;
4+
5+
use PHPMailer\PHPMailer\PHPMailer;
6+
use Utopia\Messaging\Adapter\Email as EmailAdapter;
7+
use Utopia\Messaging\Messages\Email as EmailMessage;
8+
use Utopia\Messaging\Response;
9+
10+
class SMTP extends EmailAdapter
11+
{
12+
protected const NAME = 'SMTP';
13+
14+
/**
15+
* @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.
16+
* @param int $port The default SMTP server port.
17+
* @param string $username Authentication username.
18+
* @param string $password Authentication password.
19+
* @param string $smtpSecure SMTP Secure prefix. Can be '', 'ssl' or 'tls'
20+
* @param bool $smtpAutoTLS Enable/disable SMTP AutoTLS feature. Defaults to false.
21+
* @param string $xMailer The value to use for the X-Mailer header.
22+
*/
23+
public function __construct(
24+
private string $host,
25+
private int $port = 25,
26+
private string $username = '',
27+
private string $password = '',
28+
private string $smtpSecure = '',
29+
private bool $smtpAutoTLS = false,
30+
private string $xMailer = ''
31+
) {
32+
if (!\in_array($this->smtpSecure, ['', 'ssl', 'tls'])) {
33+
throw new \InvalidArgumentException('Invalid SMTP secure prefix. Must be "", "ssl" or "tls"');
34+
}
35+
}
36+
37+
public function getName(): string
38+
{
39+
return static::NAME;
40+
}
41+
42+
public function getMaxMessagesPerRequest(): int
43+
{
44+
return 1000;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
protected function process(EmailMessage $message): array
51+
{
52+
$response = new Response($this->getType());
53+
$mail = new PHPMailer();
54+
$mail->isSMTP();
55+
$mail->XMailer = $this->xMailer;
56+
$mail->Host = $this->host;
57+
$mail->Port = $this->port;
58+
$mail->SMTPAuth = !empty($this->username) && !empty($this->password);
59+
$mail->Username = $this->username;
60+
$mail->Password = $this->password;
61+
$mail->SMTPSecure = $this->smtpSecure;
62+
$mail->SMTPAutoTLS = $this->smtpAutoTLS;
63+
$mail->CharSet = 'UTF-8';
64+
$mail->Subject = $message->getSubject();
65+
$mail->Body = $message->getContent();
66+
$mail->setFrom($message->getFromEmail(), $message->getFromName());
67+
$mail->addReplyTo($message->getReplyToEmail(), $message->getReplyToName());
68+
$mail->isHTML($message->isHtml());
69+
70+
// Strip tags misses style tags, so we use regex to remove them
71+
$mail->AltBody = \preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', '', $mail->Body);
72+
$mail->AltBody = \strip_tags($mail->AltBody);
73+
$mail->AltBody = \trim($mail->AltBody);
74+
75+
foreach ($message->getTo() as $to) {
76+
$mail->addAddress($to);
77+
}
78+
79+
$sent = $mail->send();
80+
81+
if ($sent) {
82+
$response->setDeliveredTo(\count($message->getTo()));
83+
}
84+
85+
foreach ($message->getTo() as $to) {
86+
$response->addResultForRecipient($to, $sent ? '' : $mail->ErrorInfo);
87+
}
88+
89+
return $response->toArray();
90+
}
91+
}

0 commit comments

Comments
 (0)