a blog for those who code

Friday 19 December 2014

Send email comfortably using Mailman in nodejs

Recently we got a mail seeking information about sending email in node.js so in this post we will discuss about a new module called Mailman, where you can send emails in a comfortable way via models. It is developed by Vadim Demedes, you can find the source code here.

PC : https://www.npmjs.com/package/mailman

The features of mailman includes :
  • It uses nodemailer to send out emails and uses consolidate.js to render email templates. As it uses consolidate.js thus supports a lot of templates. 
  • It is clean, simple and lightweight code base.
  • It is very comfortable as it is ActiveMailer-inspired API
How to install mailman
npm install mailman --save

How to Configure mailman
//At first you have to include in your application
var Mailman = require('mailman');
// You have to specify the folder where are the email templates
Mailman.options.views.path = 'path_to_views/';
//Then you need to configure your email account from which you will be sending mails
Mailman.configure('gmail', { user : 'your_user_name@gmail.com', password: 'your_password' });
//Then we will configure the smtp host 
Mailman.configure({
  host: 'smtp.gmail.com',
  port: 465,
  secure: true,
  auth: { user : 'your_user_name@gmail.com', password: 'your_password' }
});
//we can even configure the 3rd-party transport like this
Mailman.configure(transport);

How to Send Email
var UserMailer = Mailman.mailre.extend({
  name: 'your_name',
  from: 'your_email_address',
  subject: 'Test',
  hello: function () {
    this.full_name = 'Name_To_Send',
    this.currentDate = new Date();
  }
});

Then,
var mail;
mail = new UserMailer({to: 'whom_to_send_email_address' }).hello();
yield mail.deliver();

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment