node发送email,解决方案

按照依赖包

  1. nodemailer
  2. nodemailer-smtp-transport

config.service可选的值

'1und1',
'AOL',
'DebugMail.io',
'DynectEmail',
'FastMail',
'GandiMail',
'Gmail',
'Godaddy',
'GodaddyAsia',
'GodaddyEurope',
'hot.ee',
'Hotmail',
'iCloud',
'mail.ee',
'Mail.ru',
'Mailgun',
'Mailjet',
'Mandrill',
'Naver',
'OpenMailBox',
'Postmark',
'QQ',
'QQex',
'SendCloud',
'SendGrid',
'SES',
'SES-US-EAST-1',
'SES-US-WEST-1',
'SES-EU-WEST-1',
'Sparkpost',
'Yahoo',
'Yandex',
'Zoho'

直接上代码

/**
 * 修改密码,找回密码,发送验证码
 * 2020-05-11
 */
'use strict';
const nodemailer = require('nodemailer')
const smtpTransport = require('nodemailer-smtp-transport');
const produceRandom = require('produce-random');
const config = {
	service: 'QQ',
	auth: {
		user: '******', //邮箱
		pass: '******', // 密码(该密码是获取的授权码,并非自己的邮箱密码,下面连接)
		[获取授权验证码](https://mail.qq.com/cgi-bin/frame_html?sid=Il3r6d10Ng62bCd5&r=e44820ec588276c81969dd73787eaf92)
	}
}

//创建
const transporter = nodemailer.createTransport(smtpTransport(config));

exports.main = async (event, context) => {
	let {
		recipient,
		type
	} = event
	/**
	 * recipient 收件人
	 * subject 发送的主题
	 * html 发送的html内容
	 * from 发送人
	 */
	let checkCode = produceRandom();
	let html = `

尊敬的xxx,您好!

我们检测到,您正在执行${type==='modifi' ? '修改密码' : '找回密码'}操作,为进一步保护您的账户信息安全,请在操作页面输入以下验证码。

验证码:${checkCode}

`
let subject = type==='modifi' ? '修改密码' : '找回密码'; let sendMailRes = await transporter.sendMail({ from: config.auth.user, to: recipient, subject, html }) if(sendMailRes.response == '250 OK: queued as.'){ return { status: 0, msg: "成功", checkCode } } else { return { status: -1, msg: "稍后重试" } } return { status: -1, msg: "稍后重试" } }

你可能感兴趣的:(服务器,前端)