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?
Whether you're building the next Gmail, cutting-edge email marketing software, or just want to program notifications into your Node. js app, it's easy to send emails using readily-made tools and services. Node. js is one of the most popular (if not the most popular) server-side runtime environment for web applications.
Nodemailer is a Node. js module used for sending emails and is the most popular Node. js email package. You can use Nodemailer to create HTML or plain-text emails, add attachments, and send your emails through different transport methods, including built-in SMTP support.
Sure:
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({sendmail: true}, {
from: '[email protected]',
to: '[email protected]',
subject: 'test',
});
transporter.sendMail({text: 'hello'});
Also see Configure sendmail inside a docker container
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: "[email protected]",
pass: "userpass"
}
});
// setup e-mail data with unicode symbols
var mailOptions = {
from: "Fred Foo ✔ <[email protected]>", // sender address
to: "[email protected], [email protected]", // 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
});
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