From 5fc99961f3d77c7837ce299c77106fb92c6b7d58 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Sat, 27 Feb 2021 18:20:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20email=20=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 ++ src/push/email.ts | 32 ++++++++++++++++++++++++++++++++ src/push/server-chan-turbo.ts | 3 +++ src/push/server-chan.ts | 3 +++ 4 files changed, 40 insertions(+) create mode 100644 src/push/email.ts diff --git a/package.json b/package.json index 7e98a3e..58c55f3 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "@types/lodash": "^4.14.168", "@types/module-alias": "^2.0.0", "@types/node": "^14.14.31", + "@types/nodemailer": "^6.4.0", "@types/qs": "^6.9.5", "@typescript-eslint/eslint-plugin": "^4.15.2", "@typescript-eslint/parser": "^4.15.2", @@ -73,6 +74,7 @@ "colors": "^1.4.0", "crypto-js": "^4.0.0", "debug": "^4.3.1", + "nodemailer": "^6.5.0", "qs": "^6.9.6" }, "config": { diff --git a/src/push/email.ts b/src/push/email.ts new file mode 100644 index 0000000..13c668c --- /dev/null +++ b/src/push/email.ts @@ -0,0 +1,32 @@ +import { Send } from '../interfaces/send' +import { createTransport, SendMailOptions } from 'nodemailer' +import debug from 'debug' + +const Debugger = debug('push:email') + +type Mail = ReturnType +type Args = Parameters + +export class Email implements Send { + + private mail: Mail + + constructor(...args: Args) { + this.mail = createTransport(...args) + } + + close(): void { + this.mail.close() + } + + async send(mailOptions: SendMailOptions): Promise { + return new Promise((resolve: (value: unknown) => void, reject: (err: Error) => void) => { + this.mail.sendMail(mailOptions, (err, info) => { + if (err) { + return reject(err) + } + return resolve(info) + }) + }) + } +} \ No newline at end of file diff --git a/src/push/server-chan-turbo.ts b/src/push/server-chan-turbo.ts index 1085eb9..1e0d1ed 100644 --- a/src/push/server-chan-turbo.ts +++ b/src/push/server-chan-turbo.ts @@ -24,6 +24,9 @@ export class ServerChanTurbo implements Send { constructor(SCTKEY: string) { this.SCTKEY = SCTKEY Debugger('set SCTKEY: "%s"', SCTKEY) + if (!this.SCTKEY) { + throw new Error('SCTKEY is required!') + } } /** * diff --git a/src/push/server-chan.ts b/src/push/server-chan.ts index e16e5f5..90e9a6f 100644 --- a/src/push/server-chan.ts +++ b/src/push/server-chan.ts @@ -21,6 +21,9 @@ export class ServerChan implements Send { */ constructor(SCKEY: string) { this.SCKEY = SCKEY + if (!this.SCKEY) { + throw new Error('SCKEY is required!') + } } /** * 请前往 https://sc.ftqq.com 领取