0

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

正常行为

处理 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出现在 every 之后=p此外,元素中还添加了硬换行符。每行的末尾都有一个=标志。

我该如何防止这种情况发生?

4

1 回答 1

0

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

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

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

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

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

我希望这个回答你的问题

于 2017-01-27T14:21:29.843 回答