Guides
Tutorials
Pop Tags

Mailer Module

var mailer = require("mailer");

The mailer is used to send transactional emails. The module exposes one method:

mailer.send(to, subject, message, options);

The send method accepts the following options:

html the html for the body of the email.
text a text version of the email body for plaintext clients.
attachments files to be sent as attachments, accepts an array of the standard javascript objects which file fields output.
reply_to specify the reply to email address
from specify which address the email is sent from. *requires additional setup, see below.

You can pass in multiple options like so:

mailer.send(email, subject, {html: someHtml, text: fallbackText})

This sends an html version to html email clients, and a text version to plain text clients.

*By default the mails are sent from no-reply@yourwebisonline.com, to send from another address please contact us and we’ll help to set it up (it requires validating the email address and you should also set up correct SPF records for the domain).

Here’s a simple example of an extension that will show an newsletter signup form and send a mail on submission:

var mailer = require('mailer');

exports.form = function() {
  if (request.request_method && request.params.email) {
    var message = "The email is: " + request.params.email;

    mailer.send("my-client@example.com", "Newsletter signup", message);

    return {html: "<h2>Thank you for signing up!</h2>"};
  } else {
    return {
      html: '<form method="post">' +
              '<label>Your email: <input type="email" name="email"/></label>' +
              '<button>Sign up</button>' +
            '</form>'
    };
  }
};

You can handle files submitted from a form and include them as attachments in an email like this:

var mailer = require('mailer');

exports.form = function() {
  if (request.request_method && request.params.email) {
    var message = "The email is: " + request.params.email;

    mailer.send("my-client@example.com", "Newsletter signup", message, {attachments: [request.params.file]});

    return {html: "<h2>Thank you for signing up!</h2>"};
  } else {
    return {
      html: '<form method="post" enctype="multipart/form-data">' +
              '<p>' +
                '<label>Your email: <input type="email" name="email"/></label>' +
              '</p>' +
              '<p>' +
                '<label>Your file: <input type="file" name="file"/></label>' +
              '</p>' +
              '<p><button>Send file</button></p>' +
            '</form>'
    };
  }
};
Continue to XML Module »