From 18e1c02d1ca0b9a43fe580f0ac1bed43a0262540 Mon Sep 17 00:00:00 2001 From: yoan <536464346@qq.com> Date: Sat, 2 Nov 2024 10:17:16 +0800 Subject: [PATCH] fix: resolve email notification delivery failure --- internal/notify/mail.go | 82 +++++++++++++++++++-------------------- internal/notify/notify.go | 18 ++++++--- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/internal/notify/mail.go b/internal/notify/mail.go index ece00ad2..060ffcd6 100644 --- a/internal/notify/mail.go +++ b/internal/notify/mail.go @@ -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 -} \ No newline at end of file + return m.client.Send(message) +} diff --git a/internal/notify/notify.go b/internal/notify/notify.go index a7fdcd8b..f414b6d4 100644 --- a/internal/notify/notify.go +++ b/internal/notify/notify.go @@ -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 {