An open API service indexing awesome lists of open source software.

https://github.com/louismazel/docker-adonis-mailer

Dockerized Adonis application to send e-mails in MJML
https://github.com/louismazel/docker-adonis-mailer

adonisjs docker mjml typescript

Last synced: about 2 months ago
JSON representation

Dockerized Adonis application to send e-mails in MJML

Awesome Lists containing this project

README

          

# docker-adonis-mailer

> Dockerized Adonis application to send e-mails with MJML

## Usage

### Build your docker-compose.yml

`docker-compose.yml`

```yml
version: '3.8'

services:
mailer:
restart: always
image: louismazel/mailer:latest # it's better to fix the version - louismazel/mailer:v1.2.9
ports:
- 3333:3333 # or PORT choosen
env_file:
- .env
# optional
volumes:
- ./src/emails:/app/resources/views/templates
```

### Environment variables file

`.env`

```env
PORT= # required
HOST= # default 0.0.0.0

CORS_ORIGIN= # optional - string only - if not provided, only the current origin where the image is running is allowed

MAIL_DRIVER= # required - mailgun or smtp

# if you choose MAIL_DRIVER=smtp
SMTP_HOST=
SMTP_PORT=
SMTP_SECURE= # boolean
SMTP_USERNAME=
SMTP_PASSWORD=

# if you choose MAIL_DRIVER=mailgun
MAILGUN_API_KEY= # optional
MAILGUN_DOMAIN= # optional
MAILGUN_BASE_URL= # optional

# default value for e-mails - can be set or override in request body
SENDER_MAIL= # optional
SENDER_NAME= # optional
REPLY_TO_MAIL= # optional
REPLY_TO_NAME= # optional
```

#### CORS Origin configuration

You can control the origins to allow for the CORS request using the CORS_ORIGIN environment variables.
This property controls [the Access-Control-Allow-Origin](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin) header.

Should be a string, examples:

- `*` - _Allow all origin_
- `*.example.com` - _Allow all subdomain of example.com_
- `example.com` - _Allow only requests from exemple.com_
- `example-1.com,example-2.com` - _Allow requests from example-1.com and example-2.com_
- etc...

[Adonis documentation - Allowed origin](https://docs.adonisjs.com/guides/security/cors#allowed-origin)

### Sending e-mail

You should send a `POST` request to `/emails/send`

In development mode: `http://localhost:3333/emails/send`

Request body:

```json
{
// required if SENDER_MAIL environment variable is not set
"fromEmail": "me@example.com",
// optional or equal to SENDER_NAME
"fromName": "Team Example",
// required
"toEmail": "user@example.com",
// optional
"toName": "User Name",
// optional or equal to REPLY_TO_MAIL
"replyToEmail": "reply@example.com",
// optional or equal to REPLY_TO_NAME
"replyToName": "Team Example",
// required
"subject": "Subject of the e-mail",
// default true - if false you should provide a template written in HTML in edge file
"mjml": true,
// file name of your e-mail template - required if you use custom template
"template": "example"
}
```

### Use the default template

To use it, you must not have volume in `docker-compose.yml` and not provide `template` in request body

The default template:

```html
















@if(title)

{{ title }},

@endif @if(content)
{{{ content }}}
@endif






```

1. Send request with a body like this

```json
{
// Title in the e-mail
"title": "Hello World,",
// link open on logo click
"logoLink": "https://adonisjs.com/",
// URL of the logo
"logoSrc":
"https://camo.githubusercontent.com/076aacc894daf3d9065f7d5bd1d7e8a3d0511668576cd66afddd0ce4af524eaa/68747470733a2f2f692e696d6775722e636f6d2f32774764454a4e2e706e67",
// text color of e-mail
"textColor": "#1a1a19",
// title text color
"titleColor": "#5a45ff",
// content of e-mail, can be written in HTML
"content": "

E-mail content

",
// content of e-mail, can be written in HTML
"subject": "Welcome",
// content of e-mail, can be written in HTML
"toEmail": "example@site.com",
}
```

2. Result

![Default Template Example](./assets/img/default-template-example.png)

### Custom templates

Templates should be edge files and use the templating syntax of Adonis ([see default template example](./resources/views/emails/transactional.edge))

[Adonis templating syntax documentation](https://docs.adonisjs.com/guides/views/templating-syntax)

3. Provide your templates

Provide your templates in `docker-compose.yml` volume configuration

```yml
version: '3.8'

services:
mailer:
...
volumes:
- ./path/to/local/emails/directory:/app/resources/views/templates
```

4. Provide the template to use in body request

```json
{
...
// is the file name - Ex: example.edge
"template": "example"
...
}
```

---

## Contributing

### Run server in development mode

```bash
make dev
```

### Build application for production

```bash
make build
```

### Lint and format application

#### Lint with Eslint

```bash
make lint
```

#### Format files with Prettier

```bash
make format
```

### Check dependencies updates

```bash
make check-update
```

### Adonis commands

#### To see all adonis commands, run

```bash
node ace -h
```

#### Create a new controller

```bash
node ace make:controller <% ControllerName %>
```

### Docker

#### Build and start dev server

You should use this command when the container isn't already initialized

```bash
make docker-up-build
```

#### Start dev server in container

```bash
make docker-up
```

#### Show server logs

```bash
make docker-logs
```

#### Stop server

```bash
make docker-stop
```

#### Stops containers and removes containers, networks, volumes, and images created by up

```bash
make docker-down
```

#### Build docker image

```bash
make docker-build
```