add: email module

This commit is contained in:
骑着蜗牛追导弹 2023-02-02 17:59:03 +08:00
parent 6466292524
commit a40ab2d218
8 changed files with 108 additions and 3 deletions

3
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/druidcaesa/gotool v0.0.0-20210722000450-b972ac6f3771
github.com/gin-gonic/gin v1.7.3
github.com/go-basic/uuid v1.0.0
github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df
github.com/go-sql-driver/mysql v1.6.0
github.com/go-xorm/xorm v0.7.9
github.com/gomodule/redigo v1.8.5
@ -21,5 +22,7 @@ require (
github.com/xuri/excelize/v2 v2.4.1
github.com/yinheli/mahonia v0.0.0-20131226213531-0eef680515cc // indirect
github.com/yinheli/qqwry v0.0.0-20160229183603-f50680010f4a
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
gopkg.in/yaml.v2 v2.2.8
)

6
go.sum
View File

@ -33,6 +33,8 @@ github.com/gin-gonic/gin v1.7.3 h1:aMBzLJ/GMEYmv1UWs2FFTcPISLrQH2mRgL9Glz8xows=
github.com/gin-gonic/gin v1.7.3/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/go-basic/uuid v1.0.0 h1:Faqtetcr8uwOzR2qp8RSpkahQiv4+BnJhrpuXPOo63M=
github.com/go-basic/uuid v1.0.0/go.mod h1:yVtVnsXcmaLc9F4Zw7hTV7R0+vtuQw00mdXi+F6tqco=
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-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
@ -242,11 +244,15 @@ google.golang.org/genproto v0.0.0-20190404172233-64821d5d2107/go.mod h1:VzzqZJRn
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

20
main.go
View File

@ -2,6 +2,7 @@ package main
// init函数执行顺序自上而下, 最后执行main包里面的init函数
import (
"cutego/modules/core/dataobject"
"cutego/pkg/config"
_ "cutego/pkg/cronjob"
_ "cutego/pkg/gin"
@ -10,10 +11,29 @@ import (
"cutego/refs"
"fmt"
"github.com/gin-gonic/gin"
"time"
)
func main() {
//go testChangeJob()
//starter()
user := dataobject.SysUser{}
user.UserId = 1
user.UserName = "test"
user.Password = "123456"
user.LoginDate = time.Now()
jsonString := util.ToJSONString(user)
fmt.Println(jsonString)
fmt.Println(util.FormatDateTime(user.LoginDate))
fmt.Println(util.FormatDate(user.LoginDate))
fmt.Println(util.FormatTime(user.LoginDate))
sysUser := dataobject.SysUser{}
util.ParseJSONStruct(jsonString, &sysUser)
fmt.Println(sysUser)
}
func starter() {
gin.SetMode(util.IF(config.AppEnvConfig.Server.RunMode == "", "debug", config.AppEnvConfig.Server.RunMode).(string))
err := refs.CoolGin.Run(fmt.Sprintf(":%d", config.AppEnvConfig.Server.Port))
if err != nil {

36
pkg/util/mail_util.go Normal file
View File

@ -0,0 +1,36 @@
package util
import "cutego/pkg/config"
import "github.com/go-gomail/gomail"
// BatchSendCode 批量发送验证码
// Param: targetUserEmails 接收者邮箱地址
// Param: code 验证码
func BatchSendCode(targetUserEmails []string, code string) error {
mailConfig := config.AppCoreConfig.CuteGoConfig.Mail
sender := mailConfig.Username
authCode := mailConfig.Password
mailTitle := "CuteGo验证码"
mailBody := "您的验证码为: " + code
message := gomail.NewMessage()
message.SetHeader("From", sender)
message.SetHeader("To", targetUserEmails...)
message.SetHeader("Subject", mailTitle)
message.SetBody("text/html", mailBody)
// 添加附件
//zipPath := "./xxxx.zip"
//message.Attach(zipPath)
dialer := gomail.NewDialer(mailConfig.Host, mailConfig.Port, sender, authCode)
return dialer.DialAndSend(message)
}
// SendCode 发送验证码
// Param: targetUserEmail 接收者邮箱地址
// Param: code 验证码
func SendCode(targetUserEmail string, code string) error {
targetUserEmails := []string{targetUserEmail}
return BatchSendCode(targetUserEmails, code)
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strconv"
"time"
)
// IF 三元表达式
@ -78,3 +79,35 @@ func IsFileOrDirExist(path string) bool {
}
return true
}
// ToJSONString 转json字符串
// v 对象, 非对象地址
func ToJSONString(v interface{}) string {
output, _ := json.Marshal(&v)
return string(output)
}
// ParseJSONStruct 解析json字符串
// jsonStr json字符串
// v 对象, 非对象地址
func ParseJSONStruct(jsonStr string, v interface{}) {
err := json.Unmarshal([]byte(jsonStr), &v)
if err != nil {
panic("JSON解析失败, " + err.Error())
}
}
// FormatDateTime 格式化日期时间
func FormatDateTime(date time.Time) string {
return date.Format("2006-01-02 15:04:05")
}
// FormatDate 格式化日期
func FormatDate(date time.Time) string {
return date.Format("2006-01-02")
}
// FormatTime 格式化时间
func FormatTime(date time.Time) string {
return date.Format("15:04:05")
}

View File

@ -15,7 +15,7 @@ func init() {
var err error
// 配置mysql数据库
ds := config.AppEnvConfig.DataSource
jdbc := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
jdbc := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True",
ds.Username,
ds.Password,
ds.Host,
@ -44,5 +44,12 @@ func init() {
SqlDB.ShowSQL(true)
// 开启缓存
SqlDB.SetDefaultCacher(xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000))
// 切换标准时区
location, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic("加载时区异常, " + err.Error())
}
SqlDB.SetTZLocation(location)
SqlDB.SetTZDatabase(location)
logging.InfoLog("database init end...")
}

View File

@ -20,7 +20,7 @@ redis:
database: 1
host: 127.0.0.1
port: 6379
password:
password: 123456
# 连接超时时间
timeout: 5000
pool:

View File

@ -6,7 +6,7 @@ cutego:
#邮件
mail:
host: smtp.qq.com
port: 465
port: 587
username: tianjun@odboy.cn
password: xxxxxxxxxxx
default-encoding: UTF-8