新增smtp发送邮件接口

pull/30/head
taoshihan1991 5 years ago
parent 32381d92c8
commit 1ad43c45bd

@ -7,6 +7,8 @@ require github.com/emersion/go-imap v1.0.4
require (
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/emersion/go-message v0.11.2
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
github.com/emersion/go-smtp v0.13.0
github.com/gobuffalo/packr v1.30.1
golang.org/x/net v0.0.0-20200513185701-a91f0712d120
golang.org/x/text v0.3.2

@ -16,6 +16,10 @@ github.com/emersion/go-message v0.11.2 h1:oxO9SQ+3wgBAQRdk07eqfkCJ26Tl8ZHF7CcpGV
github.com/emersion/go-message v0.11.2/go.mod h1:C4jnca5HOTo4bGN9YdqNQM9sITuT3Y0K6bSUw9RklvY=
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b h1:uhWtEWBHgop1rqEk2klKaxPAkVDCXexai6hSuRQ7Nvs=
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.13.0 h1:aC3Kc21TdfvXnuJXCQXuhnDXUldhc12qME/S7Y3Y94g=
github.com/emersion/go-smtp v0.13.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe h1:40SWqY0zE3qCi6ZrtTf5OUdNm5lDnGnjRSq9GgmeTrg=
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=

@ -162,9 +162,17 @@ func send(w http.ResponseWriter, r *http.Request){
smtpServer:=sendData.Smtp
smtpFrom:=sendData.From
//smtpTo:=sendData["to"].(string)
//smtpBody:=sendData["body"].(string)
msg, _ := json.Marshal(tools.JsonResult{Code: 200, Msg: smtpServer+smtpFrom})
smtpTo:=sendData.To
smtpBody:=sendData.Body
smtpPass:=sendData.Password
smtpSubject:=sendData.Subject
err=tools.Send(smtpServer,smtpFrom,smtpPass,smtpTo,smtpSubject,smtpBody)
if err!=nil{
msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: err.Error()})
w.Write(msg)
return
}
msg, _ := json.Marshal(tools.JsonResult{Code: 200, Msg: "发送成功!"})
w.Write(msg)
}
//邮件夹接口

@ -0,0 +1,22 @@
package tools
import (
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
"strings"
)
func Send(server string,from string,password string,to []string,subject string,body string)error{
auth := sasl.NewPlainClient("", from, password)
msg := strings.NewReader(
"From: "+from+"\r\n"+
"To: "+strings.Join(to,",")+"\r\n"+
"Subject: "+subject+"\r\n" +
"\r\n" +
body+"\r\n")
err := smtp.SendMail(server, auth, from, to, msg)
if err != nil {
return err
}
return nil
}

@ -46,6 +46,8 @@ type JsonFolders struct {
type SmtpBody struct{
Smtp string
From string
To string
To []string
Password string
Subject string
Body string
}

Loading…
Cancel
Save