Compare commits

...

2 Commits
v1 ... master

Author SHA1 Message Date
yutent dd5a1cec86 更新readme 2023-11-02 10:43:34 +08:00
yutent b207971b6e 重构mail模块, 更换依赖为nodemailer 2023-11-02 10:37:51 +08:00
4 changed files with 107 additions and 33 deletions

3
.gitignore vendored
View File

@ -7,3 +7,6 @@
._* ._*
.idea .idea
.vscode .vscode
package-lock.json
node_modules

View File

@ -1,12 +1,45 @@
![module info](https://nodei.co/npm/@gm5/controller.png?downloads=true&downloadRank=true&stars=true)
# @gm5/controller ![downloads](https://img.shields.io/npm/dt/@gm5/mail.svg)
![version](https://img.shields.io/npm/v/@gm5/mail.svg)
> 控制器基类。 # @gm5/mail
> 一个简单的邮件发送模块, 可用于发送系统邮件, 如注册验证等。
## 安装 ## 安装
```bash ```bash
npm install @gm5/controller npm i @gm5/mail
``` ```
## 使用
```js
import { createApp } from '@gm5/code'
import { MailModule } from '@gm5/mail'
var app = createApp()
app.install(MailModule, {
auth: {
user: 'example@example.com',
username: 'example',
pass: '123456'
}
})
.run()
// in controller
this.context.$$mail
.to('tom@xx.com', 'tom')
.send({
subject: 'hello',
text: 'how are you?',
html: '<p>how are you?</p>'
})
```

View File

@ -4,42 +4,72 @@
* @date 2020/09/18 15:59:31 * @date 2020/09/18 15:59:31
*/ */
import mailx from 'mailx' import nodemailer from 'nodemailer'
export default class Sendmail { const DEFAULT_CONFIG = {
constructor({ host, port, mail, passwd }) { host: 'smtp.example.com',
if (!host || !port || !mail || !passwd) { port: 465,
throw new Error('smtp options [host, port, mail, passwd] is required.') secure: true,
auth: {
user: 'no-reply@example.com',
username: 'no-reply',
pass: ''
} }
this.smtp = mailx.transport(host, port, mail, passwd) }
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) { from(email, username) {
this.mail = mailx.message() this.#from = `${username || 'no named'}"<${email}>"`
this.mail.setFrom(info.name, info.mail)
return this return this
} }
// 收件人 // 收件人
to(info) { to(email, username) {
this.mail.addTo(info.name, info.mail) this.#to = `${username || 'no named'}"<${email}>"`
return this return this
} }
// 发送正文 // 发送正文
send(mail) { send(
this.mail.setSubject(mail.subject) mail = {
this.mail.setHtml(mail.content) subject: 'example mail',
var defer = Promise.defer() text: 'example mail plain content',
this.smtp.send(this.mail, function(err, res) { html: '<p>example mail html content</p>'
if (err) {
defer.reject(err)
} else {
defer.resolve(res)
} }
}) ) {
return defer.promise 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)
} }
} }

View File

@ -1,11 +1,19 @@
{ {
"name": "@gm5/mail", "name": "@gm5/mail",
"version": "1.0.0", "version": "2.0.0",
"type": "module", "type": "module",
"description": "邮件收发管理", "description": "邮件收发管理",
"main": "index.js", "main": "index.js",
"author": "yutent", "author": "yutent",
"keywords": ["fivejs", "controller", "http"], "keywords": [
"repository": "https://github.com/bytedo/gmf.mail.git", "fivejs",
"license": "MIT" "gm5",
"mail",
"sendmail"
],
"repository": "https://git.wkit.fun/gm5/mail.git",
"license": "MIT",
"dependencies": {
"nodemailer": "^6.9.7"
}
} }