Skip to content
Snippets Groups Projects
mailer.md 3.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • ## 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](https://nestjs.com/) framework. We refer the reader to the [NestJS-based micro-services](../../miscellaneous/nestjs-micro-services.md) page for further details concerning the latter framework and the features it provides.
    
    
    ![mail-service](../../assets/mail-service.png)
    
    
    <!-- The entrypoint of the service is a REST API provided by a [NestJS](https://github.com/nestjs/nest) application. The service builds email bodies based on the information it receives and on the provided HTML templates. It then format a JSON with all the properties (to, from, body...) expected by an SMTP server to correctly send an email.
    
    However the service does not send this JSON directly to the distant SMTP server. Indeed as a connection failure might occure, we chose to persist this object in a RabbitMQ queue. Then a small worker written in Node.js will consume the messages from the queue and send it to the SMTP server if correctly formatted. The messages will be removed (acknoledged) from the queue only if the SMTP received the message. -->
    
    ## Templates
    
    In order to generate responsive (on most-popular email clients) email templates, we use the framework [MJML](https://mjml.io/).
    
    There are different ways to install it, an easy one is with `npm`.
    
    ```bash
    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:
    
    ```ts
    ${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.
    
    ```ts
    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