Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounces from multiple providers due to 501 5.5.4 Syntax error in BODY parameter #37

Closed
chriselkins opened this issue Aug 12, 2022 · 6 comments · Fixed by #38 or #40
Closed

Bounces from multiple providers due to 501 5.5.4 Syntax error in BODY parameter #37

chriselkins opened this issue Aug 12, 2022 · 6 comments · Fixed by #38 or #40
Assignees
Labels
bug Something isn't working

Comments

@chriselkins
Copy link

chriselkins commented Aug 12, 2022

Description

Some domains we are sending to are producing bounces with : "501 5.5.4 Syntax error in BODY parameter". We've also had an instance of "550 Maximum line length exceeded (see RFC 5322 2.1.1).". The plain text message we are sending is static and I've ensured every line is less than 78 characters long. The emails we send do contain a PDF attachment. At first I thought it may be something in our HTML template so we have since switched to only sending plain text emails and are still seeing the issue. Emails to Google and lot's of other domains are not bouncing.

Relevant code (removed error handling and redacted email addresses only):

func makeValidUTF8(s string) string {
	return strings.ToValidUTF8(s, "")
}

// email sending code
m := mail.NewMsg()
fromerr := m.FromFormat("Magnolia Fabrics", `redacted`)

toerr := m.To(`redacted`)

m.SetCharset(mail.CharsetUTF8)
m.Subject(makeValidUTF8(fmt.Sprintf("Magnolia Fabrics Invoice - %s", `645211`)))
m.SetDate()
m.SetMessageID()
m.SetImportance(mail.ImportanceHigh)

pdfreader := bytes.NewBuffer(pdf)
m.AttachReader(fmt.Sprintf("%s.pdf", `645211`), pdfreader)

m.SetBodyString(mail.TypeTextPlain, makeValidUTF8(cfg.PlainTextEmailMessage))

if !cfg.SendPlainTextOnlyEmails {
    m.SetBodyString(mail.TypeTextHTML, makeValidUTF8(string(b.Bytes())))
}

c, cerr := mail.NewClient(cfg.SMTPServer,
    mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithUsername(cfg.SMTPUser),
    mail.WithPassword(cfg.SMTPPassword), mail.WithTLSPolicy(mail.TLSMandatory))

derr := c.DialAndSend(m)

In go.mod and go.sum:

// in go.mod
github.com/wneessen/go-mail v0.2.5 // indirect

// in go.sum
github.com/wneessen/go-mail v0.2.5 h1:lVQ5Q1hYaUNU/VL9F4AOtYaBAxgmrH+6W6wwMcDpKDE=
github.com/wneessen/go-mail v0.2.5/go.mod h1:m25lkU2GYQnlVr6tdwK533/UXxo57V0kLOjaFYmub0E=

The values of the relevant variables mentioned above are read from this TOML file:

send_plain_text_only_emails = true
smtp_server    = 'email-smtp.us-east-1.amazonaws.com'
plain_text_email_message = '''Dear Customer,

Good news! Your order is on the way and invoice is attached!

You will find your tracking number on the invoice. Tracking data may take
up to 24 hours to be accessible online.

• Please remit payment at your earliest convenience unless invoice is
marked “PAID”.
• Some items may ship separately from multiple locations. Separate
invoices will be issued when applicable.
• PLEASE INSPECT UPON RECEIPT FOR PATTERN, COLOR, DEFECTS, DAMAGE FROM
SHIPPING, CORRECT YARDAGE, ETC! Once an order is cut or sewn, no returns
will be accepted for any reason, no matter the party in error. No returns
will be authorized after 30 days of invoice date. No exceptions will be
made.

Thank your for your business!

Magnolia Fabrics'''

Domains we are seeing bounces from:
yadtel.net, eplus.net, windstream.net, lawlessupholstery.com, comporium.net

Examples of the bounces:

>>> [email protected] (reading confirmation): 501 5.5.4 Syntax error in BODY parameter
Arrival-Date: Mon, 01 Aug 2022 16:26:13 -0400
Reporting-MTA: dns; mx04.nrtc.email-ash1.sync.lan

>>> [email protected] (reading confirmation): 501 5.5.4 Syntax error in BODY parameter
Reporting-MTA: dns; mx04.nrtc.email-ash1.sync.lan
Arrival-Date: Tue, 02 Aug 2022 16:59:33 -0400

Action: failed
Last-Attempt-Date: Tue, 02 Aug 2022 16:59:33 -0400
Remote-MTA: dns; 10.219.135.68
Diagnostic-Code: smtp; 501 5.5.4 Syntax error in BODY parameter
Final-Recipient: rfc822; [email protected]
Status: 5.5.4

>>> [email protected] (reading confirmation): 501 5.5.4 Syntax error in BODY parameter

>>> [email protected] (reading confirmation): 501 5.5.4 Syntax error in BODY parameter

An error occurred while trying to deliver the mail to the following recipients:
[email protected]
Reporting-MTA: dns; a77-213.smtp-out.amazonses.com

Action: failed
Final-Recipient: rfc822; [email protected]
Diagnostic-Code: smtp; 550 Maximum line length exceeded (see RFC 5322 2.1.1).
Status: 5.3.0

Example of email successfully received at a Google account -- contents copied from the "Show Original" option in Gmail:

Delivered-To: redacted
Date: Fri, 12 Aug 2022 21:37:33 +0000
Importance: high
MIME-Version: 1.0
Message-ID: redacted
Priority: 1
Subject: Magnolia Fabrics Invoice - 42
User-Agent: go-mail v0.2.4 // https://github.com/wneessen/go-mail
X-MSMail-Priority: 1
X-Mailer: go-mail v0.2.4 // https://github.com/wneessen/go-mail
X-Priority: 1
From: redacted
To: redacted
Content-Type: multipart/mixed; boundary=f5d04f41bcb3d5701fa0e855a6fdde977c003c89d2bf398c6de7abaaebde


--f5d04f41bcb3d5701fa0e855a6fdde977c003c89d2bf398c6de7abaaebde
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8

Dear Customer,

Good news! Your order is on the way and invoice is attached!

You will find your tracking number on the invoice. Tracking data may take
up to 24 hours to be accessible online.

=E2=80=A2 Please remit payment at your earliest convenience unless invoice =
is
marked =E2=80=9CPAID=E2=80=9D.
=E2=80=A2 Some items may ship separately from multiple locations. Separate
invoices will be issued when applicable.
=E2=80=A2 PLEASE INSPECT UPON RECEIPT FOR PATTERN, COLOR, DEFECTS, DAMAGE F=
ROM
SHIPPING, CORRECT YARDAGE, ETC! Once an order is cut or sewn, no returns
will be accepted for any reason, no matter the party in error. No returns
will be authorized after 30 days of invoice date. No exceptions will be
made.

Thank your for your business!

Magnolia Fabrics
--f5d04f41bcb3d5701fa0e855a6fdde977c003c89d2bf398c6de7abaaebde
Content-Disposition: attachment; filename="42.pdf"
Content-Transfer-Encoding: base64
Content-Type: application/pdf; name="42.pdf"


--f5d04f41bcb3d5701fa0e855a6fdde977c003c89d2bf398c6de7abaaebde--

To Reproduce

We're running the app on a customer's Windows server; we build and develop on Linux.

We have run go get -u github.com/wneessen/go-mail to ensure we are on latest version of lib.

The app is built with:
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o invoice-generator.exe

Expected behaviour

No bounces due to syntax error in body.

Screenshots

No response

Attempted Fixes

No response

Additional context

No response

@chriselkins chriselkins added the bug Something isn't working label Aug 12, 2022
@wneessen wneessen self-assigned this Aug 13, 2022
@wneessen
Copy link
Owner

Hi Chris,

this is hard to narrow down, since it's a little bit "black magic" on what's happening on the recipient server end. Since this only happens for some recipients, I assume that it's some hard limit, that some mail servers still allow and some are more strict on. I've checked the mail server EHLO messages of the different domains and they all seem to use the same mail server software.

At first glance, this might be due to the attachment. By default go-mail uses Base64 encoding for attachments. The base64 encoder of Go does not limit the length of the output, so the attachments might have long lines, which might not correspond with RFC 5322 2.1.1 - which describes a maximum line length.

For testing purpose, I've created a new branch https://github.com/wneessen/go-mail/tree/37-bugfix in which I've changed the attachment encoding to quoted printable, which will make sure that lines are split at ~78 char length. Could you download that branch and try again or alternatively, if that is not easy to accomplish, remove the attachment from your code for testing purpose, so we can easily see if the attachment length is causing the issue? If that is the case, I have a better understanding of what is happening and can implement a patch accordingly.

wneessen added a commit that referenced this issue Aug 14, 2022
By default, the encoding/base64 in Go does not add line breaks to its output.
This patch introduces the Base64LineBreaker which satisfies the io.WriteCloser interface
Attachments are now correctly broken up into maximum of 76 chars
@wneessen
Copy link
Owner

Hi again Chris,

please disregard my previous message. I've meanwhile implemented a proper Base64LineBreaker which will make sure that any Base64 encoded mail part (like i. e. attachments) is properly encoded and lines are broken at 76 chars lenght. This should fix the issues with the 550 Maximum line length exceeded (see RFC 5322 2.1.1). errors. It's hard to say what the other servers mean by 501 5.5.4 Syntax error in BODY parameter but maybe the address the same issue and this fix also resolves those errors. I'll release v0.2.6 later today. Please try it out and let me know if this solves your issues.

This was linked to pull requests Aug 14, 2022
@chriselkins
Copy link
Author

Thanks! I bet that will fix it. I've rebuilt my app and deployed it. It sends out ~80 emails per night so I should know by tomorrow. I'll keep you updated.

@wneessen
Copy link
Owner

@chriselkins How did the last batch go? Any further bounces?

@chriselkins
Copy link
Author

I haven't received any bounces this week. I think we're good to go and this issue can be closed. Thanks for taking care of this!

@wneessen
Copy link
Owner

Great to hear that. Closing the issue for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants