8000 Remove CTA template and move logic to mails worker by christyjacob4 · Pull Request #1438 · appwrite/appwrite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove CTA template and move logic to mails worker #1438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 11 additions & 57 deletions app/config/locale/templates/email-base.tpl
8000
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{{title}}</title>
<title>{{subject}}</title>
<style>
body {
background-color: {{bg-body}};
Expand Down Expand Up @@ -82,59 +82,11 @@
word-break: break-all;
}

.btn {
box-sizing: border-box;
width: 100%;
}

.btn>tbody>tr>td {
padding-bottom: 15px;
padding-top: 15px;
}

.btn table {
width: auto;
}

.btn table td {
background-color: {{bg-content}};
border-radius: 20px;
text-align: center;
}

.btn a {
background-color: {{bg-content}};
border-radius: 20px;
box-sizing: border-box;
color: #577590;
cursor: pointer;
display: inline-block;
font-size: 14px;
font-weight: bold;
margin: 0;
padding: 12px 25px;
text-decoration: none;
text-transform: capitalize;
}

.btn-primary table td {
background-color: {{bg-cta}};
}

.btn-primary a {
background-color: {{bg-cta}};
color: {{text-cta}};
}

@media only screen and (max-width: 620px) {
.container {
padding: 0;
width: 100%;
}

.btn-primary a {
font-size: 13px;
}
}

@media all {
Expand Down Expand Up @@ -169,13 +121,6 @@
line-height: inherit;
}

.btn-primary table td:hover {
opacity: 0.7 !important;
}

.btn-primary a:hover {
opacity: 0.7 !important;
}
}
</style>
</head>
Expand All @@ -191,7 +136,16 @@
<td class="wrapper">
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>{{content}}</td>
<td>
<p>{{hello}}</p>
<p>{{body}}</p>
<a href="{{redirect}}" target="_blank">{{redirect}}</a>
<p></br>{{footer}}</p>
<p>{{thanks}}
</br>
{{signature}}
</p>
</td>
</tr>
</table>
</td>
Expand Down
24 changes: 0 additions & 24 deletions app/config/locale/templates/email-cta.tpl

This file was deleted.

30 changes: 23 additions & 7 deletions app/config/locale/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@
"settings.inspire": "\"The art of being wise is the art of knowing what to overlook.\"",
"settings.locale": "en",
"settings.direction": "ltr",
"account.emails.team": "%s Team",
"account.emails.verification.title": "Account Verification",
"account.emails.verification.body": "\n Hello {{name}},\n\n\n Follow this link to verify your email address.\n\n{{cta}}\n\n If you didn’t ask to verify this address, you can ignore this message.\n\n\n Thanks,\n {{project}} team\n",
"account.emails.recovery.title": "Password Reset",
"account.emails.recovery.body": "\n Hello {{name}},\n\n\n Follow this link to reset your {{project}} password.\n\n{{cta}}\n\n If you didn’t ask to reset your password, you can ignore this message.\n\n\n Thanks,\n {{project}} team\n\n",
"account.emails.invitation.title": "Invitation to %s Team at %s",
"account.emails.invitation.body": "\n Hello,\n\n\n This mail was sent to you because {{owner}} wanted to invite you to become a member of the {{team}} team at {{project}}.\n\n{{cta}}\n\n If you are not interested, you can ignore this message.\n\n Thanks,\n {{project}} team\n\n",
"emails.sender": "%s Team",

"emails.verification.subject": "Account Verification",
"emails.verification.hello": "Hey {{name}}",
"emails.verification.body": "Follow this link to verify your email address.",
"emails.verification.footer": "If you didn’t ask to verify this address, you can ignore this message.",
"emails.verification.thanks": "Thanks",
"emails.verification.signature": "{{project}} team",

"emails.recovery.subject": "Password Reset",
"emails.recovery.hello": "Hello {{name}}",
"emails.recovery.body": "Follow this link to reset your {{project}} password.",
"emails.recovery.footer": "If you didn’t ask to reset your password, you can ignore this message.",
"emails.recovery.thanks": "Thanks",
"emails.recovery.signature": "{{project}} team",

"emails.invitation.subject": "Invitation to %s Team at %s",
"emails.invitation.hello": "Hello",
"emails.invitation.body": "This mail was sent to you because {{owner}} wanted to invite you to become a member of the {{team}} team at {{project}}.",
"emails.invitation.footer": "If you are not interested, you can ignore this message.",
"emails.invitation.thanks": "Thanks",
"emails.invitation.signature": "{{project}} team",

"locale.country.unknown": "Unknown",
"countries.af": "Afghanistan",
"countries.ao": "Angola",
Expand Down
56 changes: 12 additions & 44 deletions app/controllers/api/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -1515,32 +1515,16 @@
$url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['userId' => $profile->getId(), 'secret' => $secret, 'expire' => $expire]);
$url = Template::unParseURL($url);

$body = Template::fromFile(__DIR__.'/../../config/locale/templates/email-base.tpl');
$content = Template::fromString($locale->getText('account.emails.recovery.body'));
$cta = Template::fromFile(__DIR__.'/../../config/locale/templates/email-cta.tpl');

$body
->setParam('{{content}}', $content->render(false))
->setParam('{{cta}}', $cta->render())
->setParam('{{title}}', $locale->getText('account.emails.recovery.title'))
->setParam('{{direction}}', $locale->getText('settings.direction'))
->setParam('{{project}}', $project->getAttribute('name', ['[APP-NAME]']))
->setParam('{{name}}', $profile->getAttribute('name'))
->setParam('{{redirect}}', $url)
->setParam('{{bg-body}}', '#f7f7f7')
->setParam('{{bg-content}}', '#ffffff')
->setParam('{{bg-cta}}', '#073b4c')
->setParam('{{text-content}}', '#000000')
->setParam('{{text-cta}}', '#ffffff')
;

$mails
->setParam('event', 'account.recovery.create')
->setParam('from', ($project->getId() === 'console') ? '' : \sprintf($locale->getText('account.emails.team'), $project->getAttribute('name')))
->setParam('from', ($project->getId() === 'console') ? '' : \sprintf($locale->getText('emails.sender'), $project->getAttribute('name')))
->setParam('recipient', $profile->getAttribute('email', ''))
->setParam('name', $profile->getAttribute('name', ''))
->setParam('subject', $locale->getText('account.emails.recovery.title'))
->setParam('body', $body->render())
->setParam('subject', $locale->getText('emails.recovery.subject'))
->setParam('url', $url)
->setParam('locale', $locale->default)
->setParam('project', $project->getAttribute('name', ['[APP-NAME]']))
->setParam('type', MAIL_TYPE_RECOVERY)
->trigger();
;

Expand Down Expand Up @@ -1719,32 +1703,16 @@
$url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['userId' => $user->getId(), 'secret' => $verificationSecret, 'expire' => $expire]);
$url = Template::unParseURL($url);

$body = Template::fromFile(__DIR__.'/../../config/locale/templates/email-base.tpl');
$content = Template::fromString($locale->getText('account.emails.verification.body'));
$cta = Template::fromFile(__DIR__.'/../../config/locale/templates/email-cta.tpl');

$body
->setParam('{{content}}', $content->render(false))
->setParam('{{cta}}', $cta->render())
->setParam('{{title}}', $locale->getText('account.emails.verification.title'))
->setParam('{{direction}}', $locale->getText('settings.direction'))
->setParam('{{project}}', $project->getAttribute('name', ['[APP-NAME]']))
->setParam('{{name}}', $user->getAttribute('name'))
->setParam('{{redirect}}', $url)
->setParam('{{bg-body}}', '#f7f7f7')
->setParam('{{bg-content}}', '#ffffff')
->setParam('{{bg-cta}}', '#073b4c')
->setParam('{{text-content}}', '#000000')
->setParam('{{text-cta}}', '#ffffff')
;

$mails
->setParam('event', 'account.verification.create')
->setParam('from', ($project->getId() === 'console') ? '' : \sprintf($locale->getText('account.emails.team'), $project->getAttribute('name')))
->setParam('from', ($project->getId() === 'console') ? '' : \sprintf($locale->getText('emails.sender'), $project->getAttribute('name')))
->setParam('recipient', $user->getAttribute('email'))
->setParam('name', $user->getAttribute('name'))
->setParam('subject', $locale->getText('account.emails.verification.title'))
->setParam('body', $body->render())
->setParam('subject', $locale->getText('emails.verification.subject'))
->setParam('url', $url)
->setParam('locale', $locale->default)
->setParam('project', $project->getAttribute('name', ['[APP-NAME]']))
->setParam('type', MAIL_TYPE_VERIFICATION)
->trigger()
;

Expand Down
33 changes: 9 additions & 24 deletions app/controllers/api/teams.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,36 +418,21 @@
$url = Template::parseURL($url);
$url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['membershipId' => $membership->getId(), 'teamId' => $team->getId(), 'userId' => $invitee->getId(), 'secret' => $secret, 'teamId' => $teamId]);
$url = Template::unParseURL($url);

$body = Template::fromFile(__DIR__.'/../../config/locale/templates/email-base.tpl');
$content = Template::fromString($locale->getText('account.emails.invitation.body'));
$cta = Template::fromFile(__DIR__.'/../../config/locale/templates/email-cta.tpl');
$title = \sprintf($locale->getText('account.emails.invitation.title'), $team->getAttribute('name', '[TEAM-NAME]'), $project->getAttribute('name', ['[APP-NAME]']));

