mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-18 14:47:03 +00:00
queue: Make DSN tidier, especially in handling multi-line errors
This patch contains some changes to generate tidier DSNs, which should make them slightly more readable. In particular, it also makes it able to handle multi-line errors much better than before.
This commit is contained in:
@@ -3,6 +3,7 @@ package queue
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -84,8 +85,18 @@ type dsnInfo struct {
|
|||||||
Boundary string
|
Boundary string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// indent s with the given number of spaces.
|
||||||
|
func indent(sp int, s string) string {
|
||||||
|
pad := strings.Repeat(" ", sp)
|
||||||
|
return strings.Replace(s, "\n", "\n"+pad, -1)
|
||||||
|
}
|
||||||
|
|
||||||
var dsnTemplate = template.Must(
|
var dsnTemplate = template.Must(
|
||||||
template.New("dsn").Parse(
|
template.New("dsn").Funcs(
|
||||||
|
template.FuncMap{
|
||||||
|
"indent": indent,
|
||||||
|
"trim": strings.TrimSpace,
|
||||||
|
}).Parse(
|
||||||
`From: Mail Delivery System <postmaster-dsn@{{.OurDomain}}>
|
`From: Mail Delivery System <postmaster-dsn@{{.OurDomain}}>
|
||||||
To: <{{.Destination}}>
|
To: <{{.Destination}}>
|
||||||
Subject: Mail delivery failed: returning message to sender
|
Subject: Mail delivery failed: returning message to sender
|
||||||
@@ -108,17 +119,17 @@ Content-Transfer-Encoding: 8bit
|
|||||||
|
|
||||||
Delivery of your message to the following recipient(s) failed permanently:
|
Delivery of your message to the following recipient(s) failed permanently:
|
||||||
|
|
||||||
{{range .FailedTo -}} - {{.}}
|
{{range .FailedTo}} - {{.}}
|
||||||
{{- end}}
|
{{end}}
|
||||||
|
|
||||||
Technical details:
|
Technical details:
|
||||||
{{- range .FailedRecipients}}
|
{{- range .FailedRecipients}}
|
||||||
- "{{.Address}}" ({{.Type}}) failed permanently with error:
|
- "{{.Address}}" ({{.Type}}) failed permanently with error:
|
||||||
{{.LastFailureMessage}}
|
{{.LastFailureMessage | trim | indent 4}}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- range .PendingRecipients}}
|
{{- range .PendingRecipients}}
|
||||||
- "{{.Address}}" ({{.Type}}) failed repeatedly and timed out, last error:
|
- "{{.Address}}" ({{.Type}}) failed repeatedly and timed out, last error:
|
||||||
{{.LastFailureMessage}}
|
{{.LastFailureMessage | trim | indent 4}}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
|
||||||
|
|
||||||
@@ -134,14 +145,16 @@ Original-Recipient: utf-8; {{.OriginalAddress}}
|
|||||||
Final-Recipient: utf-8; {{.Address}}
|
Final-Recipient: utf-8; {{.Address}}
|
||||||
Action: failed
|
Action: failed
|
||||||
Status: 5.0.0
|
Status: 5.0.0
|
||||||
Diagnostic-Code: smtp; {{.LastFailureMessage}}
|
Diagnostic-Code: smtp; {{.LastFailureMessage | trim | indent 4}}
|
||||||
{{end}}
|
|
||||||
|
{{end -}}
|
||||||
{{range .PendingRecipients -}}
|
{{range .PendingRecipients -}}
|
||||||
Original-Recipient: utf-8; {{.OriginalAddress}}
|
Original-Recipient: utf-8; {{.OriginalAddress}}
|
||||||
Final-Recipient: utf-8; {{.Address}}
|
Final-Recipient: utf-8; {{.Address}}
|
||||||
Action: failed
|
Action: failed
|
||||||
Status: 4.0.0
|
Status: 4.0.0
|
||||||
Diagnostic-Code: smtp; {{.LastFailureMessage}}
|
Diagnostic-Code: smtp; {{.LastFailureMessage | trim | indent 4}}
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
--{{.Boundary}}
|
--{{.Boundary}}
|
||||||
|
|||||||
@@ -7,6 +7,17 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const multilineErr = `550 5.7.1 [11:22:33:44::1] Our system has detected that this
|
||||||
|
5.7.1 message is likely unsolicited mail. To reduce the amount of spam sent
|
||||||
|
5.7.1 to BlahMail, this message has been blocked. Please visit
|
||||||
|
5.7.1 https://support.blah/mail/?p=UnsolicitedMessageError
|
||||||
|
5.7.1 for more information. a1b2c3a1b2c3a1b.123 - bsmtp`
|
||||||
|
|
||||||
|
const data = `Message-ID: <msgid-123@zaraza>
|
||||||
|
|
||||||
|
Data ñaca.
|
||||||
|
`
|
||||||
|
|
||||||
func TestDSN(t *testing.T) {
|
func TestDSN(t *testing.T) {
|
||||||
item := &Item{
|
item := &Item{
|
||||||
Message: Message{
|
Message: Message{
|
||||||
@@ -16,12 +27,14 @@ func TestDSN(t *testing.T) {
|
|||||||
Rcpt: []*Recipient{
|
Rcpt: []*Recipient{
|
||||||
{"poe@rcpt", Recipient_EMAIL, Recipient_FAILED,
|
{"poe@rcpt", Recipient_EMAIL, Recipient_FAILED,
|
||||||
"oh! horror!", "ñaca@africa.org"},
|
"oh! horror!", "ñaca@africa.org"},
|
||||||
|
{"muchos@rcpt", Recipient_EMAIL, Recipient_FAILED,
|
||||||
|
multilineErr, "pepe@africa.org"},
|
||||||
{"newman@rcpt", Recipient_EMAIL, Recipient_PENDING,
|
{"newman@rcpt", Recipient_EMAIL, Recipient_PENDING,
|
||||||
"oh! the humanity!", "ñaca@africa.org"},
|
"oh! the humanity!", "ñaca@africa.org"},
|
||||||
{"ant@rcpt", Recipient_EMAIL, Recipient_SENT,
|
{"ant@rcpt", Recipient_EMAIL, Recipient_SENT,
|
||||||
"", "negra@sosa.org"},
|
"", "negra@sosa.org"},
|
||||||
},
|
},
|
||||||
Data: []byte("data ñaca"),
|
Data: []byte(data),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,9 +55,9 @@ To: <from@from.org>
|
|||||||
Subject: Mail delivery failed: returning message to sender
|
Subject: Mail delivery failed: returning message to sender
|
||||||
Message-ID: <chasquid-dsn-???????????@dsnDomain>
|
Message-ID: <chasquid-dsn-???????????@dsnDomain>
|
||||||
Date: *
|
Date: *
|
||||||
In-Reply-To: *
|
In-Reply-To: <msgid-123@zaraza>
|
||||||
References: *
|
References: <msgid-123@zaraza>
|
||||||
X-Failed-Recipients: ñaca@africa.org,
|
X-Failed-Recipients: pepe@africa.org, ñaca@africa.org,
|
||||||
Auto-Submitted: auto-replied
|
Auto-Submitted: auto-replied
|
||||||
MIME-Version: 1.0
|
MIME-Version: 1.0
|
||||||
Content-Type: multipart/report; report-type=delivery-status;
|
Content-Type: multipart/report; report-type=delivery-status;
|
||||||
@@ -59,11 +72,19 @@ Content-Transfer-Encoding: 8bit
|
|||||||
|
|
||||||
Delivery of your message to the following recipient(s) failed permanently:
|
Delivery of your message to the following recipient(s) failed permanently:
|
||||||
|
|
||||||
|
- pepe@africa.org
|
||||||
- ñaca@africa.org
|
- ñaca@africa.org
|
||||||
|
|
||||||
|
|
||||||
Technical details:
|
Technical details:
|
||||||
- "poe@rcpt" (EMAIL) failed permanently with error:
|
- "poe@rcpt" (EMAIL) failed permanently with error:
|
||||||
oh! horror!
|
oh! horror!
|
||||||
|
- "muchos@rcpt" (EMAIL) failed permanently with error:
|
||||||
|
550 5.7.1 [11:22:33:44::1] Our system has detected that this
|
||||||
|
5.7.1 message is likely unsolicited mail. To reduce the amount of spam sent
|
||||||
|
5.7.1 to BlahMail, this message has been blocked. Please visit
|
||||||
|
5.7.1 https://support.blah/mail/?p=UnsolicitedMessageError
|
||||||
|
5.7.1 for more information. a1b2c3a1b2c3a1b.123 - bsmtp
|
||||||
- "newman@rcpt" (EMAIL) failed repeatedly and timed out, last error:
|
- "newman@rcpt" (EMAIL) failed repeatedly and timed out, last error:
|
||||||
oh! the humanity!
|
oh! the humanity!
|
||||||
|
|
||||||
@@ -81,6 +102,16 @@ Action: failed
|
|||||||
Status: 5.0.0
|
Status: 5.0.0
|
||||||
Diagnostic-Code: smtp; oh! horror!
|
Diagnostic-Code: smtp; oh! horror!
|
||||||
|
|
||||||
|
Original-Recipient: utf-8; pepe@africa.org
|
||||||
|
Final-Recipient: utf-8; muchos@rcpt
|
||||||
|
Action: failed
|
||||||
|
Status: 5.0.0
|
||||||
|
Diagnostic-Code: smtp; 550 5.7.1 [11:22:33:44::1] Our system has detected that this
|
||||||
|
5.7.1 message is likely unsolicited mail. To reduce the amount of spam sent
|
||||||
|
5.7.1 to BlahMail, this message has been blocked. Please visit
|
||||||
|
5.7.1 https://support.blah/mail/?p=UnsolicitedMessageError
|
||||||
|
5.7.1 for more information. a1b2c3a1b2c3a1b.123 - bsmtp
|
||||||
|
|
||||||
Original-Recipient: utf-8; ñaca@africa.org
|
Original-Recipient: utf-8; ñaca@africa.org
|
||||||
Final-Recipient: utf-8; newman@rcpt
|
Final-Recipient: utf-8; newman@rcpt
|
||||||
Action: failed
|
Action: failed
|
||||||
@@ -88,12 +119,16 @@ Status: 4.0.0
|
|||||||
Diagnostic-Code: smtp; oh! the humanity!
|
Diagnostic-Code: smtp; oh! the humanity!
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--???????????
|
--???????????
|
||||||
Content-Type: message/rfc822
|
Content-Type: message/rfc822
|
||||||
Content-Description: Undelivered Message
|
Content-Description: Undelivered Message
|
||||||
Content-Transfer-Encoding: 8bit
|
Content-Transfer-Encoding: 8bit
|
||||||
|
|
||||||
data ñaca
|
Message-ID: <msgid-123@zaraza>
|
||||||
|
|
||||||
|
Data ñaca.
|
||||||
|
|
||||||
|
|
||||||
--???????????--
|
--???????????--
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ Delivery of your message to the following recipient(s) failed permanently:
|
|||||||
|
|
||||||
- fail@testserver
|
- fail@testserver
|
||||||
|
|
||||||
|
|
||||||
Technical details:
|
Technical details:
|
||||||
- "false" (PIPE) failed permanently with error:
|
- "false" (PIPE) failed permanently with error:
|
||||||
exit status 1
|
exit status 1
|
||||||
|
|||||||
Reference in New Issue
Block a user