6

I am using nodemailer to send e-mails in nodejs. I am able to send the mails, but the script doesn't terminate. I don't know how to close the connection.

This is the code:

var nodemailer = require('nodemailer');
nodemailer.SMTP = {
  host: 'localhost'
}
nodemailer.send_mail(
{
    sender: 'me@example.com',
    to:'my_gmail_nickname@gmail.com',
    subject:'Hello!',
    html: 'test',
    body:'test'
},
function(error, success){
    console.log(error);
    console.log(success);
    console.log('Message ' + success ? 'sent' : 'failed');
});
Prachi g
  • 849
  • 3
  • 9
  • 23

1 Answers1

9

I got it working like this:

var nodemailer = require('nodemailer');

var transport = nodemailer.createTransport("SMTP", {
    host: 'localhost',
});

var send_email = function (email_content) {
    var mailOptions = {
        from: 'me@example.com',
        to: 'my_gmail_nickname@gmail.com',
        subject: 'Hello!',
        html: email_content.content
    };

    transport.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log(error);
        } else {
            console.log('Message sent: ' + info.message);
            transport.close();
        }
    })
};
Prachi g
  • 849
  • 3
  • 9
  • 23