Nodemailer is a simple and easy-to-use Node.js email sending component. The specific operations are as follows
1. Install nodemailer
npm install nodemailer --save
2. Features
The main features of Nodemailer include:
The above functional features have covered most of the needs for sending emails. Next, let us start writing the program.
3. Simple example
This is a complete example to send email with clear text and HTML body
var nodemailer = require('nodemailer'); // create reusable transporter object using the default SMTP transport var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass@smtp.gmail.com'); // 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 transporter.sendMail(mailOptions, function(error, info){ if(error){ return console.log(error); } console.log('Message sent: ' + info.response); });
4. Common mistakes
{ [AuthError: Invalid login - 454 Authentication failed, please open smtp flag first!] name: 'AuthError', data: '454 Authentication failed, please open smtp flag first!', stage: 'auth' }
Cause of error: The account has not set up this service
Solution: QQ mailbox -> Settings -> Account -> Enable service: POP3/SMTP service
{ [SenderError: Mail from command failed - 501 mail from address must be same as authorization user] name: 'SenderError', data: '501 mail from address must be same as authorization user', stage: 'mail' }
Cause of error: The sending account and the authentication account are different, that is, the username and password do not match.
The above is the entire content of this article, I hope it will be helpful to everyone’s study.