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.
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.
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