Emails with longer subjects get bounced
Closed this issue · 2 comments
TL;DR: The quoted-printable encoding is not implemented correctly for subjects.
Background story
I am part of the team that opened this PR a while back: #21. We are currently using bamboo_ses from our own fork that has the changes from that PR, but we would want to switch back to your fork.
When testing the current version of bamboo_ses on staging, we noticed that some emails get bounced.
Here's part of the bounce notification from SES:
"bouncedRecipients": [
{
"emailAddress": "angelika+6@our-domain.com",
"action": "failed",
"status": "5.7.1",
"diagnosticCode": "smtp; 550-5.7.1 [69.169.226.217 11] Our system has detected that this message is\n550-5.7.1 not RFC 5322 compliant:\n550-5.7.1 'From' header is missing.\n550-5.7.1 To reduce the amount of spam sent to Gmail, this message has been\n550-5.7.1 blocked. Please visit\n550-5.7.1 https://support.google.com/mail/?p=RfcMessageNonCompliant\n550 5.7.1 and review RFC 5322 specifications for more information. j1si4459323wrp.107 - gsmtp"
}
]
And another part that lists the headers of the email:
"headers": [
{
"name": "To",
"value": "angelika+6@our-domain.com"
},
{
"name": "Subject",
"value": "=?utf-8?Q?=F0=9F=92=8C qqqqq just published: =C2=BBtest umlauts 3rd time =C3=B6=C3=BC="
},
{
"name": "=C3=A4=C2=AB?=",
"value": "=C3=A4=C2=AB?="
},
{
"name": "Reply-To",
"value": "\"=?utf-8?Q?Angelika6 Tyborska6?=\" <angelika+6@our-domain.com>"
},
{
"name": "Mime-Version",
"value": "1.0"
},
{
"name": "From",
"value": "\"=?utf-8?Q?qqqqq (via Steady)?=\" <support@our-domain.com>"
},
{
"name": "Content-Type",
"value": "multipart/alternative; boundary=\"A1E5B8009A42C39BF811C67E\""
}
]
The bounce reason says that the "From"
header is missing, but it's there. However, there is some nonsense header that shouldn't exist - "=C3=A4=C2=AB?="
. This got me thinking that there's something wrong with how the email is rendered.
The problem
Let's say take an email with this subject:
This is a long subject with an emoji 🙂 bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
This is how bamboo_ses renders the subject:
Subject: =?utf-8?Q?This is a long subject with an emoji =F0=9F=99=82 bla bla bla bla bla bla b=
la bla bla bla bla bla bla bla bla bla?=
This is how it looks like when I sent myself an email with the same subject between two Gmail accounts using the Thunderbird email client:
Subject: =?UTF-8?Q?This_is_a_long_subject_with_an_emoji_=f0=9f=99=82_bla_bla?=
=?UTF-8?Q?_bla_bla_bla_bla_bla_bla_bla_bla_bla_bla_bla_bla_bla_bla?=
I see a few differences:
- The max line length of 76 characters is respected.
- Spaces are changed to underscores.
- Each new line is wrapped by its own
?UTF-8?Q?
and?=
.
I am not sure if all of those differences are relevant. I haven't read all of the email-related RFCs, but my guess would be that the last one is the problem.
I tried to create a unit test that would show this problem, here it is: steady-media@e832916
When you run it, you will get an error because Mail.Parsers.RFC2822
cannot parse the headers:
** (MatchError) no match of right hand side value: ["la bla bla bla bla bla bla bla bla bla?="]
code: SesAdapter.deliver(
stacktrace:
(mail 0.2.1) lib/mail/parsers/rfc_2822.ex:95: Mail.Parsers.RFC2822.parse_headers/2
(mail 0.2.1) lib/mail/parsers/rfc_2822.ex:21: Mail.Parsers.RFC2822.parse/1
test/lib/bamboo/adapters/ses_adapter_test.exs:60: anonymous fn/5 in Bamboo.SesAdapterTest."test delivers successfully with long subject"/1
(ex_aws 2.1.3) lib/ex_aws/request.ex:42: ExAws.Request.request_and_retry/7
(ex_aws 2.1.3) lib/ex_aws/operation/query.ex:41: ExAws.Operation.ExAws.Operation.Query.perform/2
(bamboo_ses 0.1.5) lib/bamboo/adapters/ses_adapter.ex:44: Bamboo.SesAdapter.deliver/2
test/lib/bamboo/adapters/ses_adapter_test.exs:70: (test)
Thanks for detailed description.
I identified the cause of the problem. https://github.com/DockYard/elixir-mail/blob/master/lib/mail/encoders/quoted_printable.ex#L10
Now I'm considering possible solutions.
Fixed with 0.1.6