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