index.js
1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import nodemailer from 'nodemailer'
import { SendEmail } from '../../config'
export default () => {
// console.log('ok')
}
// 发送Email(目前使用的是阿里云SMTP发送邮件)
// receivers 目标邮箱,可以用英文逗号分隔多个。(我没试过)
// subject 邮件标题
// text 文本版本的邮件内容
// html HTML版本的邮件内容
// 返回
// result 200是成功,500是失败
// info 是返回的消息,可能是结果的文本,也可能是对象。(这个错误不要暴露给用户)
export let sendemail = (receivers, subject, text, html) => {
return new Promise(function (resolve) {
let transporter = nodemailer.createTransport('smtp://' + SendEmail.username + ':' + SendEmail.password + '@' + SendEmail.service)
// setup e-mail data with unicode symbols
let mailOptions = {
from: SendEmail.sender_address, // sender address
to: receivers,
subject: subject,
text: text || 'Hello world 🐴', // plaintext body
html: html || '<b>Hello world 🐴</b>' // html body
}
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
resolve({
result: 500,
info: error
})
} else {
resolve({
result: 200,
info: info.response
})
}
})
})
}