2020-06-13 18:30:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
2021-06-11 14:20:18 +00:00
|
|
|
use Appwrite\Resque\Worker;
|
2023-08-27 22:45:37 +00:00
|
|
|
use Appwrite\Template\Template;
|
2020-07-05 21:54:41 +00:00
|
|
|
use Utopia\App;
|
2020-11-20 23:44:00 +00:00
|
|
|
use Utopia\CLI\Console;
|
2023-03-13 01:35:30 +00:00
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
2020-07-05 21:54:41 +00:00
|
|
|
|
2021-08-13 08:39:46 +00:00
|
|
|
require_once __DIR__ . '/../init.php';
|
2020-06-13 18:30:44 +00:00
|
|
|
|
2021-01-15 06:02:48 +00:00
|
|
|
Console::title('Mails V1 Worker');
|
2021-07-29 18:46:06 +00:00
|
|
|
Console::success(APP_NAME . ' mails worker v1 has started' . "\n");
|
2020-06-13 18:30:44 +00:00
|
|
|
|
2021-06-11 14:20:18 +00:00
|
|
|
class MailsV1 extends Worker
|
2020-06-13 18:30:44 +00:00
|
|
|
{
|
2022-05-23 14:54:50 +00:00
|
|
|
public function getName(): string
|
|
|
|
|
{
|
2021-11-23 14:24:25 +00:00
|
|
|
return "mails";
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 14:20:18 +00:00
|
|
|
public function init(): void
|
2020-06-13 18:30:44 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 14:20:18 +00:00
|
|
|
public function run(): void
|
2020-06-13 18:30:44 +00:00
|
|
|
{
|
|
|
|
|
global $register;
|
|
|
|
|
|
2023-03-13 01:35:30 +00:00
|
|
|
$smtp = $this->args['smtp'];
|
|
|
|
|
|
2023-07-18 07:08:02 +00:00
|
|
|
if (empty($smtp) && empty(App::getEnv('_APP_SMTP_HOST'))) {
|
2023-03-13 01:35:30 +00:00
|
|
|
Console::info('Skipped mail processing. No SMTP configuration has been set.');
|
2020-11-20 23:44:00 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 12:39:31 +00:00
|
|
|
$recipient = $this->args['recipient'];
|
2022-12-14 06:23:14 +00:00
|
|
|
$subject = $this->args['subject'];
|
|
|
|
|
$body = $this->args['body'];
|
2023-08-27 22:45:37 +00:00
|
|
|
$variables = $this->args['variables'];
|
2023-08-31 00:20:20 +00:00
|
|
|
$name = $this->args['name'];
|
2021-07-29 18:46:06 +00:00
|
|
|
|
2023-08-27 22:45:37 +00:00
|
|
|
$body = Template::fromFile(__DIR__ . '/../config/locale/templates/email-base.tpl');
|
|
|
|
|
|
|
|
|
|
foreach ($variables as $key => $value) {
|
2023-08-28 05:53:26 +00:00
|
|
|
$body->setParam('{{' . $key . '}}', $value);
|
2023-08-27 22:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$body = $body->render();
|
|
|
|
|
|
2023-08-30 04:30:44 +00:00
|
|
|
/** @var PHPMailer $mail */
|
|
|
|
|
$mail = empty($smtp)
|
|
|
|
|
? $register->get('smtp')
|
|
|
|
|
: $this->getMailer($smtp);
|
2020-07-05 21:54:41 +00:00
|
|
|
|
|
|
|
|
$mail->clearAddresses();
|
|
|
|
|
$mail->clearAllRecipients();
|
|
|
|
|
$mail->clearReplyTos();
|
|
|
|
|
$mail->clearAttachments();
|
|
|
|
|
$mail->clearBCCs();
|
|
|
|
|
$mail->clearCCs();
|
2020-06-13 18:30:44 +00:00
|
|
|
$mail->addAddress($recipient, $name);
|
|
|
|
|
$mail->Subject = $subject;
|
|
|
|
|
$mail->Body = $body;
|
2020-06-20 11:20:49 +00:00
|
|
|
$mail->AltBody = \strip_tags($body);
|
2020-06-13 18:30:44 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$mail->send();
|
|
|
|
|
} catch (\Exception $error) {
|
|
|
|
|
throw new Exception('Error sending mail: ' . $error->getMessage(), 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 01:35:30 +00: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;
|
2023-08-30 04:30:44 +00:00
|
|
|
$mail->SMTPSecure = $smtp['secure'];
|
2023-03-13 01:35:30 +00:00
|
|
|
$mail->SMTPAutoTLS = false;
|
|
|
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
|
|
2023-08-30 04:30:44 +00:00
|
|
|
$mail->setFrom($smtp['senderEmail'], $smtp['senderName']);
|
|
|
|
|
|
|
|
|
|
if (!empty($smtp['replyTo'])) {
|
|
|
|
|
$mail->addReplyTo($smtp['replyTo'], $smtp['senderName']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mail->isHTML();
|
2023-03-13 01:35:30 +00:00
|
|
|
|
|
|
|
|
return $mail;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 14:20:18 +00:00
|
|
|
public function shutdown(): void
|
2020-06-13 18:30:44 +00:00
|
|
|
{
|
|
|
|
|
}
|
2021-07-29 18:46:06 +00:00
|
|
|
}
|