From 51a7d9f94131ef4ac7e633264ce27220eb94f260 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 31 Jan 2024 23:53:19 +1300 Subject: [PATCH 1/3] Add SMTP adapter --- src/Utopia/Messaging/Adapter/Email/SMTP.php | 89 +++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/Utopia/Messaging/Adapter/Email/SMTP.php diff --git a/src/Utopia/Messaging/Adapter/Email/SMTP.php b/src/Utopia/Messaging/Adapter/Email/SMTP.php new file mode 100644 index 0000000..9ccc4fe --- /dev/null +++ b/src/Utopia/Messaging/Adapter/Email/SMTP.php @@ -0,0 +1,89 @@ +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->CharSet = 'UTF-8'; + $mail->Subject = $message->getSubject(); + $mail->Body = $message->getContent(); + $mail->AltBody = \strip_tags($message->getContent()); + $mail->setFrom($message->getFromEmail(), $message->getFromName()); + $mail->addReplyTo($message->getReplyToEmail(), $message->getReplyToName()); + $mail->isHTML($message->isHtml()); + + foreach ($message->getTo() as $to) { + $mail->addAddress($to); + } + + if (!$mail->send()) { + foreach ($message->getTo() as $to) { + $response->addResultForRecipient($to, $mail->ErrorInfo); + } + return $response->toArray(); + } + + $response->setDeliveredTo(\count($message->getTo())); + foreach ($message->getTo() as $to) { + $response->addResultForRecipient($to); + } + + return $response->toArray(); + } +} From 3b932da6a9c6b8857040d4141d990fc160aaa672 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 1 Feb 2024 00:28:21 +1300 Subject: [PATCH 2/3] Simplify --- src/Utopia/Messaging/Adapter/Email/SMTP.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Utopia/Messaging/Adapter/Email/SMTP.php b/src/Utopia/Messaging/Adapter/Email/SMTP.php index 9ccc4fe..8a2d709 100644 --- a/src/Utopia/Messaging/Adapter/Email/SMTP.php +++ b/src/Utopia/Messaging/Adapter/Email/SMTP.php @@ -72,16 +72,14 @@ protected function process(EmailMessage $message): array $mail->addAddress($to); } - if (!$mail->send()) { - foreach ($message->getTo() as $to) { - $response->addResultForRecipient($to, $mail->ErrorInfo); - } - return $response->toArray(); + $sent = $mail->send(); + + if ($sent) { + $response->setDeliveredTo(\count($message->getTo())); } - $response->setDeliveredTo(\count($message->getTo())); foreach ($message->getTo() as $to) { - $response->addResultForRecipient($to); + $response->addResultForRecipient($to, $sent ? '' : $mail->ErrorInfo); } return $response->toArray(); From b7d0e440cea757d529109f25c60eb5324ffb2cca Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 1 Feb 2024 00:42:47 +1300 Subject: [PATCH 3/3] Strip tags fix --- src/Utopia/Messaging/Adapter/Email/SMTP.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Utopia/Messaging/Adapter/Email/SMTP.php b/src/Utopia/Messaging/Adapter/Email/SMTP.php index 8a2d709..7a5a240 100644 --- a/src/Utopia/Messaging/Adapter/Email/SMTP.php +++ b/src/Utopia/Messaging/Adapter/Email/SMTP.php @@ -63,11 +63,15 @@ protected function process(EmailMessage $message): array $mail->CharSet = 'UTF-8'; $mail->Subject = $message->getSubject(); $mail->Body = $message->getContent(); - $mail->AltBody = \strip_tags($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>/is', '', $mail->Body); + $mail->AltBody = \strip_tags($mail->AltBody); + $mail->AltBody = \trim($mail->AltBody); + foreach ($message->getTo() as $to) { $mail->addAddress($to); }