-
Notifications
You must be signed in to change notification settings - Fork 66
Add SMTP adapter #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add SMTP adapter #76
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| <?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. | ||
| */ | ||
| 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 = '' | ||
| ) { | ||
| 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->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); | ||
abnegate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| $sent = $mail->send(); | ||
|
|
||
| if ($sent) { | ||
| $response->setDeliveredTo(\count($message->getTo())); | ||
| } | ||
|
|
||
| foreach ($message->getTo() as $to) { | ||
| $response->addResultForRecipient($to, $sent ? '' : $mail->ErrorInfo); | ||
| } | ||
|
|
||
| return $response->toArray(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.