Mandrill raw_message 中的额外“\n”会破坏一些链接

Posted

技术标签:

【中文标题】Mandrill raw_message 中的额外“\\n”会破坏一些链接【英文标题】:Extra "\n " in Mandrill raw_message break some linksMandrill raw_message 中的额外“\n”会破坏一些链接 【发布时间】:2015-04-29 06:03:59 【问题描述】:

我正在通过 SMTP 从 Heroku 上的 Rails 应用程序向 Mandrill 发送电子邮件。

一段时间以来一切正常,但最近(2 天前检测到),我们收到了一些链接已损坏的报告。

当我通过 SMTP 将消息发送到 Mailtrap 时,原始消息很好,html 正文在 1 行中。

然而,在 Mandrill 上,出现在 API 调用中的 raw_message 包含额外的 \n(即换行符后跟一个空格)。

有点倒霉,出现一个链接是这样的:

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

打开链接跟踪,这将转换为:

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

关闭链接跟踪后,Mailbox 的理解如下:

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

在这两种情况下,链接显然都断开了。看到 SMTP 消息看起来不错,我猜 Mandrill 的 SMTP 服务器在调用 API 时添加了换行符。我能做些什么呢?我怎么可能是唯一一个似乎受到影响的人?

感谢您提供任何帮助,包括解决方法,因为这也会破坏我们在生产中的“重置密码”链接!

编辑

这是我在 mailtrap 中收到的(略微编辑的)原始消息。它是多部分文本/html,对我来说一切都很好。

Date: Thu, 26 Feb 2015 16:11:49 +0000
From: redacted <notifications+staging@redacted.com>
Reply-To: redacted <support@redacted.io>
To: redacted@redacted.com
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  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 ></td><td style="background: #3897d3; text-align: center; vertical-align: middle; width: 34%; height: 50px;" ><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 ></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--

【问题讨论】:

您的电子邮件模板是什么样的? 【参考方案1】:

您确定要发送 HTML 格式的电子邮件吗?好像您以纯文本格式发送它,而 Mandril 将其对齐到一列。你能计算出\n符号的位置吗?

更新:

尝试将X-MC-Autotext 电子邮件标头设置为no,并将X-MC-AutoHtml 设置为no。描述的所有标题都是here

【讨论】:

我用发送到 Mailtrap 时收到的原始消息编辑了我的问题。忽略转义 (\"),剪辑周围的每个块都是 998 个字符长。我有“从 html 生成文本选项”,虽然我很确定它没有使用,因为我提供了文本,并且“生成 html”在默认情况下处于关闭状态。 那个链接坏了 @raven 固定链接。【参考方案2】:

我们通常会在生成没有真正换行符的 HTML 的 SMTP 库或框架中看到此类问题。 SMTP 规范规定电子邮件的行长不应超过 1000 个字符。当达到该限制时,将在通过 SMTP 传输消息数据时自动插入换行符。例如,不幸的是,这经常发生在单词或 URL 的中间。您需要查看您的 SMTP 库,看看是否可以修改换行符的处理方式。

如果您使用像&lt;br&gt; 这样的HTML 换行符来表示中断,那么不幸的是,这些换行符在这种情况下无济于事。添加您自己的换行符(不是 HTML 换行符,而是数据中的实际换行符,例如换行符或行尾 - 通常是 \r\n - 将有助于确保不会在 SMTP 对话中任意添加强制换行符不方便的地方。

【讨论】:

好的,显然这是因为 SLIM 默认编译为单行。使用pretty: true 选项会强制它在您期望的位置放置换行符,因此不会突破到 1000 的限制。感谢凯特琳!

以上是关于Mandrill raw_message 中的额外“\n”会破坏一些链接的主要内容,如果未能解决你的问题,请参考以下文章

Mandrill 中的子域

使用车把和 Mandrill-dotnet -library 在 mandrill 中的每个循环

找不到 Mandrill 红宝石宝石的方法 - 示例中的剪切和粘贴

如何将 MailChimp 设计的活动转换为 Mandrill 中的模板?

Parse 的 javascript CloudCode 的 httprequest 字符串操作添加额外的反斜杠

Mandrill 模板中的条件内容