Skip to content
Snippets Groups Projects
mail.md 2.38 KiB
Newer Older
Bastien DUMONT's avatar
Bastien DUMONT committed
# Mail

Hugo NOUTS's avatar
Hugo NOUTS committed
## Explanation
Two templating libraries are used, they allow easy editing and responsive :
Bastien DUMONT's avatar
Bastien DUMONT committed

- [Handlebars](https://handlebarsjs.com/) is used as a semantic template motor. It allow us to split email template in order to reuse some parts (header, footer ...)
- [MJML](https://mjml.io/) is a lib providing a syntax allowing developers to easily build responsive templates.

## Usage

### Scaffolding

Mails templates can be found inside `src/notifications`. All reusable parts can be found in `base` folder.

All stylesheet must be include inside `style.hbs`.

### Create your template

Bastien DUMONT's avatar
Bastien DUMONT committed
Simply create your template file `template.hbs` inside the notifications folder.

```xml title="template.hbs"
<mj-hero background-color="radial-gradient(96.2% 96.2% at 50% 3.8%, #343641 0%, #1B1C22 100%)" padding="17px">
  <mj-text align="center" color="white" padding-top="20px" font-size="24px" font-family="Lato" font-weight="normal">
    <img style="vertical-align: middle; margin-right: 12px" src="https://ecolyo.com/assets/ecolyo-icon.svg" alt="Ecolyo"/>
  {{title}}
  </mj-text>
</mj-hero>
```

### How to use it ?

#### Inside a cozy service

1. Import the mjml lib in order to compile the template.
Bastien DUMONT's avatar
Bastien DUMONT committed
```js
import mjml2html from 'mjml';
2. Link all the info mandatory to render your email :
Bastien DUMONT's avatar
Bastien DUMONT committed

```js
const template = monthlyReportTemplate({
  title: 'Du nouveau dans votre espace Ecolyo !',
});
3. Build the email and send it:
Bastien DUMONT's avatar
Bastien DUMONT committed

```js
const mailData = {
  mode: 'noreply',
  subject: '[Ecolyo] - Votre bilan mensuel',
  parts: [
    {
      type: 'text/html',
      body: mjml2html(template).html,
    },
  ],
};
mailService.SendMail(client, mailData);
```

#### Inside the app
Bastien DUMONT's avatar
Bastien DUMONT committed

Hugo NOUTS's avatar
Hugo NOUTS committed
It's very similar to service usage, but because we use react, the app rendering is done in the user browser. That's why we need to change the import for mjml lib into :
Bastien DUMONT's avatar
Bastien DUMONT committed
```js
const mjml2html = require('mjml-browser');
Hugo NOUTS's avatar
Hugo NOUTS committed
### Useful links
Bastien DUMONT's avatar
Bastien DUMONT committed

- [Mjml live editor](https://mjml.io/try-it-live/bohYnxKkCq-)

## Bounce token

A bounce token stored in the profile of each user is sent in email. It allows us to track if user clicks on links.

## Unsubscribe

Bastien DUMONT's avatar
Bastien DUMONT committed
A user can unsubscribe to our newsletter by a link in each mails. It **does not** require authentication. This link is built with a `sharecode` token that contains specific permissions to turn off the `sendAnalysisNotification` property in the user profile. The sent link has a TTL of 15 days.