add: email module
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user