@waveorb/mailer v1.0.1
Waveorb Mailer
Send emails with Waveorb. Boasts the following features:
- Send email with mxmail
- Layout support
- Supports HTML, Markdown and Mustache templates
- Automatically converts HTML to use as text version
Made for the Waveorb web app development platform.
Installation
npm i @waveorb/mailerTemplates
In app/layouts add a file called mail.js:
module.exports = async function(mail, $, data) {
  return /* HTML */`
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>${mail.subject || 'Waveorb Mailer'}</title>
        <style>
          body { background-color: gold; }
        </style>
      </head>
      <body>
        <div class="content">${mail.content}</div>
        <div>Best regards</div>
      </body>
    </html>
  `
}Then in your app/mail directory add a file called mail1.js (or whatever):
module.exports = async function($, data) {
  return {
    layout: 'mail',
    subject: 'mail1',
    content: `mail1 html content link ${data.key}`
  }
}The email content can be written in Markdown:
// ...
format: 'markdown',
content: `# Hello`
// ...The layout can't do Markdown, it has to be HTML.
You can use the file option to set the content from a file:
module.exports = async function($, data) {
  return {
    layout: 'mail',
    subject: 'mail1',
    file: 'data/markdown/mail.md'
  }
}The markdown will be automatically transformed to HTML if it's a markdown file.
Variables
You can pass variables through the data parameter:
await mailer.send('mail1', $, options, data)
// ...
content: `mail1 html content link ${data.key}`
// ...You can also use Mustache:
// ...
content: `mail1 html content link {{data.key}}`
// ...Both of these techniques work in the layout as well.
Configuration
If you don't provide a config file emails will be sent through mx record lookup.
To use your own email server to send mail, create a file called mail.yml in app/config:
host: smtp.ethereal.email
port: 587
auth:
  user: virginia.cassin10@ethereal.email
  pass: 1md9Xes49Nbfka6aFwCreate a plugin in app/plugins called mailer.js:
var mailer = require('waveorb-mailer')
module.exports = async function(app) {
  app.mailer = mailer(app.config.mail)
}Send email
Emails will automatically include the text version which is converted from the HTML in your templates.
// Use mailer from plugin
var mailer = $.app.mailer
// Send email
var options = {
  to: 'Vidar Eldøy <vidar@eldoy.com>',
  attachment: [file]
}
// All possible options:
{
  to: 'vidar@eldoy.com',
  from: 'vidar@eldoy.com',
  cc: 'cc@eldoy.com',
  bcc: 'bcc@eldoy.com',
  subject: 'hello',
  html: '<h1>Hello</h1>',
  text: 'Hello',
  replyTo: 'vidar@eldoy.com',
  attachment: [readStream]
}
// Parameters: name, $, options, data
var data = { key: 'hello' }
var result = await mailer.send('mail1', $, options, data)
// Returns delivered and failed emails
{
  delivered: [{ result, mail }],
  failed: [{ result, mail }]
}