Search code examples
ruby-on-railsmandrill

Extra "\n " in Mandrill raw_message break some links


I am sending e-mails from a Rails app on Heroku to Mandrill via SMTP.

Everything has been working fine for a while, but recently (detected 2 days ago), we received reports that some links are broken.

When I send the message to Mailtrap, also via SMTP, the raw message is fine, with the HTML body in 1 single line.

On Mandrill however, the raw_message that appears in the API call contains extra \n (that's a newline followed by a space).

A little bit of bad luck, and a link appears like this:

<a href=\"https://www.\n facebook.com/

With link tracking ON, this is translated to:

https://www.%20facebook.com/

with link tracking OFF, Mailbox understands it like:

https://www.%0d%0a%20facebook.com/

In both cases, the link is obviously broken. Seeing the SMTP message looks fine, I'm guessing Mandrill's SMTP server is adding the newlines when calling the API. What can I do about this? And how can I be the only one seemingly affected?

Any help is appreciated, including workarounds, as this also breaks our "Reset password" links in production!

EDIT

Here is the (slightly redacted) raw message I get in mailtrap. It's multipart text/html, and everything looks good to me.

Date: Thu, 26 Feb 2015 16:11:49 +0000
From: redacted <[email protected]>
Reply-To: redacted <[email protected]>
To: [email protected]
Message-ID: <54ef45c51372b_3a3fddaa4e53f081961@3d32585e-59a2-456b-b7ff-4ed464b73400.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_54ef45c510602_3a3fddaa4e53f081842";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_54ef45c510602_3a3fddaa4e53f081842
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Hello redacted,

Someone has requested a link to change your password for redacted. You can do this through the link below.

http://redacted.io/security/users/password/edit?reset_password_token=redacted

If you didn't request this, please ignore this email

Your password won't change until you access the link above and create a new one.

----==_mimepart_54ef45c510602_3a3fddaa4e53f081842
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<div style="width: 100%; background: #ffffff;"><table style="width: 100%; max-width: 520px; margin: 0 auto; border: 0; border-collapse: collapse;"><tr><td style="width: 50%;  padding: 10px;"><img alt="redacted" src="https://s3-eu-west-1.amazonaws.com/redacted/redacted.png" /></td><td style="width: 50%; padding: 20px; text-align: right; vertical-align: middle; font-family: Helvetica,Arial,sans-serif; color: #8494a4; font-size: 14px;">LOST PASSWORD</td></tr><tr><td colspan="2" style="padding: 40px 50px 20px 50px; font-family: Helvetica,Arial,sans-serif; color: #8494a4; font-size: 14px;"><p>Hello redacted,</p><p>Someone has requested a link to change your password for redacted. You can do this through the link below.</p></td></tr></table><table style="width: 100%; max-width: 520px; margin: 0 auto; border: 0; border-collapse: collapse;"><tr><td width="33%"></td><td style="background: #3897d3; text-align: center; vertical-align: middle; width: 34%; height: 50px;" width="34%"><a href="http://redacted.io/security/users/password/edit?reset_password_token=redacted" style="font-family: Helvetica,Arial,sans-serif; font-size: 14px; color: #ffffff; text-decoration: none; background: #3897d3; display: block; padding: 15px 0; text-align: center;">Change password</a></td><td width="33%"></td></tr><tr><td colspan="3" style="padding: 20px 50px; font-family: Helvetica,Arial,sans-serif; color: #8494a4; font-size: 14px;"><p>If you didn&#39;t request this, please ignore this email</p><p>Your password won&#39;t change until you access the link above and create a new one.</p></td></tr><tr><td colspan="3" style="padding: 30px 0 10px 0; text-align: center; font-family: Helvetica,Arial,sans-serif; font-size: 12px; color: #8494a4;"><a href="http://redacted.io/" style="color: #3897d3; text-decoration: none;">redacted</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href="https://twitter.com/redacted" style="color: #3897d3; text-decoration: none;">Twitter</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href="https://www.facebook.com/redacted" style="color: #3897d3; text-decoration: none;">Facebook</a>&nbsp;&nbsp;-&nbsp;&nbsp;<a href="https://plus.google.com/redacted" style="color: #3897d3; text-decoration: none;">Google+</a></td></tr></table></div>
----==_mimepart_54ef45c510602_3a3fddaa4e53f081842--

Solution

  • We typically see this kind of issue with SMTP libraries or frameworks that generate HTML with no true line breaks. The SMTP specs state that the line length for email shouldn't exceed 1000 characters. When that limit is reached, a line break gets inserted automatically when the message data is being transmitted over SMTP. This unfortunately often happens right in the middle of a word or a URL, for example. You'll want to take a look at your SMTP library to see if you can modify how line breaks are being handled.

    If you're using HTML line breaks like <br> that are being used to indicate a break, those unfortunately won't help in this case. Adding your own line breaks (not HTML line breaks, but actual line breaks in the data such as a newline or end of line - usually \r\n - will help ensure that the forced line breaks aren't arbitrarily added in the SMTP conversation in inconvenient places.