Mailer
Last updated
Last updated
Mailer module is abstraction of , and 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. ).
To instantiate the Mailer class and prepare it for sending email you need to pass configuration as following:
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
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
And that's it!
This will result sending email with subject Hello dan
, and body <div>Hi Dan Radenkovic</div>
.
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>
.
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
.
optional function that has two arguments (content, variables)
described
TIP: If you are looking for free/cheap email plans, you can check , or .
In the we prepared Mail
for sending, so sending is straightforward:
Most of the time, you want to include some dynamic data in email subject, or email body. Mailer module supports templating thanks to :