From b207971b6e0473a66ae6a8619f68b6a86fdd4d19 Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 2 Nov 2023 10:37:51 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84mail=E6=A8=A1=E5=9D=97,=20?= =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E4=BE=9D=E8=B5=96=E4=B8=BAnodemailer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ index.js | 80 ++++++++++++++++++++++++++++++++++++---------------- package.json | 16 ++++++++--- 3 files changed, 70 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 7c24ca8..42318c5 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ ._* .idea .vscode + +package-lock.json +node_modules \ No newline at end of file diff --git a/index.js b/index.js index de4f69a..1df4ab7 100644 --- a/index.js +++ b/index.js @@ -4,42 +4,72 @@ * @date 2020/09/18 15:59:31 */ -import mailx from 'mailx' +import nodemailer from 'nodemailer' -export default class Sendmail { - constructor({ host, port, mail, passwd }) { - if (!host || !port || !mail || !passwd) { - throw new Error('smtp options [host, port, mail, passwd] is required.') - } - this.smtp = mailx.transport(host, port, mail, passwd) +const DEFAULT_CONFIG = { + host: 'smtp.example.com', + port: 465, + secure: true, + auth: { + user: 'no-reply@example.com', + username: 'no-reply', + pass: '' + } +} + +export class Mailer { + #smtp = null + + #from = '' + #to = '' + + constructor(opts) { + this.#smtp = nodemailer.createTransport(opts) + this.#from = `${opts.auth.username || 'no named'}"<${opts.auth.user}>"` } // 发件人 - from(info) { - this.mail = mailx.message() - this.mail.setFrom(info.name, info.mail) + from(email, username) { + this.#from = `${username || 'no named'}"<${email}>"` return this } // 收件人 - to(info) { - this.mail.addTo(info.name, info.mail) - + to(email, username) { + this.#to = `${username || 'no named'}"<${email}>"` return this } // 发送正文 - send(mail) { - this.mail.setSubject(mail.subject) - this.mail.setHtml(mail.content) - var defer = Promise.defer() - this.smtp.send(this.mail, function(err, res) { - if (err) { - defer.reject(err) - } else { - defer.resolve(res) - } - }) - return defer.promise + send( + mail = { + subject: 'example mail', + text: 'example mail plain content', + html: '

example mail html content

' + } + ) { + if (!this.#to) { + throw new Error('the email address send to is empty!') + } + + mail.from = this.#from + mail.to = this.#to + + this.#to = '' + + return this.#smtp.sendMail(mail) + } +} + +export const MailModule = { + name: 'mail', + install(conf = {}) { + if (!conf.auth) { + throw new Error('mail auth account must not be empty.') + } + + let smtp = Object.assign({}, DEFAULT_CONFIG, conf) + + return new Mailer(smtp) } } diff --git a/package.json b/package.json index 7bc214e..1d0d0e0 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,19 @@ { "name": "@gm5/mail", - "version": "1.0.0", + "version": "2.0.0", "type": "module", "description": "邮件收发管理", "main": "index.js", "author": "yutent", - "keywords": ["fivejs", "controller", "http"], - "repository": "https://github.com/bytedo/gmf.mail.git", - "license": "MIT" + "keywords": [ + "fivejs", + "gm5", + "mail", + "sendmail" + ], + "repository": "https://git.wkit.fun/gm5/mail.git", + "license": "MIT", + "dependencies": { + "nodemailer": "^6.9.7" + } }