diff --git a/app/config/locale/templates/email-stock-body.tpl b/app/config/locale/templates/email-stock-body.tpl new file mode 100644 index 0000000000..13af5477f1 --- /dev/null +++ b/app/config/locale/templates/email-stock-body.tpl @@ -0,0 +1,13 @@ +

{{hello}}

+ +

{{body}}

+ +{{redirect}} + +


{{footer}}

+
+ +

{{thanks}} +
+{{signature}} +

\ No newline at end of file diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 9fd8f10f09..3560cff166 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1114,26 +1114,26 @@ App::post('/v1/account/sessions/magic-url') ->setSmtpSenderName($customTemplate['senderName'] ?? ''); } - $body - ->setParam('{{subject}}', $subject) - ->setParam('{{hello}}', $locale->getText("emails.magicSession.hello")) - ->setParam('{{name}}', '') - ->setParam('{{body}}', $locale->getText("emails.magicSession.body")) - ->setParam('{{redirect}}', $url) - ->setParam('{{footer}}', $locale->getText("emails.magicSession.footer")) - ->setParam('{{thanks}}', $locale->getText("emails.magicSession.thanks")) - ->setParam('{{signature}}', $locale->getText("emails.magicSession.signature")) - ->setParam('{{project}}', $project->getAttribute('name')) - ->setParam('{{direction}}', $locale->getText('settings.direction')) - ->setParam('{{bg-body}}', '#f7f7f7') - ->setParam('{{bg-content}}', '#ffffff') - ->setParam('{{text-content}}', '#000000'); - - $body = $body->render(); + $emailVariables = [ + 'subject' => $subject, + 'hello' => $locale->getText("emails.magicSession.hello"), + 'name' => '', + 'body' => $locale->getText("emails.magicSession.body"), + 'redirect' => $url, + 'footer' => $locale->getText("emails.magicSession.footer"), + 'thanks' => $locale->getText("emails.magicSession.thanks"), + 'signature' => $locale->getText("emails.magicSession.signature"), + 'project' => $project->getAttribute('name'), + 'direction' => $locale->getText('settings.direction'), + 'bg-body' => '#f7f7f7', + 'bg-content' => '#ffffff', + 'text-content' => '#000000', + ]; $mails ->setSubject($subject) - ->setBody($body) + ->setBody($body->render()) + ->setVariables($emailVariables) ->setFrom($from) ->setRecipient($user->getAttribute('email')) ->trigger() @@ -2519,13 +2519,13 @@ App::post('/v1/account/recovery') $projectName = $project->isEmpty() ? 'Console' : $project->getAttribute('name', '[APP-NAME]'); $from = $project->isEmpty() || $project->getId() === 'console' ? '' : \sprintf($locale->getText('emails.sender'), $projectName); - $body = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-base.tpl'); + $body = $locale->getText("emails.recovery.body"); $subject = $locale->getText("emails.recovery.subject"); $smtpEnabled = $project->getAttribute('smtp', [])['enabled'] ?? false; $customTemplate = $project->getAttribute('templates', [])['email.recovery-' . $locale->default] ?? []; if ($smtpEnabled && !empty($customTemplate)) { - $body = Template::fromString($customTemplate['message'] ?? ''); + $body = $customTemplate['message']; $subject = $customTemplate['subject'] ?? $subject; $from = $customTemplate['senderName'] ?? $from; @@ -2541,28 +2541,45 @@ App::post('/v1/account/recovery') ->setSmtpSenderName($customTemplate['senderName'] ?? ''); } - $body - ->setParam('{{subject}}', $subject) - ->setParam('{{hello}}', $locale->getText("emails.recovery.hello")) - ->setParam('{{name}}', $profile->getAttribute('name')) - ->setParam('{{body}}', $locale->getText("emails.recovery.body")) - ->setParam('{{redirect}}', $url) - ->setParam('{{footer}}', $locale->getText("emails.recovery.footer")) - ->setParam('{{thanks}}', $locale->getText("emails.recovery.thanks")) - ->setParam('{{signature}}', $locale->getText("emails.recovery.signature")) - ->setParam('{{project}}', $projectName) - ->setParam('{{direction}}', $locale->getText('settings.direction')) - ->setParam('{{bg-body}}', '#f7f7f7') - ->setParam('{{bg-content}}', '#ffffff') - ->setParam('{{text-content}}', '#000000'); + // $body + // ->setParam('{{subject}}', $subject) + // ->setParam('{{hello}}', $locale->getText("emails.recovery.hello")) + // ->setParam('{{name}}', $profile->getAttribute('name')) + // ->setParam('{{body}}', $locale->getText("emails.recovery.body")) + // ->setParam('{{redirect}}', $url) + // ->setParam('{{footer}}', $locale->getText("emails.recovery.footer")) + // ->setParam('{{thanks}}', $locale->getText("emails.recovery.thanks")) + // ->setParam('{{signature}}', $locale->getText("emails.recovery.signature")) + // ->setParam('{{project}}', $projectName) + // ->setParam('{{direction}}', $locale->getText('settings.direction')) + // ->setParam('{{bg-body}}', '#f7f7f7') + // ->setParam('{{bg-content}}', '#ffffff') + // ->setParam('{{text-content}}', '#000000'); - $body = $body->render(); + // $body = $body->render(); + + $emailVariables = [ + 'subject' => $subject, + 'hello' => $locale->getText("emails.recovery.hello"), + 'name' => $profile->getAttribute('name'), + 'body' => $locale->getText("emails.recovery.body"), + 'redirect' => $url, + 'footer' => $locale->getText("emails.recovery.footer"), + 'thanks' => $locale->getText("emails.recovery.thanks"), + 'signature' => $locale->getText("emails.recovery.signature"), + 'project' => $projectName, + 'direction' => $locale->getText('settings.direction'), + 'bg-body' => '#f7f7f7', + 'bg-content' => '#ffffff', + 'text-content' => '#000000', + ]; $mails ->setRecipient($profile->getAttribute('email', '')) ->setName($profile->getAttribute('name')) ->setBody($body) + ->setVariables($emailVariables) ->setFrom($from) ->setSubject($subject) ->trigger(); diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index a5487f57ec..bcf6e1594d 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -1525,11 +1525,11 @@ App::patch('/v1/projects/:projectId/smtp') ->label('sdk.response.model', Response::MODEL_PROJECT) ->param('projectId', '', new UID(), 'Project unique ID.') ->param('enabled', false, new Boolean(), 'Enable custom SMTP service') - ->param('sender', '', new Email(), 'SMTP sender email') - ->param('host', '', new HostName(), 'SMTP server host name') - ->param('port', null, new Integer(), 'SMTP server port') - ->param('username', null, new Text(0), 'SMTP server username') - ->param('password', null, new Text(0), 'SMTP server password') + ->param('sender', '', new Email(), 'SMTP sender email', true) + ->param('host', '', new HostName(), 'SMTP server host name', true) + ->param('port', null, new Integer(), 'SMTP server port', true) + ->param('username', null, new Text(0, 0), 'SMTP server username', true) + ->param('password', null, new Text(0, 0), 'SMTP server password', true) ->param('secure', '', new WhiteList(['tls'], true), 'Does SMTP server use secure connection', true) ->inject('response') ->inject('dbForConsole') @@ -1645,19 +1645,7 @@ App::get('/v1/projects/:projectId/templates/email/:type/:locale') $localeObj = new Locale($locale); if (is_null($template)) { - $message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-base.tpl'); - $message = $message - ->setParam('{{hello}}', $localeObj->getText("emails.{$type}.hello")) - ->setParam('{{name}}', '') - ->setParam('{{body}}', $localeObj->getText("emails.{$type}.body")) - ->setParam('{{footer}}', $localeObj->getText("emails.{$type}.footer")) - ->setParam('{{thanks}}', $localeObj->getText("emails.{$type}.thanks")) - ->setParam('{{signature}}', $localeObj->getText("emails.{$type}.signature")) - ->setParam('{{direction}}', $localeObj->getText('settings.direction')) - ->setParam('{{bg-body}}', '#f7f7f7') - ->setParam('{{bg-content}}', '#ffffff') - ->setParam('{{text-content}}', '#000000') - ->render(); + $message = $localeObj->getText("emails.{$type}.body"); $from = $project->isEmpty() || $project->getId() === 'console' ? '' : \sprintf($localeObj->getText('emails.sender'), $project->getAttribute('name')); $from = empty($from) ? \urldecode(App::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server')) : $from; diff --git a/app/workers/mails.php b/app/workers/mails.php index baff259495..73fb0abb7f 100644 --- a/app/workers/mails.php +++ b/app/workers/mails.php @@ -1,6 +1,7 @@ args['recipient']; $subject = $this->args['subject']; $name = $this->args['name']; $body = $this->args['body']; + $variables = $this->args['variables']; $from = $this->args['from']; + $body = Template::fromFile(__DIR__ . '/../config/locale/templates/email-base.tpl'); + + foreach ($variables as $key => $value) { + var_dump($key, $value); + $body->setParam('{{'.$key.'}}', $value); + } + + var_dump($body); + + $body = $body->render(); + + var_dump($body); + /** @var \PHPMailer\PHPMailer\PHPMailer $mail */ $mail = empty($smtp) ? $register->get('smtp') : $this->getMailer($smtp); diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php index dfec4cd756..6dde45ac57 100644 --- a/src/Appwrite/Event/Mail.php +++ b/src/Appwrite/Event/Mail.php @@ -13,6 +13,7 @@ class Mail extends Event protected string $subject = ''; protected string $body = ''; protected array $smtp = []; + protected array $variables = []; public function __construct() { @@ -310,6 +311,28 @@ class Mail extends Event return $this->smtp['replyTo'] ?? ''; } + /** + * Get Email Variables + * + * @return array + */ + public function getVariables(): array + { + return $this->variables; + } + + /** + * Set Email Variables + * + * @param array $variables + * @return self + */ + public function setVariables(array $variables): self + { + $this->variables = $variables; + return $this; + } + /** * Executes the event and sends it to the mails worker. * @@ -325,6 +348,7 @@ class Mail extends Event 'subject' => $this->subject, 'body' => $this->body, 'smtp' => $this->smtp, + 'variables' => $this->variables, 'events' => Event::generateEvents($this->getEvent(), $this->getParams()) ]); }