nodemailer with sails.js

761 views
Skip to first unread message

Birendra Kumar

unread,
Aug 21, 2015, 5:28:23 AM8/21/15
to sails.js
How to integrated nodemailer with sails.js?

John Tomaselli

unread,
Sep 9, 2015, 11:58:21 PM9/9/15
to sails.js
Sails10,   "nodemailer",  "version": "1.3.2" Example
Ceate a service EmailService.js
var nodemailer = require('nodemailer');

var opts = {
    
    service: 'Gmail',
    auth: {
        user: 'x...@gmail.com',
        pass: 'password'
    }
}



var transporter = nodemailer.createTransport(opts);

exports.sendEmail = function (options) {
//  console.log('=================================\n')
    console.log('to ', options.from,options.to)
    // from always uses opts auth user
    var mailOptions = {
        from: options.from,
        sender: options.from,
        to: options.to,
        subject: options.subject,
        text: options.text,
        html: options.html
        //  replyTo:options.replyTo,
        //  attachments: options.attachments
    };
    transporter.sendMail(mailOptions, function (error) {
        if (error) {
            console.log('Error occured');
            console.log(error.message);
            return;
        }
        console.log('Message sent successfully!');
    });

};

From a controller (setup variables to pass)
 mtext = adjf + ' ' + adjl + " batch " + nextno + " data entries dated : " + dd + ' \r';
 newdata = 'This is batch no ' + nextno + " data entries dated : " + dd + '\n You can adjust and entries  through File Time Sheet option.</br> 0 values are null entries. </br>' + newdata;
            var mailOptions = {
              from: "some...@somemail.com",
               to: "somebo...@somewhere.com",
              subject: adj,
              text: mtext,
              html: newdata + '</br>'
            }

            EmailService.sendEmail(mailOptions);

           
          });
HTH
John
Reply all
Reply to author
Forward
0 new messages