-
FORESTIER Fabien authoredFORESTIER Fabien authored
Mail service
Features
This service allow to deliver mails to any email address from the address specified in the configuration. It also provides two particular endpoints, one for the user to give a feedback and another one for general contact purpose. Those endpoints will both send an email to the admin address specified in the configuration.
Dependencies
Endpoints
/contact
: formulaire de contact
/feedback
:
/send
/health
This service will return a 200
http status code when all indicators are healthy. Otherwise it will return a 503
http status code.
For this service we declared an health indicator that verify that the connection to the SMTP Server is available.
Implementation
The service is implemented using the NestJS framework. We refer the reader to the NestJS-based micro-services page for further details concerning the latter framework and the features it provides.
Templates
In order to generate responsive (on most-popular email clients) email templates, we use the framework MJML.
There are different ways to install it, an easy one is with npm
.
npm install -g mjml
It uses its own file extention (.mjml
) and syntax that abstracts the whole layer of complexity related to responsive email design. Once the template done and the content updated we can convert the mjml syntax to html using the following command:
mjml my-file.mjml -o my-file.html
As we needed to add some dynamic content in our mails, we used the power of template literals. Indeed when we want to add some dynamic content in the template, we used the following syntax:
${myVariableName}
Once the html is generated, we copy it inside a template literal and put it inside a js function. This function is taking parameters which corresponds to the name of the variables inside the template. As the function also returns the email body as template literal the ${variableName}
will automatically replaced. Here is an example.
export const buildFeedbackEmail = (myVariable) => {
const html = `<html>.... ${myVariable} ......</html>`
return html;
};
So, everytime we need to use this template, a simple call to this function with the appropriate parameters will do the job.
AUTHZ
email-writer