add send email api for plugin

This commit is contained in:
xiaoyaofenfen 2022-12-07 10:34:46 +08:00
parent ecacda2596
commit 6ade77c4bf
3 changed files with 66 additions and 24 deletions

View File

@ -50,6 +50,7 @@
+ [1.禁止特定文件上传](#1.禁止特定文件上传)
+ [2.上传文件后删除本地文件](#2.上传文件后删除本地文件)
+ [3.下载文件并截断过长的文件名](#3.下载文件并截断过长的文件名)
+ [4.上传文件去掉文件名包含的部分字符](#4.上传文件去掉文件名包含的部分字符)
* [显示和修改程序配置项](#显示和修改程序配置项)
- [常见问题Q&A](#常见问题Q&A)
* [1. 如何获取RefreshToken](#1-如何获取RefreshToken)
@ -1113,6 +1114,47 @@ console.log("hello world");
PluginUtil.LocalFS.deleteFile(params["localFilePath"]);
```
5.PluginUtil.Email.sendTextMail()
发送文本邮件,定义如下
```
PluginUtil.Email.sendTextMail(mailServer, userName, password, to, subject, body)
其中:
mailServer - smtp服务器+端口
userName - 发件人邮箱地址
password - 发件人邮箱密码
to - 收件人邮箱地址
subject - 邮件标题
body - 邮件内容,纯文本
```
样例
```js
PluginUtil.Email.sendTextMail("smtp.qq.com:465", "123xxx@qq.com", "pwdxxxxxx", "12545xxx@qq.com", "文件上传通知", "该文件已经上传完毕");
```
6.PluginUtil.Email.sendHtmlMail()
发送HTML富文本邮件定义如下
```
PluginUtil.Email.sendHtmlMail(mailServer, userName, password, to, subject, body)
其中:
mailServer - smtp服务器+端口
userName - 发件人邮箱地址
password - 发件人邮箱密码
to - 收件人邮箱地址
subject - 邮件标题
body - 邮件内容HTML富文本
```
样例
```js
var html = "<html>"
+"<body>"
+"<h1>文件上传通知</h1>"
+"<h2>该文件已经上传完毕</h2>"
+"</body>"
+"</html>";
PluginUtil.Email.sendHtmlMail("smtp.qq.com:465", "123xxx@qq.com", "pwdxxxxxx", "12545xxx@qq.com", "文件上传通知", html);
```
### 常见场景样例
这里收集了一些常见的需求样例,可以作为插件定制的样例模板。

View File

@ -55,6 +55,12 @@ func (js *JsPlugin) Start() error {
pluginObj.Set("LocalFS", localFS)
localFS.Set("deleteFile", DeleteLocalFile) // PluginUtil.LocalFS.deleteFile()
// PluginUtil.Email
emailObj := js.vm.NewObject()
pluginObj.Set("Email", emailObj)
emailObj.Set("sendTextMail", SendTextMail) // PluginUtil.Email.sendTextMail()
emailObj.Set("sendHtmlMail", SendHtmlMail) // PluginUtil.Email.sendHtmlMail()
return nil
}

View File

@ -45,6 +45,24 @@ func DeleteLocalFile(localFilePath string) bool {
return false
}
// SendTextMail 发送文本邮件
func SendTextMail(mailServer, userName, password, to, subject, body string) bool {
if e := sendEmail(mailServer, userName, password, to, subject, body, "text", true); e != nil {
logger.Verboseln("js SendTextMail error ", e)
return false
}
return true
}
// SendHtmlMail 发送HTML富文本邮件
func SendHtmlMail(mailServer, userName, password, to, subject, body string) bool {
if e := sendEmail(mailServer, userName, password, to, subject, body, "html", true); e != nil {
logger.Verboseln("js SendHtmlMail error ", e)
return false
}
return true
}
func sendEmail(mailServer, userName, password, to, subject, body, mailType string, useSsl bool) error {
mailServerHost := strings.Split(mailServer, ":")[0]
auth := smtp.PlainAuth("", userName, password, mailServerHost)
@ -64,28 +82,4 @@ func sendEmail(mailServer, userName, password, to, subject, body, mailType strin
} else {
return e.Send(mailServer, auth)
}
//
//// 拼接消息体
//var contentType string
//if mailType == "html" {
// contentType = "Content-Type: text/" + mailType + "; charset=UTF-8"
//} else {
// contentType = "Content-Type: text/plain" + "; charset=UTF-8"
//}
//msg := []byte("To: " + to + "\r\nFrom: " + userName + "\r\nSubject: " + subject + "\r\n" + contentType + "\r\n\r\n" + body)
//
//// msg 内容输出查看
//logger.Verboseln("To: " + to + "\r\n" +
// "From: " + userName + "\r\n" +
// "Subject: " + subject + "\r\n" +
// "" + contentType + "\r\n\r\n" +
// "" + body)
//
//// 进行身份认证
//hp := strings.Split(mailServer, ":")
//auth := smtp.PlainAuth("", userName, password, hp[0])
//
//sendTo := strings.Split(to, ";")
//err := smtp.SendMail(mailServer, auth, userName, sendTo, msg)
//return err
}