Configure nodejs to send emails and nodejs to send emails
1. Use nodemailer
npm install nodemailer --save-dev
2. Create a mail sending toolkit, such as email_server.js.
Var nodemailer = require ('nodemailer'); var internals ={}; internals. extend = function (o, n) {for (var p in n) {if (n. hasOwnProperty (p) {o [p] = n [p] ;}}; return o ;}; var mailConfig = {service: 'qq', auth: {user: '2014 * 29@qq.com ', pass:' ********** '}; var mailOptions = {from: '2014 * 9@qq.com',: 'noahli @**. com ', subject: 'test mail', text: 'test mail' content. Please do not reply'}; internals. sendEmail = function (_ config, _ options, attachment) {mailConfig = internals. extend (mailConfig, _ config); mailOptions = internals. extend (mailOptions, _ options); // execute the send operation var transporter = nodemailer. createTransport (mailConfig); transporter. sendMail (mailOptions, function (error, info) {if (error) {console. log ("email sending error:" + error);} else {console. log ("email sent successfully:" + info. response) ;}}) ;}; exports = module. exports = internals;
3. Use tools to send emails
Var email_tools = require ('. /service/email_server '); // send the mailbox configuration var config = {service: 'qq', auth: {user: '2017 *** 9@qq.com', pass: '*******'}; // configure var options in the inbox = {subject: "Test mail", text: "I am mail content",: "noahli @****. com ", cc:" 870 *** 29@qq.com, 51 *** 78@qq.com ", sender:" imdou8 ", attachments: [{filename: 'app. js', // content: 'Hello world! 'Path: "app. js"}], html: {path: "tpl/index.html" }}; email_tools.sendEmail (config, options );