From 885592d774d15cd734402acbe6ed06b2d53761c7 Mon Sep 17 00:00:00 2001 From: Kininaru Date: Sun, 9 May 2021 11:38:48 +0800 Subject: [PATCH 1/2] feat: add email sender Signed-off-by: Kininaru update go mod sum Signed-off-by: Kininaru --- conf/app.conf | 6 +++++- go.mod | 1 + go.sum | 2 ++ object/email.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 object/email.go diff --git a/conf/app.conf b/conf/app.conf index 17867f68..61acef76 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -8,4 +8,8 @@ dataSourceName = root:123@tcp(localhost:3306)/ dbName = casdoor AuthState = "casdoor" UseProxy = false -EnableDocs = true \ No newline at end of file +EnableDocs = true +mailUser = "" +mailPass = "" +mailHost = "" +mailPort = "" \ No newline at end of file diff --git a/go.mod b/go.mod index 73f0c843..e5e4f5f6 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 782834a4..26a023ea 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/object/email.go b/object/email.go new file mode 100644 index 00000000..f0099dee --- /dev/null +++ b/object/email.go @@ -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) +} From 5d1133dac9a1f94b9fc38977884c6ad2d2143934 Mon Sep 17 00:00:00 2001 From: Kininaru Date: Sun, 9 May 2021 11:42:22 +0800 Subject: [PATCH 2/2] fix: add owner to avatar url Signed-off-by: Kininaru --- controllers/account.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/account.go b/controllers/account.go index 2ecaafb0..93c54306 100644 --- a/controllers/account.go +++ b/controllers/account.go @@ -190,7 +190,7 @@ func (c *ApiController) UploadAvatar() { } dist, _ := base64.StdEncoding.DecodeString(avatarBase64[index+1:]) - msg := object.UploadAvatar(user.Name, dist) + msg := object.UploadAvatar(user.Owner + "/" + user.Name, dist) if msg != "" { resp = Response{Status: "error", Msg: msg} c.Data["json"] = resp