Mailer
Mailer module is abstraction of node-mailer, and mustache for email template and email subject rendering with variables. However, you can use mailer with plain html, or even replace mustache with another renderer (eg. nunjucks).
Creating and configuring Mailer
To instantiate the Mailer class and prepare it for sending email you need to pass configuration as following:
Configuration Object
All keys are mandatory unless stated differently.
key
type
description
from
string
default sender, used in format "name" <email>
, can be overridden
transport.host
string
SMTP
transport.port
number
SMTP PORT (eg. 25, 587, 465)
transport.secure
boolean
whether to use encrypted transport
transport.auth
object
SMTP server credentials (auth.user
, auth.pass
)
renderer (optional)
function
TIP: If you are looking for free/cheap email plans, you can check Mailjet, or Amazon SES.
Mailer.send(options
)
options
)Mailer send function expects options
object. All keys are mandatory unless stated differently.
key
type
description
from (optional)
string
if you want to override default sender
to
string
comma separated emails of recipients
html
string
body of the message, will be parsed with renderer
subject
string
subject of the message, will be parsed with renderer
variables (optional)
object
variables to use in html
and subject
when parsing the template
baseTemplate (optional)
string
optional base template to wrap the html
, needs to include {{email_body}}
which will be replaced with parsed html
Sending your first email
In the previous step we prepared Mail
for sending, so sending is straightforward:
And that's it!
Sending email with variables
Most of the time, you want to include some dynamic data in email subject, or email body. Mailer module supports templating thanks to mustache:
This will result sending email with subject Hello dan
, and body <div>Hi Dan Radenkovic</div>
.
Sending email with base template
Often, you want to have consistent header and footer in email, so you design a base template, and you inject different messages in it. To send such email, you just need to pass baseTemplate
string, which has to include {{{email_body}}}
(notice that it's wrapped in three curly braces so html does not get escaped) variable, which will be replaced with parsed html
of the email. Base template can use passed variables too.
This will result sending email body <html>from base template I say <div>Hi Dan Radenkovic</div></html>
.
Replacing the renderer
If you want to use more robust template renderer, you can pass renderer function when instantiating the mailer class:
The renderer
function should accept two arguments: content
(string with some tags to be replaced), and variables
, object with variables and it should return parsed string
.
Last updated