mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-21 11:50:01 +00:00
fix: resolve email notification delivery failure
This commit is contained in:
parent
28992f178e
commit
18e1c02d1c
@ -2,57 +2,55 @@ package notify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/smtp"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
"strconv"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/mailer"
|
||||
)
|
||||
|
||||
const defaultSmtpHostPort = "25"
|
||||
|
||||
type Mail struct {
|
||||
senderAddress string
|
||||
smtpHostAddr string
|
||||
smtpHostPort string
|
||||
smtpAuth smtp.Auth
|
||||
receiverAddresses string
|
||||
username string
|
||||
to string
|
||||
client *mailer.SmtpClient
|
||||
}
|
||||
|
||||
func NewMail(senderAddress, receiverAddresses, smtpHostAddr, smtpHostPort string) *Mail {
|
||||
if(smtpHostPort == "") {
|
||||
smtpHostPort = "25"
|
||||
func NewMail(senderAddress, receiverAddresses, smtpHostAddr, smtpHostPort, password string) (*Mail, error) {
|
||||
if smtpHostPort == "" {
|
||||
smtpHostPort = defaultSmtpHostPort
|
||||
}
|
||||
|
||||
port, err := strconv.Atoi(smtpHostPort)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid smtp port: %w", err)
|
||||
}
|
||||
|
||||
client := mailer.SmtpClient{
|
||||
Host: smtpHostAddr,
|
||||
Port: port,
|
||||
Username: senderAddress,
|
||||
Password: password,
|
||||
Tls: true,
|
||||
}
|
||||
|
||||
return &Mail{
|
||||
senderAddress: senderAddress,
|
||||
smtpHostAddr: smtpHostAddr,
|
||||
smtpHostPort: smtpHostPort,
|
||||
receiverAddresses: receiverAddresses,
|
||||
}
|
||||
username: senderAddress,
|
||||
client: &client,
|
||||
to: receiverAddresses,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *Mail) SetAuth(username, password string) {
|
||||
m.smtpAuth = smtp.PlainAuth("", username, password, m.smtpHostAddr)
|
||||
}
|
||||
|
||||
func (m *Mail) Send(ctx context.Context, subject, message string) error {
|
||||
// 构建邮件
|
||||
from := m.senderAddress
|
||||
to := []string{m.receiverAddresses}
|
||||
msg := []byte(
|
||||
"From: " + from + "\r\n" +
|
||||
"To: " + m.receiverAddresses + "\r\n" +
|
||||
"Subject: " + subject + "\r\n" +
|
||||
"\r\n" +
|
||||
message + "\r\n")
|
||||
|
||||
var smtpAddress string
|
||||
// 组装邮箱服务器地址
|
||||
if(m.smtpHostPort == "25"){
|
||||
smtpAddress = m.smtpHostAddr
|
||||
}else{
|
||||
smtpAddress = m.smtpHostAddr + ":" + m.smtpHostPort
|
||||
func (m *Mail) Send(ctx context.Context, subject, content string) error {
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Address: m.username,
|
||||
},
|
||||
To: []mail.Address{{Address: m.to}},
|
||||
Subject: subject,
|
||||
Text: content,
|
||||
}
|
||||
|
||||
err := smtp.SendMail(smtpAddress, m.smtpAuth, from, to, msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return m.client.Send(message)
|
||||
}
|
@ -108,7 +108,7 @@ func getNotifier(channel string, conf map[string]any) (notifyPackage.Notifier, e
|
||||
case domain.NotifyChannelServerChan:
|
||||
return getServerChanNotifier(conf), nil
|
||||
case domain.NotifyChannelMail:
|
||||
return getMailNotifier(conf), nil
|
||||
return getMailNotifier(conf)
|
||||
case domain.NotifyChannelBark:
|
||||
return getBarkNotifier(conf), nil
|
||||
}
|
||||
@ -180,12 +180,18 @@ func getLarkNotifier(conf map[string]any) notifyPackage.Notifier {
|
||||
return lark.NewWebhookService(getString(conf, "webhookUrl"))
|
||||
}
|
||||
|
||||
func getMailNotifier(conf map[string]any) notifyPackage.Notifier {
|
||||
rs := NewMail(getString(conf, "senderAddress"), getString(conf, "receiverAddress"), getString(conf, "smtpHostAddr"), getString(conf, "smtpHostPort"))
|
||||
func getMailNotifier(conf map[string]any) (notifyPackage.Notifier, error) {
|
||||
rs, err := NewMail(getString(conf, "senderAddress"),
|
||||
getString(conf, "receiverAddresses"),
|
||||
getString(conf, "smtpHostAddr"),
|
||||
getString(conf, "smtpHostPort"),
|
||||
getString(conf, "password"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rs.SetAuth(getString(conf, "username"), getString(conf, "password"))
|
||||
|
||||
return rs
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
func getString(conf map[string]any, key string) string {
|
||||
|
Loading…
x
Reference in New Issue
Block a user