High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Fix bounce POP scanner incorrectly returning errors while scanning. Closes #2884.
Kailash Nadhdce582a51c2b
diff --git a/internal/bounce/bounce.go b/internal/bounce/bounce.goindex a990f6e0..7400d247 100644--- a/internal/bounce/bounce.go+++ b/internal/bounce/bounce.go@@ -71,7 +71,7 @@ func New(opt Opt, q *Queries, lo *log.Logger) (*Manager, error) {if opt.MailboxEnabled {switch opt.MailboxType {case "pop":- m.mailbox = mailbox.NewPOP(opt.Mailbox)+ m.mailbox = mailbox.NewPOP(opt.Mailbox, lo)default:return nil, errors.New("unknown bounce mailbox type")}diff --git a/internal/bounce/mailbox/pop.go b/internal/bounce/mailbox/pop.goindex 85b4c1a3..c1715093 100644--- a/internal/bounce/mailbox/pop.go+++ b/internal/bounce/mailbox/pop.go@@ -4,6 +4,7 @@ import ("encoding/json""fmt""io"+ "log""regexp""strings""time"@@ -18,6 +19,7 @@ import (type POP struct {opt Optclient *pop3.Client+ lo *log.Logger}type bounceHeaders struct {@@ -58,7 +60,7 @@ var ()// NewPOP returns a new instance of the POP mailbox client.-func NewPOP(opt Opt) *POP {+func NewPOP(opt Opt, lo *log.Logger) *POP {return &POP{opt: opt,client: pop3.New(pop3.Opt{@@ -67,41 +69,10 @@ func NewPOP(opt Opt) *POP {TLSEnabled: opt.TLSEnabled,TLSSkipVerify: opt.TLSSkipVerify,}),+ lo: lo,}}-// classifyBounce analyzes the bounce message content and determines if it's a hard or soft bounce.-// It checks SMTP status codes, diagnostic headers, and bounce keywords (using string heuristics).-// soft is the default preference.-// Returns the bounce type and a classification reason containing context about what matched.-func classifyBounce(b []byte) (string, string) {- if matches := reSMTPStatus.FindAllSubmatch(b, -1); matches != nil {- for _, m := range matches {- if len(m) >= 2 && len(m[0]) > 1 {- // Full status code (e.g., "5.1.1").- status := m[1]-- // 5.x.x is hard bounce.- if status[0] == '5' {- return models.BounceTypeHard, fmt.Sprintf("smtp_status=%s", status)- }-- // 4.x.x is soft bounce.- if status[0] == '4' {- return models.BounceTypeSoft, fmt.Sprintf("smtp_status=%s", status)- }- }- }- }-- // Check for explicit hard bounce keywords.- if match := reHardBounce.FindSubmatch(b); match != nil {- return models.BounceTypeHard, fmt.Sprintf("body_match=%s", match[1])- }-- return models.BounceTypeSoft, "default"-}-// Scan scans the mailbox and pushes the downloaded messages into the given channel.// The messages that are downloaded are deleted from the server. If limit > 0,// all messages on the server are downloaded and deleted.@@ -139,13 +110,15 @@ func (p *POP) Scan(limit int, ch chan models.Bounce) error {// Retrieve the raw bytes of the message.b, err := c.RetrRaw(id)if err != nil {- return err+ p.lo.Printf("error retrieving bounce message %d: %v", id, err)+ continue}// Parse the message.m, err := message.Read(b)if err != nil {- return err+ p.lo.Printf("error parsing bounce message %d: %v", id, err)+ continue}h := m@@ -157,7 +130,8 @@ func (p *POP) Scan(limit int, ch chan models.Bounce) error {if err == io.EOF {break} else if err != nil {- return err+ p.lo.Printf("error reading multipart bounce message %d: %v", id, err)+ continue}h = part}@@ -186,7 +160,7 @@ func (p *POP) Scan(limit int, ch chan models.Bounce) error {msgReceived := h.Header.Map()[models.EmailHeaderReceived]if len(msgReceived) == 0 {if u := reHdrReceived.FindAllSubmatch(b.Bytes(), -1); u != nil {- for i := 0; i < len(u); i++ {+ for i := range u {msgReceived = append(msgReceived, string(u[i][1]))}}@@ -232,3 +206,35 @@ func (p *POP) Scan(limit int, ch chan models.Bounce) error {return nil}++// classifyBounce analyzes the bounce message content and determines if it's a hard or soft bounce.+// It checks SMTP status codes, diagnostic headers, and bounce keywords (using string heuristics).+// soft is the default preference.+// Returns the bounce type and a classification reason containing context about what matched.+func classifyBounce(b []byte) (string, string) {+ if matches := reSMTPStatus.FindAllSubmatch(b, -1); matches != nil {+ for _, m := range matches {+ if len(m) >= 2 && len(m[0]) > 1 {+ // Full status code (e.g., "5.1.1").+ status := m[1]++ // 5.x.x is hard bounce.+ if status[0] == '5' {+ return models.BounceTypeHard, fmt.Sprintf("smtp_status=%s", status)+ }++ // 4.x.x is soft bounce.+ if status[0] == '4' {+ return models.BounceTypeSoft, fmt.Sprintf("smtp_status=%s", status)+ }+ }+ }+ }++ // Check for explicit hard bounce keywords.+ if match := reHardBounce.FindSubmatch(b); match != nil {+ return models.BounceTypeHard, fmt.Sprintf("body_match=%s", match[1])+ }++ return models.BounceTypeSoft, "default"+}