I come from the world of PHP and I'm accustomed to using mail() to send quick diagnostic emails on occasion. Is there a module or method in the standar library of NodeJS that's roughly the equivalent of this?
Asked
Active
Viewed 7,387 times
2 Answers
16
Sure:
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({sendmail: true}, {
from: 'no-reply@your-domain.com',
to: 'your@mail.com',
subject: 'test',
});
transporter.sendMail({text: 'hello'});

Igor Sukharev
- 2,467
- 24
- 21
10
Nodemailer is a popular, stable, and flexible solution:
Full usage looks something like this (the top bit is just setup - so you would only have to do that once per app):
var nodemailer = require("nodemailer");
// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "gmail.user@gmail.com",
pass: "userpass"
}
});
// setup e-mail data with unicode symbols
var mailOptions = {
from: "Fred Foo ✔ <foo@blurdybloop.com>", // sender address
to: "bar@blurdybloop.com, baz@blurdybloop.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world ✔", // plaintext body
html: "<b>Hello world ✔</b>" // html body
}
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
// if you don't want to use this transport object anymore, uncomment following line
//smtpTransport.close(); // shut down the connection pool, no more messages
});

hunterloftis
- 13,386
- 5
- 48
- 50
-
13Late to the party here. With nodemailer you need an auth. PHP mail() method you dont need an auth right? It will just send to email you specify in the script. At least this is my experience. Is there anything like this in node that does not need auth to send to gmail... I can send an email to my gmail from a php script with no password specified in an auth, but in nodemailer I have to have the auth there – Chipe Jul 18 '16 at 17:50
-
2It's also possible with Nodemailer. I've edited this answer to include a direct link. https://nodemailer.com/transports/sendmail/ – Neonit Jan 10 '18 at 15:27