node发送邮件

代码

Mail.mjs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import nodemailer from "nodemailer";

export default async function sendMail(to, title, msg) {
// 创建transporter
const transporter = nodemailer.createTransport({
host: "smtp.qq.com", // 邮箱的smtp地址
auth: {
user: "[email protected]", // 邮箱的smtp地址
pass: process.env.QQMAIL_SMTP_TOKEN // 邮箱授权码
}
});

// 配置邮件信息
const mailInfo = {
from: "[email protected]",
to: to,
subject: title, // Subject line
text: "仓库容量邮件通知", // plain text body
html: msg, // html body
};

// 发送邮件
const ret = await transporter.sendMail(mailInfo);
console.log("send mail finish to " + to);
return ret;
}

测试

Checkrepo.mjs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// yarn add node-fetch -S
// yarn add babel-cli -S

import fetch from 'node-fetch';
import sendMail from "./mail.mjs"

const args = process.argv.slice(2)

console.log(args[1] + " repo size:");

fetch('https://api.github.com/repos/' + args[0] + '/' + args[1])
.then(v => v.json()).then(async (v) => {
const msize = (v['size'] / 1024).toFixed(2)
const gsize = (v['size'] / 1024 / 1024).toFixed(2)
console.log(msize + 'MB');
console.log(gsize + 'GB');

const repoName = args[0] + '/' + args[1]
const to = "[email protected]";
const title = "仓库【" + repoName + "】容量邮件通知邮件✔";
const msg = "您的仓库【" + repoName + "】目前容量大小为请留意!";
console.log("check token is=>" + process.env.QQMAIL_SMTP_TOKEN);

const ret = await sendMail(to, title, msg);
console.log("finish." + ret.response);
})
.catch(err => console.log(err));

运行

1
node checkrepo.mjs terwer upload

效果

image-20220707161013420

图片处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import fetch from 'node-fetch';

export default function handler(req, res) {
// https://img1.terwergreen.com/api/public/20220706194731.png
const imagePath = req.query.slug.join("/");
let host = req.headers.host;
if (host.indexOf("localhost") > -1) {
host = "http://localhost:3000";
} else {
host = "https://img1.terwergreen.com";
}
const fileUrl = `${host}/${imagePath}`;
console.log("fileUrl=>", fileUrl)

fetch(fileUrl).then(async (v) => {
if (v.status == 200) {
const absUrl = '/' + imagePath;
console.log("absUrl=>", absUrl)
res.redirect(307, '/' + imagePath).end()
} else {
// 获取中间代理地址
const newUrl = 'https://ghproxy.com/https://raw.githubusercontent.com/terwer/upload/main/public/' + imagePath
console.log("newUrl=>", newUrl)
res.redirect(307, newUrl).end()
}
}).catch(err => {
console.log(err)
res.end("500")
});
}

测试图片

未同步的时候

https://img1.terwergreen.com/api/public/20220707191555.png

已经同步的

https://img1.terwergreen.com/api/public/20220706194731.png

作者

Terwer

发布于

2022-07-07

更新于

2022-07-07

许可协议

评论