UNPKG

create-chuntianxiaozhu

Version:

春天小猪模板工具

48 lines (44 loc) 999 B
import { createTransport } from 'nodemailer'; import { generateCode, isDev } from './common'; export interface SendEmailArgs { from: string; to: string; subject: string; html: string; } export async function sendEmail(args: SendEmailArgs) { const transport = createTransport({ service: 'QQ', auth: { user: process.env.ADDRESS, pass: process.env.STMPCODE, }, }); try { const result = await transport.sendMail(args); return result; } catch { return false; } } /** * 发送邮件验证码 * @param toAddress */ export async function sendCode(toAddress: string) { const code = generateCode(); if (isDev()) { // 如果是开发环境直接返回code,不发送邮件 return '111111'; } const result = await sendEmail({ from: `"认证邮件"<${process.env.ADDRESS}>`, to: toAddress, subject: '注册验证', html: `<h3>注册验证码是${code}</h3>`, }); if (result) { return code; } return result; }