Compare commits
No commits in common. "master" and "v1" have entirely different histories.
|
@ -7,6 +7,3 @@
|
|||
._*
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
package-lock.json
|
||||
node_modules
|
41
Readme.md
41
Readme.md
|
@ -1,45 +1,12 @@
|
|||
![module info](https://nodei.co/npm/@gm5/controller.png?downloads=true&downloadRank=true&stars=true)
|
||||
|
||||
![downloads](https://img.shields.io/npm/dt/@gm5/mail.svg)
|
||||
![version](https://img.shields.io/npm/v/@gm5/mail.svg)
|
||||
# @gm5/controller
|
||||
|
||||
# @gm5/mail
|
||||
|
||||
> 一个简单的邮件发送模块, 可用于发送系统邮件, 如注册验证等。
|
||||
> 控制器基类。
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
npm i @gm5/mail
|
||||
npm install @gm5/controller
|
||||
```
|
||||
|
||||
## 使用
|
||||
|
||||
```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>'
|
||||
})
|
||||
|
||||
```
|
76
index.js
76
index.js
|
@ -4,72 +4,42 @@
|
|||
* @date 2020/09/18 15:59:31
|
||||
*/
|
||||
|
||||
import nodemailer from 'nodemailer'
|
||||
import mailx from 'mailx'
|
||||
|
||||
const DEFAULT_CONFIG = {
|
||||
host: 'smtp.example.com',
|
||||
port: 465,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: 'no-reply@example.com',
|
||||
username: 'no-reply',
|
||||
pass: ''
|
||||
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.')
|
||||
}
|
||||
}
|
||||
|
||||
export class Mailer {
|
||||
#smtp = null
|
||||
|
||||
#from = ''
|
||||
#to = ''
|
||||
|
||||
constructor(opts) {
|
||||
this.#smtp = nodemailer.createTransport(opts)
|
||||
this.#from = `${opts.auth.username || 'no named'}"<${opts.auth.user}>"`
|
||||
this.smtp = mailx.transport(host, port, mail, passwd)
|
||||
}
|
||||
|
||||
// 发件人
|
||||
from(email, username) {
|
||||
this.#from = `${username || 'no named'}"<${email}>"`
|
||||
from(info) {
|
||||
this.mail = mailx.message()
|
||||
this.mail.setFrom(info.name, info.mail)
|
||||
return this
|
||||
}
|
||||
|
||||
// 收件人
|
||||
to(email, username) {
|
||||
this.#to = `${username || 'no named'}"<${email}>"`
|
||||
to(info) {
|
||||
this.mail.addTo(info.name, info.mail)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
// 发送正文
|
||||
send(
|
||||
mail = {
|
||||
subject: 'example mail',
|
||||
text: 'example mail plain content',
|
||||
html: '<p>example mail html content</p>'
|
||||
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)
|
||||
}
|
||||
) {
|
||||
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)
|
||||
})
|
||||
return defer.promise
|
||||
}
|
||||
}
|
||||
|
|
16
package.json
16
package.json
|
@ -1,19 +1,11 @@
|
|||
{
|
||||
"name": "@gm5/mail",
|
||||
"version": "2.0.0",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"description": "邮件收发管理",
|
||||
"main": "index.js",
|
||||
"author": "yutent",
|
||||
"keywords": [
|
||||
"fivejs",
|
||||
"gm5",
|
||||
"mail",
|
||||
"sendmail"
|
||||
],
|
||||
"repository": "https://git.wkit.fun/gm5/mail.git",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"nodemailer": "^6.9.7"
|
||||
}
|
||||
"keywords": ["fivejs", "controller", "http"],
|
||||
"repository": "https://github.com/bytedo/gmf.mail.git",
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue