diff --git a/app/config/locale/templates/email-base-styled.tpl b/app/config/locale/templates/email-base-styled.tpl
index f6d3e8cd63..b5aece0253 100644
--- a/app/config/locale/templates/email-base-styled.tpl
+++ b/app/config/locale/templates/email-base-styled.tpl
@@ -120,6 +120,11 @@
+
+ {{preview}}
+
{{previewWhitespace}}
+
+
diff --git a/app/config/locale/templates/email-base.tpl b/app/config/locale/templates/email-base.tpl
index 13056fd5ae..f6807ce7b2 100644
--- a/app/config/locale/templates/email-base.tpl
+++ b/app/config/locale/templates/email-base.tpl
@@ -121,6 +121,11 @@
+
+ {{preview}}
+
{{previewWhitespace}}
+
+
diff --git a/app/config/locale/translations/en.json b/app/config/locale/translations/en.json
index dbfa2e1be8..653e8c16e5 100644
--- a/app/config/locale/translations/en.json
+++ b/app/config/locale/translations/en.json
@@ -29,6 +29,7 @@
"emails.sessionAlert.thanks": "Thanks,",
"emails.sessionAlert.signature": "{{project}} team",
"emails.otpSession.subject": "OTP for {{project}} Login",
+ "emails.otpSession.preview": "Use OTP {{otp}} to sign in to {{project}}",
"emails.otpSession.hello": "Hello {{user}},",
"emails.otpSession.description": "Enter the following verification code when prompted to securely sign in to your {{b}}{{project}}{{/b}} account. This code will expire in 15 minutes.",
"emails.otpSession.clientInfo": "This sign in was requested using {{b}}{{agentClient}}{{/b}} on {{b}}{{agentDevice}}{{/b}} {{b}}{{agentOs}}{{/b}}. If you didn't request the sign in, you can safely ignore this email.",
@@ -49,6 +50,7 @@
"emails.recovery.buttonText": "Reset password",
"emails.recovery.signature": "{{project}} team",
"emails.invitation.subject": "Invitation to %s Team at %s",
+ "emails.invitation.preview": "{{owner}} invited you to join {{team}} at {{project}}",
"emails.invitation.hello": "Hello {{user}},",
"emails.invitation.body": "This mail was sent to you because {{b}}{{owner}}{{/b}} wanted to invite you to become a member of the {{b}}{{team}}{{/b}} team at {{b}}{{project}}{{/b}}.",
"emails.invitation.footer": "If you are not interested, you can ignore this message.",
diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php
index 6de91f23cc..81bafa29f1 100644
--- a/app/controllers/api/account.php
+++ b/app/controllers/api/account.php
@@ -2254,6 +2254,7 @@ App::post('/v1/account/tokens/email')
$dbForProject->purgeCachedDocument('users', $user->getId());
$subject = $locale->getText("emails.otpSession.subject");
+ $preview = $locale->getText("emails.otpSession.preview");
$customTemplate = $project->getAttribute('templates', [])['email.otpSession-' . $locale->default] ?? [];
$detector = new Detector($request->getUserAgent('UNKNOWN'));
@@ -2339,6 +2340,7 @@ App::post('/v1/account/tokens/email')
$queueForMails
->setSubject($subject)
+ ->setPreview($preview)
->setBody($body)
->setVariables($emailVariables)
->setRecipient($email)
diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php
index 98ec49ca48..fe411d53ab 100644
--- a/app/controllers/api/teams.php
+++ b/app/controllers/api/teams.php
@@ -657,6 +657,7 @@ App::post('/v1/teams/:teamId/memberships')
$projectName = $project->isEmpty() ? 'Console' : $project->getAttribute('name', '[APP-NAME]');
$body = $locale->getText("emails.invitation.body");
+ $preview = $locale->getText("emails.invitation.preview");
$subject = \sprintf($locale->getText("emails.invitation.subject"), $team->getAttribute('name'), $projectName);
$customTemplate = $project->getAttribute('templates', [])['email.invitation-' . $locale->default] ?? [];
@@ -729,6 +730,7 @@ App::post('/v1/teams/:teamId/memberships')
$queueForMails
->setSubject($subject)
->setBody($body)
+ ->setPreview($preview)
->setRecipient($invitee->getAttribute('email'))
->setName($invitee->getAttribute('name', ''))
->setVariables($emailVariables)
diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php
index 87312182ea..c9a0671461 100644
--- a/src/Appwrite/Event/Mail.php
+++ b/src/Appwrite/Event/Mail.php
@@ -10,6 +10,7 @@ class Mail extends Event
protected string $name = '';
protected string $subject = '';
protected string $body = '';
+ protected string $preview = '';
protected array $smtp = [];
protected array $variables = [];
protected string $bodyTemplate = '';
@@ -93,6 +94,28 @@ class Mail extends Event
return $this->body;
}
+ /**
+ * Sets preview for the mail event.
+ *
+ * @return string
+ */
+ public function setPreview(string $preview): self
+ {
+ $this->preview = $preview;
+
+ return $this;
+ }
+
+ /**
+ * Returns preview for the mail event.
+ *
+ * @return string
+ */
+ public function getPreview(string $preview): string
+ {
+ return $this->preview;
+ }
+
/**
* Sets name for the mail event.
*
@@ -409,6 +432,7 @@ class Mail extends Event
'subject' => $this->subject,
'bodyTemplate' => $this->bodyTemplate,
'body' => $this->body,
+ 'preview' => $this->preview,
'smtp' => $this->smtp,
'variables' => $this->variables,
'attachment' => $this->attachment,
diff --git a/src/Appwrite/Platform/Workers/Mails.php b/src/Appwrite/Platform/Workers/Mails.php
index 4e8b5e085c..117b689863 100644
--- a/src/Appwrite/Platform/Workers/Mails.php
+++ b/src/Appwrite/Platform/Workers/Mails.php
@@ -14,6 +14,11 @@ use Utopia\System\System;
class Mails extends Action
{
+ protected int $previewMaxLen = 150;
+
+ protected string $whitespaceCodes = ' ';
+
+
public static function getName(): string
{
return 'mails';
@@ -74,6 +79,7 @@ class Mails extends Action
$variables['host'] = $protocol . '://' . $hostname;
$name = $payload['name'];
$body = $payload['body'];
+ $preview = $payload['preview'] ?? '';
$variables['subject'] = $subject;
$variables['year'] = date("Y");
@@ -92,6 +98,27 @@ class Mails extends Action
foreach ($this->richTextParams as $key => $value) {
$bodyTemplate->setParam('{{' . $key . '}}', $value, escapeHtml: false);
}
+
+ $previewWhitespace = '';
+
+ if (!empty($preview)) {
+ $previewTemplate = Template::fromString($preview);
+ foreach ($variables as $key => $value) {
+ $previewTemplate->setParam('{{' . $key . '}}', $value);
+ }
+ // render() will return the subject in tags, so use strip_tags() to remove them
+ $preview = \strip_tags($previewTemplate->render());
+
+ $previewLen = strlen($preview);
+ if ($previewLen < $this->previewMaxLen) {
+ $previewWhitespace = str_repeat($this->whitespaceCodes, $this->previewMaxLen - $previewLen);
+ }
+ }
+
+
+ $bodyTemplate->setParam('{{preview}}', $preview);
+ $bodyTemplate->setParam('{{previewWhitespace}}', $previewWhitespace, false);
+
$body = $bodyTemplate->render();
$subjectTemplate = Template::fromString($subject);