mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: mail SMTP settings
This commit is contained in:
@@ -1,12 +1,23 @@
|
||||
package email
|
||||
|
||||
import model "github.com/HFO4/cloudreve/models"
|
||||
import (
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Client 默认的邮件发送客户端
|
||||
var Client Driver
|
||||
|
||||
// Lock 读写锁
|
||||
var Lock sync.RWMutex
|
||||
|
||||
// Init 初始化
|
||||
func Init() {
|
||||
util.Log().Debug("邮件队列初始化")
|
||||
Lock.Lock()
|
||||
defer Lock.Unlock()
|
||||
|
||||
if Client != nil {
|
||||
Client.Close()
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ func Send(to, title, body string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
Lock.RLock()
|
||||
defer Lock.RUnlock()
|
||||
|
||||
if Client == nil {
|
||||
return ErrNoActiveDriver
|
||||
}
|
||||
|
||||
@@ -45,7 +45,8 @@ func (client *SMTP) Send(to, title, body string) error {
|
||||
return ErrChanNotOpen
|
||||
}
|
||||
m := mail.NewMessage()
|
||||
m.SetHeader("From", client.Config.Address)
|
||||
m.SetAddressHeader("From", client.Config.Address, client.Config.Name)
|
||||
m.SetAddressHeader("Reply-To", client.Config.ReplyTo, client.Config.Name)
|
||||
m.SetHeader("To", to)
|
||||
m.SetHeader("Subject", title)
|
||||
m.SetBody("text/html", body)
|
||||
@@ -66,8 +67,8 @@ func (client *SMTP) Init() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
client.chOpen = false
|
||||
util.Log().Error("邮件发送队列出现异常, %s ,30 秒后重新连接", err)
|
||||
time.Sleep(time.Duration(30) * time.Second)
|
||||
util.Log().Error("邮件发送队列出现异常, %s ,10 秒后重置", err)
|
||||
time.Sleep(time.Duration(10) * time.Second)
|
||||
client.Init()
|
||||
}
|
||||
}()
|
||||
@@ -83,6 +84,7 @@ func (client *SMTP) Init() {
|
||||
select {
|
||||
case m, ok := <-client.ch:
|
||||
if !ok {
|
||||
util.Log().Debug("邮件队列关闭")
|
||||
client.chOpen = false
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user