1
0
Fork 0
mirror of synced 2024-07-07 23:46:11 +12:00
appwrite/app/workers/mails.php

97 lines
2.5 KiB
PHP
Raw Normal View History

2020-06-14 06:30:44 +12:00
<?php
use Appwrite\Resque\Worker;
2020-07-06 09:54:41 +12:00
use Utopia\App;
2020-11-21 12:44:00 +13:00
use Utopia\CLI\Console;
2023-03-13 14:35:30 +13:00
use PHPMailer\PHPMailer\PHPMailer;
2020-07-06 09:54:41 +12:00
require_once __DIR__ . '/../init.php';
2020-06-14 06:30:44 +12:00
2021-01-15 19:02:48 +13:00
Console::title('Mails V1 Worker');
Console::success(APP_NAME . ' mails worker v1 has started' . "\n");
2020-06-14 06:30:44 +12:00
class MailsV1 extends Worker
2020-06-14 06:30:44 +12:00
{
2022-05-24 02:54:50 +12:00
public function getName(): string
{
return "mails";
}
public function init(): void
2020-06-14 06:30:44 +12:00
{
}
public function run(): void
2020-06-14 06:30:44 +12:00
{
global $register;
2023-03-13 14:35:30 +13:00
$smtp = $this->args['smtp'];
2023-07-18 19:08:02 +12:00
if (empty($smtp) && empty(App::getEnv('_APP_SMTP_HOST'))) {
2023-03-13 14:35:30 +13:00
Console::info('Skipped mail processing. No SMTP configuration has been set.');
2020-11-21 12:44:00 +13:00
return;
}
2022-04-04 18:30:07 +12:00
$recipient = $this->args['recipient'];
$subject = $this->args['subject'];
$name = $this->args['name'];
$body = $this->args['body'];
$from = $this->args['from'];
2020-07-06 09:54:41 +12:00
/** @var \PHPMailer\PHPMailer\PHPMailer $mail */
2023-03-13 14:35:30 +13:00
$mail = empty($smtp) ? $register->get('smtp') : $this->getMailer($smtp);
2020-07-06 09:54:41 +12:00
$mail->clearAddresses();
$mail->clearAllRecipients();
$mail->clearReplyTos();
$mail->clearAttachments();
$mail->clearBCCs();
$mail->clearCCs();
2020-06-14 06:30:44 +12:00
$mail->addAddress($recipient, $name);
$mail->Subject = $subject;
$mail->Body = $body;
2020-06-20 23:20:49 +12:00
$mail->AltBody = \strip_tags($body);
2020-06-14 06:30:44 +12:00
try {
$mail->send();
} catch (\Exception $error) {
throw new Exception('Error sending mail: ' . $error->getMessage(), 500);
}
}
2023-03-13 14:35:30 +13:00
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
2020-06-14 06:30:44 +12:00
{
}
}