2020-06-14 06:30:44 +12:00
|
|
|
<?php
|
|
|
|
|
2021-06-12 02:20:18 +12:00
|
|
|
use Appwrite\Resque\Worker;
|
2021-07-28 08:31:19 +12:00
|
|
|
use Appwrite\Template\Template;
|
2020-07-06 09:54:41 +12:00
|
|
|
use Utopia\App;
|
2020-11-21 12:44:00 +13:00
|
|
|
use Utopia\CLI\Console;
|
2022-04-04 18:30:07 +12:00
|
|
|
use Utopia\Database\Document;
|
2021-07-28 08:31:19 +12:00
|
|
|
use Utopia\Locale\Locale;
|
2020-07-06 09:54:41 +12:00
|
|
|
|
2021-08-13 20:39:46 +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');
|
2021-07-30 06:46:06 +12:00
|
|
|
Console::success(APP_NAME . ' mails worker v1 has started' . "\n");
|
2020-06-14 06:30:44 +12:00
|
|
|
|
2021-06-12 02:20:18 +12:00
|
|
|
class MailsV1 extends Worker
|
2020-06-14 06:30:44 +12:00
|
|
|
{
|
2021-11-24 22:38:32 +13:00
|
|
|
public function getName(): string {
|
2021-11-24 03:24:25 +13:00
|
|
|
return "mails";
|
|
|
|
}
|
|
|
|
|
2021-06-12 02:20:18 +12:00
|
|
|
public function init(): void
|
2020-06-14 06:30:44 +12:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-12 02:20:18 +12:00
|
|
|
public function run(): void
|
2020-06-14 06:30:44 +12:00
|
|
|
{
|
|
|
|
global $register;
|
|
|
|
|
2021-07-30 06:46:06 +12:00
|
|
|
if (empty(App::getEnv('_APP_SMTP_HOST'))) {
|
2020-11-21 19:26:22 +13:00
|
|
|
Console::info('Skipped mail processing. No SMTP server hostname has been set.');
|
2020-11-21 12:44:00 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:39:31 +12:00
|
|
|
$project = new Document($this->args['project']);
|
|
|
|
$user = new Document($this->args['user'] ?? []);
|
|
|
|
$team = new Document($this->args['team'] ?? []);
|
2022-04-04 18:30:07 +12:00
|
|
|
|
2022-04-14 00:39:31 +12:00
|
|
|
$recipient = $this->args['recipient'];
|
|
|
|
$url = $this->args['url'];
|
|
|
|
$name = $this->args['name'];
|
|
|
|
$type = $this->args['type'];
|
2021-07-30 06:46:06 +12:00
|
|
|
$prefix = $this->getPrefix($type);
|
2022-04-14 00:39:31 +12:00
|
|
|
$locale = new Locale($this->args['locale']);
|
|
|
|
$projectName = $project->getAttribute('name', ['[APP-NAME]']);
|
2021-07-30 06:46:06 +12:00
|
|
|
|
|
|
|
if (!$this->doesLocaleExist($locale, $prefix)) {
|
|
|
|
$locale->setDefault('en');
|
|
|
|
}
|
2021-07-28 08:31:19 +12:00
|
|
|
|
2022-04-14 00:39:31 +12:00
|
|
|
$from = $project->getId() === 'console' ? '' : \sprintf($locale->getText('emails.sender'), $projectName);
|
2021-07-30 06:46:06 +12:00
|
|
|
$body = Template::fromFile(__DIR__ . '/../config/locale/templates/email-base.tpl');
|
|
|
|
$subject = '';
|
|
|
|
switch ($type) {
|
2021-07-28 08:31:19 +12:00
|
|
|
case MAIL_TYPE_INVITATION:
|
2022-04-14 00:39:31 +12:00
|
|
|
$subject = \sprintf($locale->getText("$prefix.subject"), $team->getAttribute('name'), $projectName);
|
|
|
|
$body->setParam('{{owner}}', $user->getAttribute('name'));
|
|
|
|
$body->setParam('{{team}}', $team->getAttribute('name'));
|
2021-07-28 08:31:19 +12:00
|
|
|
break;
|
2021-09-01 19:29:25 +12:00
|
|
|
case MAIL_TYPE_RECOVERY:
|
2021-07-28 08:31:19 +12:00
|
|
|
case MAIL_TYPE_VERIFICATION:
|
2021-08-30 22:44:52 +12:00
|
|
|
case MAIL_TYPE_MAGIC_SESSION:
|
2021-07-30 06:46:06 +12:00
|
|
|
$subject = $locale->getText("$prefix.subject");
|
2021-07-28 08:31:19 +12:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception('Undefined Mail Type : ' . $type, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$body
|
|
|
|
->setParam('{{subject}}', $subject)
|
|
|
|
->setParam('{{hello}}', $locale->getText("$prefix.hello"))
|
|
|
|
->setParam('{{name}}', $name)
|
|
|
|
->setParam('{{body}}', $locale->getText("$prefix.body"))
|
|
|
|
->setParam('{{redirect}}', $url)
|
|
|
|
->setParam('{{footer}}', $locale->getText("$prefix.footer"))
|
|
|
|
->setParam('{{thanks}}', $locale->getText("$prefix.thanks"))
|
|
|
|
->setParam('{{signature}}', $locale->getText("$prefix.signature"))
|
2022-04-14 00:39:31 +12:00
|
|
|
->setParam('{{project}}', $projectName)
|
2021-07-28 08:31:19 +12:00
|
|
|
->setParam('{{direction}}', $locale->getText('settings.direction'))
|
|
|
|
->setParam('{{bg-body}}', '#f7f7f7')
|
|
|
|
->setParam('{{bg-content}}', '#ffffff')
|
2021-07-30 06:46:06 +12:00
|
|
|
->setParam('{{text-content}}', '#000000');
|
2021-07-28 08:31:19 +12:00
|
|
|
|
|
|
|
$body = $body->render();
|
2021-07-30 06:46:06 +12:00
|
|
|
|
2020-07-06 09:54:41 +12:00
|
|
|
/** @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->clearAddresses();
|
|
|
|
$mail->clearAllRecipients();
|
|
|
|
$mail->clearReplyTos();
|
|
|
|
$mail->clearAttachments();
|
|
|
|
$mail->clearBCCs();
|
|
|
|
$mail->clearCCs();
|
2020-06-14 06:30:44 +12:00
|
|
|
|
2021-07-30 06:46:06 +12:00
|
|
|
$mail->setFrom(App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM), (empty($from) ? \urldecode(App::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server')) : $from));
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 02:20:18 +12:00
|
|
|
public function shutdown(): void
|
2020-06-14 06:30:44 +12:00
|
|
|
{
|
|
|
|
}
|
2021-07-30 06:46:06 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a prefix from a mail type
|
|
|
|
*
|
|
|
|
* @param $type
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getPrefix(string $type): string
|
|
|
|
{
|
|
|
|
switch ($type) {
|
|
|
|
case MAIL_TYPE_RECOVERY:
|
|
|
|
return 'emails.recovery';
|
|
|
|
case MAIL_TYPE_INVITATION:
|
|
|
|
return 'emails.invitation';
|
|
|
|
case MAIL_TYPE_VERIFICATION:
|
|
|
|
return 'emails.verification';
|
2021-08-30 22:44:52 +12:00
|
|
|
case MAIL_TYPE_MAGIC_SESSION:
|
|
|
|
return 'emails.magicSession';
|
2021-07-30 06:46:06 +12:00
|
|
|
default:
|
|
|
|
throw new Exception('Undefined Mail Type : ' . $type, 500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if all the required terms in a locale exist. False otherwise
|
|
|
|
*
|
|
|
|
* @param $locale
|
|
|
|
* @param $prefix
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function doesLocaleExist(Locale $locale, string $prefix): bool
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!$locale->getText('emails.sender') || !$locale->getText("$prefix.hello") || !$locale->getText("$prefix.subject") || !$locale->getText("$prefix.body") || !$locale->getText("$prefix.footer") || !$locale->getText("$prefix.thanks") || !$locale->getText("$prefix.signature")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|