为什么Gmail API会随机替换外发邮件中HTML电子邮件中的某些字符?

时间:2014-07-27 10:43:21

标签: gmail gmail-api

除了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元素中添加了强硬换行符。每行末尾都有一个=符号。

如何防止这种情况发生?

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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

  4. 我希望你回答这个问题