sending an email in nodejs

/****To send a mail  **
steps to follow
1. get email server details
2. create a smtp transport connection
3. send a mail via connection
4. close connection
**/
var fs = require('fs');
var nodemailer = require('nodemailer');

//Mail server details
var host = '';
var port = '';
var ssl = false;
var userName = '';
var password = '';

//Generic email transport connection
var getMailTransportCon = function() {
    var smtpoptions = {
        host: host,
        port: port,
        secureConnection: ssl,
        auth: {
            user: userName,
            pass: password
        }
    };

    //Create a mail transport connection
    var mailTransport = nodemailer.createTransport('SMTP', smtpoptions);
    return mailTransport;
}


var sendMail = function(to, cc, bcc, subject, text, filePath) {

    var mailOptions = {
        from: userName,
        to: to,
        cc: cc,
        bcc : bcc,
        subject: subject,
        text: text,
        attachments: [{
            filePath: filePath
        }]
    };

    var mailTransport = getMailTransportCon();
    //Send a mail
    mailTransport.sendMail(mailOptions, function(error, info, response) {
        if (error) {
            console.log('error', error);
        } else if (info) {
            console.log('info', info);
        } else if (response) {
            console.log('response', response);
        }
        //Close connection
        mailTransport.close();
    });

}



sendMail('devarajc@', 'devarajcsessn@gmail.com' , 'devarajchidambaram@gmail.com' , 'Test mail' , 'test mail no issues' , 'google.png');

Comments

Popular posts from this blog

Proxy setting in java

Using logstash to import csv json files into elasticsearch

Kibana 4 Installation and Run as a service in ubuntu