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');
});
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();
}
})
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With