Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a connection in nodemailer

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: '[email protected]',
    to:'[email protected]',
    subject:'Hello!',
    html: 'test',
    body:'test'
},
function(error, success){
    console.log(error);
    console.log(success);
    console.log('Message ' + success ? 'sent' : 'failed');
});
like image 768
Prachi g Avatar asked Mar 18 '23 13:03

Prachi g


1 Answers

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: '[email protected]',
        to: '[email protected]',
        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();
        }
    })
};
like image 50
Prachi g Avatar answered Mar 28 '23 08:03

Prachi g