add code for send mail

This commit is contained in:
zhl 2023-03-14 19:36:12 +08:00
parent c504274ca9
commit ef84705b25
6 changed files with 81 additions and 1 deletions

View File

@ -2,3 +2,10 @@ API_PORT=3087
API_HOST=0.0.0.0
API_TOKEN_SECRET=sdf(**&*&xx2214
API_TOKEN_EXPIRESIN=1d
MAIL_SMTP_HOST=
MAIL_SMTP_PORT=
MAIL_SMTP_SECURE=
MAIL_SMTP_USER=
MAIL_SMTP_PASS=

View File

@ -25,6 +25,7 @@
},
"devDependencies": {
"@types/node": "^18.15.3",
"@types/nodemailer": "^6.4.7",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"eslint": "^8.36.0",

View File

@ -0,0 +1,17 @@
import BaseController from 'common/base.controller'
import { ZError } from 'common/ZError'
import { role, router } from 'decorators/router'
import { MailQueue } from 'queue/mail.queue'
class MailController extends BaseController {
@role('anon')
@router('post /mail/send')
async registWebClient(req, res) {
let { data } = req.params
if (!data) {
throw new ZError(10, 'params mismatch')
}
const qrId = new MailQueue().addTaskToQueue(data)
return {}
}
}

21
src/queue/mail.queue.ts Normal file
View File

@ -0,0 +1,21 @@
import { AsyncQueue, createAsyncQueue } from 'common/AsyncQueue'
import { singleton } from 'decorators/singleton'
import { MailService } from 'services/MailService'
@singleton
export class MailQueue {
private queue: AsyncQueue
constructor() {
this.queue = createAsyncQueue()
}
public async addTaskToQueue(data: any) {
this.queue.push(async () => {
try {
await new MailService().send(data)
} catch (err) {
console.log('send mail error.')
}
})
}
}

View File

@ -0,0 +1,27 @@
import { singleton } from 'decorators/singleton'
import { createTransport, Transporter } from 'nodemailer'
import Mail from 'nodemailer/lib/mailer'
@singleton
export class MailService {
private transporter: Transporter
constructor() {
const options = {
host: process.env.MAIL_SMTP_HOST,
port: +process.env.MAIL_SMTP_PORT,
secure: process.env.MAIL_SMTP_SECURE,
auth: {
user: process.env.MAIL_SMTP_USER,
pass: process.env.MAIL_SMTP_PASS,
},
logger: true,
debug: false,
}
// @ts-ignore
this.transporter = createTransport(options, { from: '' })
}
public send(message: Mail.Options) {
return this.transporter.sendMail(message)
}
}

View File

@ -190,11 +190,18 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==
"@types/node@^18.15.3":
"@types/node@*", "@types/node@^18.15.3":
version "18.15.3"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.3.tgz#f0b991c32cfc6a4e7f3399d6cb4b8cf9a0315014"
integrity sha512-p6ua9zBxz5otCmbpb5D3U4B5Nanw6Pk3PPyX05xnxbB/fRv71N7CPmORg7uAD5P70T0xmx1pzAx/FUfa5X+3cw==
"@types/nodemailer@^6.4.7":
version "6.4.7"
resolved "https://registry.yarnpkg.com/@types/nodemailer/-/nodemailer-6.4.7.tgz#658f4bca47c1a895b1d7e054b3b54030a5e1f5e0"
integrity sha512-f5qCBGAn/f0qtRcd4SEn88c8Fp3Swct1731X4ryPKqS61/A3LmmzN8zaEz7hneJvpjFbUUgY7lru/B/7ODTazg==
dependencies:
"@types/node" "*"
"@types/semver@^7.3.12":
version "7.3.13"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.13.tgz#da4bfd73f49bd541d28920ab0e2bf0ee80f71c91"