【问题标题】:Why does the Gmail API randomly replace some characters in HTML emails in outgoing messages?为什么 Gmail API 会随机替换外发邮件中 HTML 电子邮件中的某些字符?
【发布时间】:2017-01-27 22:21:29
【问题描述】:

除了another StackOverflow question 中提到的奇怪的标记消失问题之外,我还注意到一些奇怪的编码问题,其中一些字符被随机字母替换。当标记中有很长的行时,这似乎会发生。以下是示例:

正常行为

处理 Gmail API 之前

<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Email Title</title>
  </head>
  <body>
    <p style="font-size: 16px; line-height: 24px; margin-top: 0; margin-bottom: 0; font-family: 'ff-tisa-web-pro, Georgia, serif;">Pinterest mumblecore authentic stumptown, deep v slowcarb skateboard Intelligentsia food truck VHS. Asymmetrical swag raw denim put a bird on it Echo Park. Pinterest four loko lofi forage gentrify cray.</p>
  </body>
</html>

Gmail API 处理后(通过在 Gmail 中打开邮件,然后选择Show original)。

--001a1133f016eff52804ff2a2885
Content-Type: text/html; charset=ISO-8859-1

<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email Title</title>
  </head>
  <body>
    <p style>Pinterest mumblecore authentic stumptown, deep v slowcarb skateboard Intelligentsia food truck VHS. Asymmetrical swag raw denim put a bird on it Echo Park. Pinterest four loko lofi forage gentrify cray.</p>
  </body>
</html>


--001a1133f016eff52804ff2a2885--

在上面的例子中,发生的事情是我们所期望的。但是,一旦p 元素的行长变长,我们就会出现异常行为。


奇怪的行为

处理 Gmail API 之前

<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Email Title</title>
  </head>
  <body>
    <p style="font-size: 16px; line-height: 24px; margin-top: 0; margin-bottom: 0; font-family: 'ff-tisa-web-pro, Georgia, serif;">Pinterest mumblecore authentic stumptown, deep v slowcarb skateboard Intelligentsia food truck VHS. Asymmetrical swag raw denim put a bird on it Echo Park. Pinterest four loko lofi forage gentrify cray. Pinterest mumblecore authentic stumptown, deep v slowcarb skateboard Intelligentsia food truck VHS. Asymmetrical swag raw denim put a bird on it Echo Park. Pinterest four loko lofi forage gentrify cray.</p>
  </body>
</html>

Gmail API 处理后(通过在 Gmail 中打开邮件,然后选择Show original)。

--001a1133547278e12e04ff2a28d8
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
    <meta name=3D"viewport" content=3D"width=3Ddevice-width, initial-scale=
=3D1.0">
    <title>Email Title</title>
  </head>
  <body>
    <p style>Pinterest mumblecore authentic stumptown, deep v slowcarb skat=
eboard Intelligentsia food truck VHS. Asymmetrical swag raw denim put a bir=
d on it Echo Park. Pinterest four loko lofi forage gentrify cray. Pinterest=
 mumblecore authentic stumptown, deep v slowcarb skateboard Intelligentsia =
food truck VHS. Asymmetrical swag raw denim put a bird on it Echo Park. Pin=
terest four loko lofi forage gentrify cray.</p>

  </body>
</html>


--001a1133547278e12e04ff2a28d8--

在上面的示例中,p 元素内的字符数增加了一倍。不知何故,这会触发各种奇怪的编码问题。请注意,添加了关于标记的Content-Transfer-Encoding: quoted-printable。另请注意,3D 出现在每个 = 之后。此外,p 元素中添加了硬换行符。在每一行的末尾都有一个= 符号。

如何防止这种情况发生?

【问题讨论】:

  • 我不知道如何防止这种情况,但这很像我所有电子邮件存储在我电脑上的格式。 (我正在使用一个使用Maildir 来存储消息的客户端。)
  • 顺便说一句:转义的不仅是换行符和等号,还有元音变音(自己试试:äöüß)。
  • 啊,在那个话题上找到了Wikipedia article
  • 电子邮件是否正确呈现?如果是这样,那么确切的传输机制就不应该那么重要了。

标签: gmail gmail-api


【解决方案1】:

Google 使用标准 RFC822 存储电子邮件,在这种特定情况下,您可以在 Content-Type text/html 旁边的标题中看到,您可以找到标题 Content-Transfer-Encoding:quoted-printable, (@987654321 @)。

因此您需要解析 RFC822 消息以获取实际的 html。

  1. 找到正确的块(消息的格式就像一个多部分,使用您将在第一个标头中找到的边界

  2. 解析 Chunk 的标头并获取编码类型(并不总是可以引用打印,所以要小心)

  3. 使用上一步的encode解码块体

我希望这个回答你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-07
    • 2016-10-18
    • 2011-08-16
    • 2015-07-09
    • 2015-01-02
    • 2010-12-06
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多