跳到主要内容

smtp 推送通知

备注

代码粘贴自青龙面板自带的通知脚本,很多地方用得到,所以值得保存一下。

import NodeMailer from "nodemailer";
export async function smtpNotify(text: string, desp: string) {
const SMTP_EMAIL = process.env.SMTP_EMAIL; // smtp 账号
const SMTP_PASSWORD = process.env.SMTP_PASSWORD; // smtp 密码
const SMTP_SERVICE = process.env.SMTP_SERVICE; // 邮件服务器
const SMTP_NAME = process.env.SMTP_NAME; // 发送人名称
if (![SMTP_EMAIL, SMTP_PASSWORD].every(Boolean) || !SMTP_SERVICE) {
return;
}

try {
const transporter = NodeMailer.createTransport({
service: SMTP_SERVICE,
auth: {
user: SMTP_EMAIL,
pass: SMTP_PASSWORD,
},
});

const addr = SMTP_NAME ? `"${SMTP_NAME}" <${SMTP_EMAIL}>` : SMTP_EMAIL;
const info = await transporter.sendMail({
from: addr,
to: addr,
subject: text,
html: `${desp.replace(/\n/g, "<br/>")}`,
});

transporter.close();

if (info.messageId) {
console.log("SMTP 发送通知消息成功🎉\n");
return true;
}
console.log("SMTP 发送通知消息失败😞\n");
} catch (e) {
console.log("SMTP 发送通知消息出现异常😞\n", e);
}
}