diff --git a/app/workers/mails.php b/app/workers/mails.php index e850885ddc..2f4a5d9534 100644 --- a/app/workers/mails.php +++ b/app/workers/mails.php @@ -3,6 +3,7 @@ use Appwrite\Resque\Worker; use Utopia\App; use Utopia\CLI\Console; +use PHPMailer\PHPMailer\PHPMailer; require_once __DIR__ . '/../init.php'; @@ -24,8 +25,10 @@ class MailsV1 extends Worker { global $register; + $smtp = $this->args['smtp']; + if (empty(App::getEnv('_APP_SMTP_HOST'))) { - Console::info('Skipped mail processing. No SMTP server hostname has been set.'); + Console::info('Skipped mail processing. No SMTP configuration has been set.'); return; } @@ -37,17 +40,7 @@ class MailsV1 extends Worker $from = $this->args['from']; /** @var \PHPMailer\PHPMailer\PHPMailer $mail */ - $mail = $register->get('smtp'); - - // Set project mail - /*$register->get('smtp') - ->setFrom( - App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM), - ($project->getId() === 'console') - ? \urldecode(App::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME.' Server')) - : \sprintf(Locale::getText('account.emails.team'), $project->getAttribute('name') - ) - );*/ + $mail = empty($smtp) ? $register->get('smtp') : $this->getMailer($smtp); $mail->clearAddresses(); $mail->clearAllRecipients(); @@ -58,6 +51,9 @@ class MailsV1 extends Worker $mail->setFrom(App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM), (empty($from) ? \urldecode(App::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server')) : $from)); $mail->addAddress($recipient, $name); + if (isset($smtp['replyTo'])) { + $mail->addReplyTo($smtp['replyTo']); + } $mail->Subject = $subject; $mail->Body = $body; $mail->AltBody = \strip_tags($body); @@ -69,6 +65,36 @@ class MailsV1 extends Worker } } + protected function getMailer(array $smtp): PHPMailer + { + $mail = new PHPMailer(true); + + $mail->isSMTP(); + + $username = $smtp['username']; + $password = $smtp['password']; + + $mail->XMailer = 'Appwrite Mailer'; + $mail->Host = $smtp['host']; + $mail->Port = $smtp['port']; + $mail->SMTPAuth = (!empty($username) && !empty($password)); + $mail->Username = $username; + $mail->Password = $password; + $mail->SMTPSecure = $smtp['secure'] === 'tls'; + $mail->SMTPAutoTLS = false; + $mail->CharSet = 'UTF-8'; + + $from = \urldecode($smtp['senderName'] ?? App::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server')); + $email = $smtp['senderEmail'] ?? App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM); + + $mail->setFrom($email, $from); + $mail->addReplyTo($email, $from); + + $mail->isHTML(true); + + return $mail; + } + public function shutdown(): void { }