Arrow keys: ← previous · → next 14 of 35
@nodedk/mail
0.2.2 stable

mail#

Send HTML and plain-text email through Nodemailer.

Requirements#

SMTP access for delivery. The JSON transport works locally without a mail server.

Install#

npm i @nodedk/mail

Usage#

Create the mailer once and inject it into the server's plugins, as in scaffold:

// app/plugins/mailer.js
var mail = require('@nodedk/mail')
var env = require('#env.js')

module.exports = function () {
  return mail({
    host: env.mailer_host,
    port: env.mailer_port,
    secure: env.mailer_secure,
    auth: { user: env.mailer_user, pass: env.mailer_pass },
    from: env.mailer_from,
    replyTo: env.mailer_reply_to
  })
}

Send through the injected function:

module.exports = async function welcome(state, email) {
  return state.plugins.mailer({
    to: email,
    subject: 'Welcome',
    html: '<h1>Welcome</h1><p>Your account is ready.</p>'
  })
}

API#

mail(config = {})#

Creates a Nodemailer transport and returns an asynchronous send(options = {}) function. config supplies transport settings and default message fields.

Transport settings include host, port, secure, auth: { user, pass }, and jsonTransport. They are passed unchanged to nodemailer.createTransport.

send(options = {})#

Merges message options over config and returns Nodemailer's send-result promise.

  • to, from, cc, bcc, replyTo: recipient and sender fields.
  • subject: subject line.
  • html: HTML body, default ''.
  • text: plain-text body. When nullish, generated from html; an explicit empty string is preserved.
  • attachments: Nodemailer attachment objects, such as { filename, content } or { filename, path }.

Other transport and message fields follow the Nodemailer API. Sending failures reject the promise.

Examples#

Inspect a message without sending it:

var mail = require('@nodedk/mail')

async function main() {
  var send = mail({ jsonTransport: true, from: 'app@example.com' })
  var result = await send({
    to: 'ada@example.com',
    subject: 'Hello',
    html: '<p>Hello Ada</p>',
    attachments: [{ filename: 'hello.txt', content: 'Hello Ada' }]
  })

  var message = JSON.parse(result.message)
  console.log(message.text) // Hello Ada
}

main().catch(console.error)

Created by Vidar Eldøy