Dockerized Adonis application to send e-mails with MJML
docker-compose.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
.env
PORT=<port> # required
HOST=<host> # default 0.0.0.0
CORS_ORIGIN=<allowed_cors_origin> # optional - string only - if not provided, only the current origin where the image is running is allowed
MAIL_DRIVER=<mail_driver> # required - mailgun or smtp
# if you choose MAIL_DRIVER=smtp
SMTP_HOST=<smtp_host>
SMTP_PORT=<smtp_port>
SMTP_SECURE=<smtp_port> # boolean
SMTP_USERNAME=<smtp_username>
SMTP_PASSWORD=<smtp_password>
# if you choose MAIL_DRIVER=mailgun
MAILGUN_API_KEY=<mailgun_api_key> # optional
MAILGUN_DOMAIN=<mailgun_domain> # optional
MAILGUN_BASE_URL=<mailgun_base_url> # optional
# default value for e-mails - can be set or override in request body
SENDER_MAIL=<sender_mail> # optional
SENDER_NAME=<sender_name> # optional
REPLY_TO_MAIL=<reply_to_mail> # optional
REPLY_TO_NAME=<reply_to_name> # optional
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 header.
Should be a string, examples:
*
- Allow all origin*.example.com
- Allow all subdomain of example.comexample.com
- Allow only requests from exemple.comexample-1.com,example-2.com
- Allow requests from example-1.com and example-2.com- etc...
Adonis documentation - Allowed origin
You should send a POST
request to /emails/send
In development mode: http://localhost:3333/emails/send
Request body:
{
// required if SENDER_MAIL environment variable is not set
"fromEmail": "[email protected]",
// optional or equal to SENDER_NAME
"fromName": "Team Example",
// required
"toEmail": "[email protected]",
// optional
"toName": "User Name",
// optional or equal to REPLY_TO_MAIL
"replyToEmail": "[email protected]",
// 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"
}
To use it, you must not have volume in docker-compose.yml
and not provide template
in request body
The default template:
<mjml>
<mj-head>
<mj-attributes>
<mj-all font-family="'IBM Plex Sans', 'Georgia', 'Times New Roman', serif" />
<mj-text font-size="18px" color="{{ textColor }}" line-height="2" padding="0" />
</mj-attributes>
<mj-font
href="https://fonts.googleapis.com/css?family=DM+Serif+Display:normal,italic,bold&display=swap"
name="DM Serif Display"
/>
<mj-font
href="https://fonts.googleapis.com/css?family=IBM+Plex+Sans:normal,italic,bold&display=swap"
name="IBM Plex Sans"
/>
</mj-head>
<mj-body background-color="#F2F2F2">
<mj-section padding="32px">
<mj-column>
<mj-image
padding="0px"
href="{{ logoLink }}"
src="{{ logoSrc }}"
alt="logo"
width="150px"
/>
</mj-column>
</mj-section>
<mj-section background-color="#FFFFFF" border-radius="24px" padding="48px">
<mj-column>
@if(title)
<mj-text
color="{{ titleColor }}"
font-size="32px"
line-height="1.25"
padding-bottom="20px"
font-family="'DM Serif Display', 'Georgia', 'Times New Roman', serif"
>
<strong> {{ title }}, </strong>
</mj-text>
@endif @if(content)
<mj-text> {{{ content }}} </mj-text>
@endif
</mj-column>
</mj-section>
<mj-section padding="48px">
<mj-column>
<mj-image
align="left"
padding="0px"
href="{{ logoLink }}"
src="{{ logoSrc }}"
width="134px"
></mj-image>
</mj-column>
</mj-section>
</mj-body>
</mjml>
- Send request with a body like this
{
// 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": "<p style="margin: 0">E-mail content</p>",
// content of e-mail, can be written in HTML
"subject": "Welcome",
// content of e-mail, can be written in HTML
"toEmail": "[email protected]",
}
- Result
Templates should be edge files and use the templating syntax of Adonis (see default template example)
Adonis templating syntax documentation
- Provide your templates
Provide your templates in docker-compose.yml
volume configuration
version: '3.8'
services:
mailer:
...
volumes:
- ./path/to/local/emails/directory:/app/resources/views/templates
- Provide the template to use in body request
{
...
// is the file name - Ex: example.edge
"template": "example"
...
}
make dev
make build
make lint
make format
make check-update
node ace -h
node ace make:controller <% ControllerName %>
You should use this command when the container isn't already initialized
make docker-up-build
make docker-up
make docker-logs
make docker-stop
make docker-down
make docker-build