$body
->setParam('{{content}}', $content->render(false))
->setParam('{{cta}}', $cta->render())
->setParam('{{title}}', $title)
->setParam('{{direction}}', $locale->getText('settings.direction'))
->setParam('{{project}}', $project->getAttribute('name', ['[APP-NAME]']))
->setParam('{{team}}', $team->getAttribute('name', '[TEAM-NAME]'))
->setParam('{{owner}}', $user->getAttribute('name', ''))
->setParam('{{redirect}}', $url)
->setParam('{{bg-body}}', '#f7f7f7')
->setParam('{{bg-content}}', '#ffffff')
->setParam('{{bg-cta}}', '#073b4c')
->setParam('{{text-content}}', '#000000')
->setParam('{{text-cta}}', '#ffffff')
;
$subject = \sprintf($locale->getText('emails.invitation.subject'), $team->getAttribute('name', '[TEAM-NAME]'), $project->getAttribute('name', ['[APP-NAME]']));

if (!$isPrivilegedUser && !$isAppUser) { // No need of confirmation when in admin or app mode
$mails
->setParam('event', 'teams.memberships.create')
->setParam('from', ($project->getId() === 'console') ? '' : \sprintf($locale->getText('account.emails.team'), $project->getAttribute('name')))
->setParam('from', ($project->getId() === 'console') ? '' : \sprintf($locale->getText('emails.sender'), $project->getAttribute('name')))
->setParam('recipient', $email)
->setParam('name', $name)
->setParam('subject', $title)
->setParam('body', $body->render())
->setParam('subject', $subject)
->setParam('url', $url)
->setParam('locale', $locale->default)
->setParam('project', $project->getAttribute('name', ['[APP-NAME]']))
->setParam('owner', $user->getAttribute('name', ''))
->setParam('team', $team->getAttribute('name', '[TEAM-NAME]'))
->setParam('type', MAIL_TYPE_INVITATION)
->trigger()
;
}
Expand Down
4 changes: 4 additions & 0 deletions app/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
const DELETE_TYPE_AUDIT = 'audit';
const DELETE_TYPE_ABUSE = 'abuse';
const DELETE_TYPE_CERTIFICATES = 'certificates';
// Mail Types
const MAIL_TYPE_VERIFICATION = 'verification';
const MAIL_TYPE_RECOVERY = 'recovery';
const MAIL_TYPE_INVITATION = 'invitation';
// Auth Types
const APP_AUTH_TYPE_SESSION = 'Session';
const APP_AUTH_TYPE_JWT = 'JWT';
Expand Down
44 changes: 43 additions & 1 deletion app/workers/mails.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use Appwrite\Resque\Worker;
use Appwrite\Template\Template;
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Locale\Locale;

require_once __DIR__.'/../workers.php';

Expand Down Expand Up @@ -33,7 +35,47 @@ public function run(): void
$recipient = $this->args['recipient'];
$name = $this->args['name'];
$subject = $this->args['subject'];
$body = $this->args['body'];
$url = $this->args['url'];
$project = $this->args['project'];
$locale = new Locale($this->args['locale']);

$type = $this->args['type'];
$prefix = '';
$body = Template::fromFile(__DIR__.'/../config/locale/templates/email-base.tpl');

switch($type) {
case MAIL_TYPE_RECOVERY:
$prefix = 'emails.recovery';
break;
case MAIL_TYPE_INVITATION:
$prefix = 'emails.invitation';
$body->setParam('{{owner}}', $this->args['owner']);
$body->setParam('{{team}}', $this->args['team']);
break;
case MAIL_TYPE_VERIFICATION:
$prefix = 'emails.verification';
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"))
->setParam('{{project}}', $project)
->setParam('{{direction}}', $locale->getText('settings.direction'))
->setParam('{{bg-body}}', '#f7f7f7')
->setParam('{{bg-content}}', '#ffffff')
->setParam('{{text-content}}', '#000000')
;

$body = $body->render();

/** @var \PHPMailer\PHPMailer\PHPMailer $mail */
$mail = $register->get('smtp');
Expand Down
2 changes: 2 additions & 0 deletions src/Appwrite/Template/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public function render($minify = true)
throw new Exception('"'.$this->path.'" template is not readable or not found');
}

// First replace the variables inside the params. Then replace the variables in the template
$this->params = array_merge($this->params, \str_replace(\array_keys($this->params), \array_values($this->params), $this->params));
$template = \str_replace(\array_keys($this->params), \array_values($this->params), $template);

return $template;
Expand Down
0