Skip to content

邮箱服务

需要用的库

shell
npm i js-yaml
npm i nodemailer

授权邮箱的账号密码|授权码一般放置在 yaml 或 环境变量中

inde.js

import nodemailder from 'nodemailer' import yaml from 'js-yaml' import fs from 'node:fs' import http from 'node:http' import url from 'node:url' const mailConfig = yaml.load(fs.readFileSync('./mail.yaml', 'utf8')) const transPort = nodemailder.createTransport({ service: "qq", port: 587, host: 'smtp.qq.com', secure: true, auth: { pass: mailConfig.pass, user: mailConfig.user } })

http.createServer((req, res) => { const { pathname } = url.parse(req.url) if (req.method === 'POST' && pathname == '/send/mail') { let mailInfo = '' req.on('data', (chunk) => { mailInfo += chunk.toString() }) req.on('end', () => { const body = JSON.parse(mailInfo) transPort.sendMail({ to: body.to, from: mailConfig.user, subject: body.subject, text: body.text }) res.end('ok') }) } }).listen(3000)

文件配置

值得注意的yaml文件配置一定需要空格的

yaml
user: 123456789@qq.com
pass: 123456
关注公众号