Node.JS 入门 --- SendMail

1.使用Nodemailer发送邮件

在平时的使用场景中,我们很常见的会遇到需要发送邮件,下面是使用nodemailer邮件插件进行的邮件发送,下面是演示代码

将mail.js封装成模块

const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({ 
    host: 'smtp.163.com',
    port: 25, 
    auth: {
        user:'[email protected]', //发送方的邮箱
        pass: 'xxxx' // pop3 授权码
    }
});

let mail={
	transporter:transporter,
	send(mail,content){
      let mailOptions = {
		    from: '[email protected]', // sender address
		    to: mail, // list of receivers
		    subject: '欢迎注册', // Subject line
		    //text: content, // plain text body
		    html: content // html body
		};

	 //发送邮件,并且捕获错误
	 this.transporter.sendMail(mailOptions, (error, info) => {
	    if (error) {
	        return console.log(error);
	    }
	    console.log('Message sent: %s', info.messageId);
			});
	}
}
module.exports=mail

创建test.js 并调用mail.js

const mail=require('./mail.js')
mail.send(body.mailadd,'

欢迎注册

')

 

你可能感兴趣的:(NodeJs)