github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/email/init.go (about)

     1  package email
     2  
     3  import (
     4  	"sync"
     5  
     6  	model "github.com/cloudreve/Cloudreve/v3/models"
     7  	"github.com/cloudreve/Cloudreve/v3/pkg/util"
     8  )
     9  
    10  // Client 默认的邮件发送客户端
    11  var Client Driver
    12  
    13  // Lock 读写锁
    14  var Lock sync.RWMutex
    15  
    16  // Init 初始化
    17  func Init() {
    18  	util.Log().Debug("Initializing email sending queue...")
    19  	Lock.Lock()
    20  	defer Lock.Unlock()
    21  
    22  	if Client != nil {
    23  		Client.Close()
    24  	}
    25  
    26  	// 读取SMTP设置
    27  	options := model.GetSettingByNames(
    28  		"fromName",
    29  		"fromAdress",
    30  		"smtpHost",
    31  		"replyTo",
    32  		"smtpUser",
    33  		"smtpPass",
    34  		"smtpEncryption",
    35  	)
    36  	port := model.GetIntSetting("smtpPort", 25)
    37  	keepAlive := model.GetIntSetting("mail_keepalive", 30)
    38  
    39  	client := NewSMTPClient(SMTPConfig{
    40  		Name:       options["fromName"],
    41  		Address:    options["fromAdress"],
    42  		ReplyTo:    options["replyTo"],
    43  		Host:       options["smtpHost"],
    44  		Port:       port,
    45  		User:       options["smtpUser"],
    46  		Password:   options["smtpPass"],
    47  		Keepalive:  keepAlive,
    48  		Encryption: model.IsTrueVal(options["smtpEncryption"]),
    49  	})
    50  
    51  	Client = client
    52  }