Github repo
NodeJs service using AWS SES to send emails and handlebars to handle email templates.
How it works
Cognito
This service is prepared to handle Cognito Custom Email calls, with payloads like this:
export interface MailingEvent {
userName: string;
request: {
type: 'customEmailSenderRequestV1';
code: string;
clientMetadata: {
language: string;
customer: string;
};
userAttributes: {
email: string;
name: string;
preferred_username: string;
sub: string;
};
};
triggerSource: string;
}
Whenever an action that needs an email is made, Cognito triggers our lambda with a different triggerSource for each action.
The triggerSource changes the logic of the email that will be sent.
Templates
Using handlebars we replace the variables inside the template with the values we have.
> The file extension is .hbs just for handlebars convention, it works just like html
So a template like this:
<html>
<body>
<p>Hi {{name}}! This is your code: {{code}}</p>
</body>
</html>
and a payload like this:
{
name: 'User',
code: 123456
}
turns into this email:
<html>
<body>
<p>Hi User! This is your code: 123456</p>
</body>
</html>
Available values
username: the user's username at dadosfera;
dadosfera_url: the main dadosfera url for the environment
customer: the customer of the user, as registered on DUC
code: the email secret code (like the code to "Forgot Password")
title: text that will appear first on the email and on the web browser tab name (currently is the same as the subject)
email_address: the user's email address
In order to use any of this variables on the email all one needs is to use {{variable}} on the email template where variable can be any of the ones listed above.
Path to the template [ 🚨 IMPORTANT ]
All templates must be on src/assets/templates/{language}, where language can be en-us or the pt-br folder.
The name of the templates must remain the same: welcome.hbs for when creating users and forgot_password.hbs for first access and forgot password emails.
Environment variables:
ENV=local|stg|prd
SOURCE_EMAIL=Suporte Dadosfera <suporte@dadosfera.ai>
KEY_ARN=(arn of the KMS created for cognito to encrypt sensitive codes)
if ENV == 'local', emails are not sent, but only logged and opened in the browser.
Tests
if some unit test using snapshot fails but the test behavior is expected to change, you can run this command to update the snashot:
npm test -- -u
be careful to only update the snapshot if you have carefully verified the difference of the files!
Github repo
NodeJs service using AWS SES to send emails and handlebars to handle email templates.
How it works
Cognito
This service is prepared to handle Cognito Custom Email calls, with payloads like this:
export interface MailingEvent {
userName: string;
request: {
type: 'customEmailSenderRequestV1';
code: string;
clientMetadata: {
language: string;
customer: string;
};
userAttributes: {
email: string;
name: string;
preferred_username: string;
sub: string;
};
};
triggerSource: string;
}
Whenever an action that needs an email is made, Cognito triggers our lambda with a different triggerSource for each action.
The triggerSource changes the logic of the email that will be sent.
Templates
Using handlebars we replace the variables inside the template with the values we have.
> The file extension is .hbs just for handlebars convention, it works just like html
So a template like this:
<html>
<body>
<p>Hi {{name}}! This is your code: {{code}}</p>
</body>
</html>
and a payload like this:
{
name: 'User',
code: 123456
}
turns into this email:
<html>
<body>
<p>Hi User! This is your code: 123456</p>
</body>
</html>
Available values
username: the user's username at dadosfera;
dadosfera_url: the main dadosfera url for the environment
customer: the customer of the user, as registered on DUC
code: the email secret code (like the code to "Forgot Password")
title: text that will appear first on the email and on the web browser tab name (currently is the same as the subject)
email_address: the user's email address
In order to use any of this variables on the email all one needs is to use {{variable}} on the email template where variable can be any of the ones listed above.
Path to the template [ 🚨 IMPORTANT ]
All templates must be on src/assets/templates/{language}, where language can be en-us or the pt-br folder.
The name of the templates must remain the same: welcome.hbs for when creating users and forgot_password.hbs for first access and forgot password emails.
Environment variables:
ENV=local|stg|prd
SOURCE_EMAIL=Suporte Dadosfera <suporte@dadosfera.ai>
KEY_ARN=(arn of the KMS created for cognito to encrypt sensitive codes)
if ENV == 'local', emails are not sent, but only logged and opened in the browser.
Tests
if some unit test using snapshot fails but the test behavior is expected to change, you can run this command to update the snashot:
npm test -- -u
be careful to only update the snapshot if you have carefully verified the difference of the files!