feat: add email sender

Signed-off-by: Kininaru <shiftregister233@outlook.com>

update go mod sum

Signed-off-by: Kininaru <shiftregister233@outlook.com>
This commit is contained in:
Kininaru 2021-05-09 11:38:48 +08:00
parent a1851c2b78
commit 885592d774
4 changed files with 58 additions and 1 deletions

View File

@ -8,4 +8,8 @@ dataSourceName = root:123@tcp(localhost:3306)/
dbName = casdoor
AuthState = "casdoor"
UseProxy = false
EnableDocs = true
EnableDocs = true
mailUser = ""
mailPass = ""
mailHost = ""
mailPort = ""

1
go.mod
View File

@ -11,6 +11,7 @@ require (
github.com/casbin/xorm-adapter/v2 v2.2.0
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df
github.com/go-sql-driver/mysql v1.5.0
github.com/google/uuid v1.2.0
github.com/jinzhu/configor v1.2.1 // indirect

2
go.sum
View File

@ -59,6 +59,8 @@ github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/glendc/gopher-json v0.0.0-20170414221815-dc4743023d0c/go.mod h1:Gja1A+xZ9BoviGJNA2E9vFkPjjsl+CoJxSXiQM1UXtw=
github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df h1:Bao6dhmbTA1KFVxmJ6nBoMuOJit2yjEgLJpIMYpop0E=
github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df/go.mod h1:GJr+FCSXshIwgHBtLglIg9M2l2kQSi6QjVAngtzI08Y=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=

50
object/email.go Normal file
View File

@ -0,0 +1,50 @@
// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// modified from https://github.com/casbin/casnode/blob/master/service/mail.go
package object
import (
"strconv"
"github.com/astaxie/beego"
"github.com/go-gomail/gomail"
)
var dialer *gomail.Dialer
var mailConn = map[string]string{
"user": beego.AppConfig.String("mailUser"),
"pass": beego.AppConfig.String("mailPass"),
"host": beego.AppConfig.String("mailHost"),
"port": beego.AppConfig.String("mailPort"),
}
func InitDialer() {
port, _ := strconv.Atoi(mailConn["port"])
dialer = gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
}
func SendEmail(title, content, dest, sender string) error {
message := gomail.NewMessage()
message.SetAddressHeader("From", beego.AppConfig.String("mailUser"), sender)
message.SetHeader("To", dest)
message.SetHeader("Subject", title)
message.SetBody("text/html", content)
if dialer == nil {
InitDialer()
}
return dialer.DialAndSend(message)
}