2023-05-29 15:03:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Appwrite\Platform\Workers;
|
|
|
|
|
|
|
|
|
|
use Appwrite\Template\Template;
|
|
|
|
|
use Exception;
|
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
2023-10-20 12:40:59 +00:00
|
|
|
use Swoole\Runtime;
|
2023-05-29 15:03:09 +00:00
|
|
|
use Utopia\App;
|
|
|
|
|
use Utopia\CLI\Console;
|
|
|
|
|
use Utopia\Platform\Action;
|
|
|
|
|
use Utopia\Queue\Message;
|
2023-10-01 17:39:26 +00:00
|
|
|
use Utopia\Registry\Registry;
|
2023-05-29 15:03:09 +00:00
|
|
|
|
|
|
|
|
class Mails extends Action
|
|
|
|
|
{
|
|
|
|
|
public static function getName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'mails';
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 03:54:34 +00:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-05-29 15:03:09 +00:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this
|
|
|
|
|
->desc('Mails worker')
|
|
|
|
|
->inject('message')
|
|
|
|
|
->inject('register')
|
2023-10-20 12:43:48 +00:00
|
|
|
->callback(fn($message, $register) => $this->action($message, $register));
|
2023-05-29 15:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-02 03:54:34 +00:00
|
|
|
/**
|
2023-10-01 17:39:26 +00:00
|
|
|
* @param Message $message
|
|
|
|
|
* @param Registry $register
|
2023-06-02 03:54:34 +00:00
|
|
|
* @throws \PHPMailer\PHPMailer\Exception
|
2023-10-01 17:39:26 +00:00
|
|
|
* @return void
|
2023-06-02 03:54:34 +00:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-10-01 17:39:26 +00:00
|
|
|
public function action(Message $message, Registry $register): void
|
2023-05-29 15:03:09 +00:00
|
|
|
{
|
2023-10-20 12:42:22 +00:00
|
|
|
Runtime::setHookFlags(SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_TCP);
|
2023-05-29 15:03:09 +00:00
|
|
|
$payload = $message->getPayload() ?? [];
|
2023-06-04 08:19:49 +00:00
|
|
|
|
2023-05-29 15:03:09 +00:00
|
|
|
if (empty($payload)) {
|
|
|
|
|
throw new Exception('Missing payload');
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-01 17:39:26 +00:00
|
|
|
$smtp = $payload['smtp'];
|
|
|
|
|
|
|
|
|
|
if (empty($smtp) && empty(App::getEnv('_APP_SMTP_HOST'))) {
|
|
|
|
|
Console::info('Skipped mail processing. No SMTP configuration has been set.');
|
2023-05-29 15:03:09 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$recipient = $payload['recipient'];
|
2023-06-04 08:19:49 +00:00
|
|
|
$subject = $payload['subject'];
|
2023-10-01 17:39:26 +00:00
|
|
|
$variables = $payload['variables'];
|
2023-05-29 15:03:09 +00:00
|
|
|
$name = $payload['name'];
|
2023-10-04 23:14:27 +00:00
|
|
|
$body = $payload['body'];
|
|
|
|
|
|
|
|
|
|
$bodyTemplate = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-base.tpl');
|
|
|
|
|
$bodyTemplate->setParam('{{body}}', $body);
|
2023-10-01 17:39:26 +00:00
|
|
|
foreach ($variables as $key => $value) {
|
2023-10-04 23:14:27 +00:00
|
|
|
$bodyTemplate->setParam('{{' . $key . '}}', $value);
|
2023-10-01 17:39:26 +00:00
|
|
|
}
|
2023-10-04 23:14:27 +00:00
|
|
|
$body = $bodyTemplate->render();
|
2023-05-29 15:03:09 +00:00
|
|
|
|
2023-10-04 23:53:38 +00:00
|
|
|
$subjectTemplate = Template::fromString($subject);
|
|
|
|
|
foreach ($variables as $key => $value) {
|
|
|
|
|
$subjectTemplate->setParam('{{' . $key . '}}', $value);
|
|
|
|
|
}
|
|
|
|
|
// render() will return the subject in <p> tags, so use strip_tags() to remove them
|
|
|
|
|
$subject = \strip_tags($subjectTemplate->render());
|
|
|
|
|
|
2023-05-29 15:03:09 +00:00
|
|
|
/** @var PHPMailer $mail */
|
2023-10-01 17:39:26 +00:00
|
|
|
$mail = empty($smtp)
|
|
|
|
|
? $register->get('smtp')
|
|
|
|
|
: $this->getMailer($smtp);
|
2023-05-29 15:03:09 +00:00
|
|
|
|
|
|
|
|
$mail->clearAddresses();
|
|
|
|
|
$mail->clearAllRecipients();
|
|
|
|
|
$mail->clearReplyTos();
|
|
|
|
|
$mail->clearAttachments();
|
|
|
|
|
$mail->clearBCCs();
|
|
|
|
|
$mail->clearCCs();
|
|
|
|
|
$mail->addAddress($recipient, $name);
|
|
|
|
|
$mail->Subject = $subject;
|
|
|
|
|
$mail->Body = $body;
|
2024-01-15 10:18:17 +00:00
|
|
|
|
|
|
|
|
$mail->AltBody = $body;
|
|
|
|
|
$mail->AltBody = preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', '', $mail->AltBody);
|
|
|
|
|
$mail->AltBody = \strip_tags($mail->AltBody);
|
|
|
|
|
$mail->AltBody = \trim($mail->AltBody);
|
|
|
|
|
|
|
|
|
|
$replyTo = App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM);
|
|
|
|
|
$replyToName = \urldecode(App::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server'));
|
|
|
|
|
|
|
|
|
|
if(!empty($smtp)) {
|
|
|
|
|
if (!empty($smtp['replyTo'])) {
|
|
|
|
|
$replyTo = $smtp['replyTo'];
|
|
|
|
|
}
|
|
|
|
|
if (!empty($smtp['senderName'])) {
|
|
|
|
|
$replyToName = $smtp['senderName'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mail->addReplyTo($replyTo, $replyToName);
|
2023-05-29 15:03:09 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$mail->send();
|
|
|
|
|
} catch (\Exception $error) {
|
|
|
|
|
throw new Exception('Error sending mail: ' . $error->getMessage(), 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-01 17:39:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $smtp
|
|
|
|
|
* @return PHPMailer
|
|
|
|
|
* @throws \PHPMailer\PHPMailer\Exception
|
|
|
|
|
*/
|
|
|
|
|
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'];
|
|
|
|
|
$mail->SMTPAutoTLS = false;
|
|
|
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
|
|
|
|
|
|
$mail->setFrom($smtp['senderEmail'], $smtp['senderName']);
|
|
|
|
|
|
|
|
|
|
$mail->isHTML();
|
|
|
|
|
|
|
|
|
|
return $mail;
|
|
|
|
|
}
|
2023-05-29 15:03:09 +00:00
|
|
|
}
|