Refactor email templates
Update the mails worker to only be responsible for building the final email using the base (layout) template and the user, team, project, and redirect variables. It is the responsibility of the controller to perform localization and determine whether a custom or default email template should be used. If the default one is used, it has to be rendered using the hello, footer, thanks, and signature variables so that it matches the custom template. Then, it can be sent to the worker.
This commit is contained in:
parent
3206cee36f
commit
836ce852ed
|
@ -1000,7 +1000,12 @@ App::post('/v1/account/sessions/magic-url')
|
||||||
$customTemplate = $project->getAttribute('templates', [])['email.magicSession-' . $locale->default] ?? [];
|
$customTemplate = $project->getAttribute('templates', [])['email.magicSession-' . $locale->default] ?? [];
|
||||||
|
|
||||||
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-inner-base.tpl');
|
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-inner-base.tpl');
|
||||||
$message->setParam('{{body}}', $body);
|
$message
|
||||||
|
->setParam('{{body}}', $body)
|
||||||
|
->setParam('{{hello}}', $locale->getText("emails.magicSession.hello"))
|
||||||
|
->setParam('{{footer}}', $locale->getText("emails.magicSession.footer"))
|
||||||
|
->setParam('{{thanks}}', $locale->getText("emails.magicSession.thanks"))
|
||||||
|
->setParam('{{signature}}', $locale->getText("emails.magicSession.signature"));
|
||||||
$body = $message->render();
|
$body = $message->render();
|
||||||
|
|
||||||
$smtp = $project->getAttribute('smtp', []);
|
$smtp = $project->getAttribute('smtp', []);
|
||||||
|
@ -1050,12 +1055,6 @@ App::post('/v1/account/sessions/magic-url')
|
||||||
}
|
}
|
||||||
|
|
||||||
$emailVariables = [
|
$emailVariables = [
|
||||||
'subject' => $subject,
|
|
||||||
'hello' => $locale->getText("emails.magicSession.hello"),
|
|
||||||
'body' => $body,
|
|
||||||
'footer' => $locale->getText("emails.magicSession.footer"),
|
|
||||||
'thanks' => $locale->getText("emails.magicSession.thanks"),
|
|
||||||
'signature' => $locale->getText("emails.magicSession.signature"),
|
|
||||||
'direction' => $locale->getText('settings.direction'),
|
'direction' => $locale->getText('settings.direction'),
|
||||||
/* {{user}} ,{{team}}, {{project}} and {{redirect}} are required in the templates */
|
/* {{user}} ,{{team}}, {{project}} and {{redirect}} are required in the templates */
|
||||||
'user' => '',
|
'user' => '',
|
||||||
|
@ -2454,7 +2453,12 @@ App::post('/v1/account/recovery')
|
||||||
$customTemplate = $project->getAttribute('templates', [])['email.recovery-' . $locale->default] ?? [];
|
$customTemplate = $project->getAttribute('templates', [])['email.recovery-' . $locale->default] ?? [];
|
||||||
|
|
||||||
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-inner-base.tpl');
|
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-inner-base.tpl');
|
||||||
$message->setParam('{{body}}', $body);
|
$message
|
||||||
|
->setParam('{{body}}', $body)
|
||||||
|
->setParam('{{hello}}', $locale->getText("emails.recovery.hello"))
|
||||||
|
->setParam('{{footer}}', $locale->getText("emails.recovery.footer"))
|
||||||
|
->setParam('{{thanks}}', $locale->getText("emails.recovery.thanks"))
|
||||||
|
->setParam('{{signature}}', $locale->getText("emails.recovery.signature"));
|
||||||
$body = $message->render();
|
$body = $message->render();
|
||||||
|
|
||||||
$smtp = $project->getAttribute('smtp', []);
|
$smtp = $project->getAttribute('smtp', []);
|
||||||
|
@ -2504,12 +2508,6 @@ App::post('/v1/account/recovery')
|
||||||
}
|
}
|
||||||
|
|
||||||
$emailVariables = [
|
$emailVariables = [
|
||||||
'subject' => $subject,
|
|
||||||
'hello' => $locale->getText("emails.recovery.hello"),
|
|
||||||
'body' => $body,
|
|
||||||
'footer' => $locale->getText("emails.recovery.footer"),
|
|
||||||
'thanks' => $locale->getText("emails.recovery.thanks"),
|
|
||||||
'signature' => $locale->getText("emails.recovery.signature"),
|
|
||||||
'direction' => $locale->getText('settings.direction'),
|
'direction' => $locale->getText('settings.direction'),
|
||||||
/* {{user}} ,{{team}}, {{project}} and {{redirect}} are required in the templates */
|
/* {{user}} ,{{team}}, {{project}} and {{redirect}} are required in the templates */
|
||||||
'user' => $profile->getAttribute('name'),
|
'user' => $profile->getAttribute('name'),
|
||||||
|
@ -2518,7 +2516,6 @@ App::post('/v1/account/recovery')
|
||||||
'redirect' => $url
|
'redirect' => $url
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
$queueForMails
|
$queueForMails
|
||||||
->setRecipient($profile->getAttribute('email', ''))
|
->setRecipient($profile->getAttribute('email', ''))
|
||||||
->setName($profile->getAttribute('name'))
|
->setName($profile->getAttribute('name'))
|
||||||
|
@ -2703,7 +2700,12 @@ App::post('/v1/account/verification')
|
||||||
$customTemplate = $project->getAttribute('templates', [])['email.verification-' . $locale->default] ?? [];
|
$customTemplate = $project->getAttribute('templates', [])['email.verification-' . $locale->default] ?? [];
|
||||||
|
|
||||||
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-inner-base.tpl');
|
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-inner-base.tpl');
|
||||||
$message->setParam('{{body}}', $body);
|
$message
|
||||||
|
->setParam('{{body}}', $body)
|
||||||
|
->setParam('{{hello}}', $locale->getText("emails.verification.hello"))
|
||||||
|
->setParam('{{footer}}', $locale->getText("emails.verification.footer"))
|
||||||
|
->setParam('{{thanks}}', $locale->getText("emails.verification.thanks"))
|
||||||
|
->setParam('{{signature}}', $locale->getText("emails.verification.signature"));
|
||||||
$body = $message->render();
|
$body = $message->render();
|
||||||
|
|
||||||
$smtp = $project->getAttribute('smtp', []);
|
$smtp = $project->getAttribute('smtp', []);
|
||||||
|
@ -2753,12 +2755,6 @@ App::post('/v1/account/verification')
|
||||||
}
|
}
|
||||||
|
|
||||||
$emailVariables = [
|
$emailVariables = [
|
||||||
'subject' => $subject,
|
|
||||||
'hello' => $locale->getText("emails.verification.hello"),
|
|
||||||
'body' => $body,
|
|
||||||
'footer' => $locale->getText("emails.verification.footer"),
|
|
||||||
'thanks' => $locale->getText("emails.verification.thanks"),
|
|
||||||
'signature' => $locale->getText("emails.verification.signature"),
|
|
||||||
'direction' => $locale->getText('settings.direction'),
|
'direction' => $locale->getText('settings.direction'),
|
||||||
/* {{user}} ,{{team}}, {{project}} and {{redirect}} are required in the templates */
|
/* {{user}} ,{{team}}, {{project}} and {{redirect}} are required in the templates */
|
||||||
'user' => $user->getAttribute('name'),
|
'user' => $user->getAttribute('name'),
|
||||||
|
|
|
@ -555,7 +555,12 @@ App::post('/v1/teams/:teamId/memberships')
|
||||||
$customTemplate = $project->getAttribute('templates', [])['email.invitation-' . $locale->default] ?? [];
|
$customTemplate = $project->getAttribute('templates', [])['email.invitation-' . $locale->default] ?? [];
|
||||||
|
|
||||||
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-inner-base.tpl');
|
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-inner-base.tpl');
|
||||||
$message->setParam('{{body}}', $body);
|
$message
|
||||||
|
->setParam('{{body}}', $body)
|
||||||
|
->setParam('{{hello}}', $locale->getText("emails.invitation.hello"))
|
||||||
|
->setParam('{{footer}}', $locale->getText("emails.invitation.footer"))
|
||||||
|
->setParam('{{thanks}}', $locale->getText("emails.invitation.thanks"))
|
||||||
|
->setParam('{{signature}}', $locale->getText("emails.invitation.signature"));
|
||||||
$body = $message->render();
|
$body = $message->render();
|
||||||
|
|
||||||
$smtp = $project->getAttribute('smtp', []);
|
$smtp = $project->getAttribute('smtp', []);
|
||||||
|
@ -606,12 +611,6 @@ App::post('/v1/teams/:teamId/memberships')
|
||||||
|
|
||||||
$emailVariables = [
|
$emailVariables = [
|
||||||
'owner' => $user->getAttribute('name'),
|
'owner' => $user->getAttribute('name'),
|
||||||
'subject' => $subject,
|
|
||||||
'hello' => $locale->getText("emails.invitation.hello"),
|
|
||||||
'body' => $body,
|
|
||||||
'footer' => $locale->getText("emails.invitation.footer"),
|
|
||||||
'thanks' => $locale->getText("emails.invitation.thanks"),
|
|
||||||
'signature' => $locale->getText("emails.invitation.signature"),
|
|
||||||
'direction' => $locale->getText('settings.direction'),
|
'direction' => $locale->getText('settings.direction'),
|
||||||
/* {{user}} ,{{team}}, {{project}} and {{redirect}} are required in the templates */
|
/* {{user}} ,{{team}}, {{project}} and {{redirect}} are required in the templates */
|
||||||
'user' => $user->getAttribute('name'),
|
'user' => $user->getAttribute('name'),
|
||||||
|
|
|
@ -58,13 +58,16 @@ class Mails extends Action
|
||||||
$subject = $payload['subject'];
|
$subject = $payload['subject'];
|
||||||
$variables = $payload['variables'];
|
$variables = $payload['variables'];
|
||||||
$name = $payload['name'];
|
$name = $payload['name'];
|
||||||
$body = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-base.tpl');
|
$body = $payload['body'];
|
||||||
|
|
||||||
|
$bodyTemplate = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-base.tpl');
|
||||||
|
$bodyTemplate->setParam('{{body}}', $body);
|
||||||
|
|
||||||
foreach ($variables as $key => $value) {
|
foreach ($variables as $key => $value) {
|
||||||
$body->setParam('{{' . $key . '}}', $value);
|
$bodyTemplate->setParam('{{' . $key . '}}', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
$body = $body->render();
|
$body = $bodyTemplate->render();
|
||||||
|
|
||||||
/** @var PHPMailer $mail */
|
/** @var PHPMailer $mail */
|
||||||
$mail = empty($smtp)
|
$mail = empty($smtp)
|
||||||
|
|
Loading…
Reference in a new issue