Gmail API - 明文自动换行
Gmail API - plaintext word wrapping
使用 Gmail API 发送电子邮件时,它会在 body 中放置硬换行符,每行约 78 个字符。可以找到关于此的类似问题 here.
我怎样才能停下来?我只是想通过 API 发送纯文本电子邮件而没有换行符。当前的格式看起来很糟糕,尤其是在移动客户端上(在 Gmail 和 iOS 邮件应用程序上测试过)。
我尝试了以下 headers:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
我错过了什么吗?
编辑: 根据 Mr.Rebot 的建议,我也尝试过但没有成功:
Content-Type: mixed/alternative
编辑 2: 这是我发送的消息的确切格式(尝试使用和不使用 quoted-printable
header:
From: Example Account <example1@example.com>
To: <example2@example.com>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Subject: This is a test!
Date: Tue, 18 Oct 2016 10:46:57 -GMT-07:00
Here is a long test message that will probably cause some words to wrap in strange places.
我将这条完整的消息进行 Base64 编码,然后 POST 使用以下 JSON body:
将其 /gmail/v1/users/{my_account}/drafts/send?fields=id
{
"id": MSG_ID,
"message": {
"raw": BASE64_DATA
}
}
您 运行 内容是通过 quoted printable 编码器并将编码后的内容值与 header 一起发送,还是希望 API 为您编码?
Per wikipedia 似乎如果您添加 =
小于 76 个字符的软换行符作为任意行的最后一个字符,它们应该从恢复原始结果的结果中解码出来文字.
更新
尝试发送此内容,其消息已 quoted-printable 编码(base64):
From: Example Account <example1@example.com>
To: <example2@example.com>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Subject: This is a test!
Date: Tue, 18 Oct 2016 10:46:57 -GMT-07:00
Here is a long test message that will probably cause some words to wrap in =
strange places.
我假设你有类似这样的功能:
1. def create_message(sender, to, cc, subject, message_body):
2. message = MIMEText(message_body, 'html')
3. message['to'] = to
4. message['from'] = sender
5. message['subject'] = subject
6. message['cc'] = cc
7. return {'raw': base64.urlsafe_b64encode(message.as_string())}
在尝试修改 header 值和有效载荷字典(它是 message
object), 是设置 (line 2
):
message = MIMEText(message_body, 'html')
<-- 添加 'html'
作为 MIMEText object 构造函数的第二个参数
Google 为他们的 gmail API 提供的默认代码只告诉你如何发送纯文本电子邮件,但他们隐藏了他们是如何发送的这样做。
翼...
message = MIMEText(message_body)
我不得不查找 python class email.mime.text.MIMEText
object。
在那里您将看到 MIMEText object:
构造函数的定义
- class email.mime.text.MIMEText(_text[ _subtype[ _charset]])
我们想明确地将一个值传递给
_subtype
。在这种情况下,我们要传递:'html'
作为 _subtype
.
现在,Google 或 Python mime.text.MIMEText
不会再对您的消息应用意外的自动换行=57=]
这个问题让我疯狂了好几个小时,但我找不到任何解决方案。
所以如果其他人最终在这里感到沮丧,我想我只是 post 我的“解决方案”。
将您的文本(电子邮件的正文)变成简单的 HTML。我将每个段落都包裹在一个简单的 <p>
中,并在需要的地方添加了换行符 (<br>
)(例如我的签名)。
然后,根据 ,我将邮件正文附加为 MIMEText(message_text, _subtype="html")
。纯文本仍然不正确 AFAIK,但它有效,我认为没有一个活跃使用的电子邮件客户端不再呈现 HTML。
使用 Gmail API 发送电子邮件时,它会在 body 中放置硬换行符,每行约 78 个字符。可以找到关于此的类似问题 here.
我怎样才能停下来?我只是想通过 API 发送纯文本电子邮件而没有换行符。当前的格式看起来很糟糕,尤其是在移动客户端上(在 Gmail 和 iOS 邮件应用程序上测试过)。
我尝试了以下 headers:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
我错过了什么吗?
编辑: 根据 Mr.Rebot 的建议,我也尝试过但没有成功:
Content-Type: mixed/alternative
编辑 2: 这是我发送的消息的确切格式(尝试使用和不使用 quoted-printable
header:
From: Example Account <example1@example.com>
To: <example2@example.com>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Subject: This is a test!
Date: Tue, 18 Oct 2016 10:46:57 -GMT-07:00
Here is a long test message that will probably cause some words to wrap in strange places.
我将这条完整的消息进行 Base64 编码,然后 POST 使用以下 JSON body:
将其/gmail/v1/users/{my_account}/drafts/send?fields=id
{
"id": MSG_ID,
"message": {
"raw": BASE64_DATA
}
}
您 运行 内容是通过 quoted printable 编码器并将编码后的内容值与 header 一起发送,还是希望 API 为您编码?
Per wikipedia 似乎如果您添加 =
小于 76 个字符的软换行符作为任意行的最后一个字符,它们应该从恢复原始结果的结果中解码出来文字.
更新
尝试发送此内容,其消息已 quoted-printable 编码(base64):
From: Example Account <example1@example.com>
To: <example2@example.com>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Subject: This is a test!
Date: Tue, 18 Oct 2016 10:46:57 -GMT-07:00
Here is a long test message that will probably cause some words to wrap in =
strange places.
我假设你有类似这样的功能:
1. def create_message(sender, to, cc, subject, message_body): 2. message = MIMEText(message_body, 'html') 3. message['to'] = to 4. message['from'] = sender 5. message['subject'] = subject 6. message['cc'] = cc 7. return {'raw': base64.urlsafe_b64encode(message.as_string())}
在尝试修改 header 值和有效载荷字典(它是 message
object), 是设置 (line 2
):
message = MIMEText(message_body, 'html')
<-- 添加'html'
作为 MIMEText object 构造函数的第二个参数
Google 为他们的 gmail API 提供的默认代码只告诉你如何发送纯文本电子邮件,但他们隐藏了他们是如何发送的这样做。
翼...
message = MIMEText(message_body)
我不得不查找 python class email.mime.text.MIMEText
object。
在那里您将看到 MIMEText object:
- class email.mime.text.MIMEText(_text[ _subtype[ _charset]])
我们想明确地将一个值传递给
_subtype
。在这种情况下,我们要传递:'html'
作为_subtype
.
现在,Google 或 Python mime.text.MIMEText
不会再对您的消息应用意外的自动换行=57=]
这个问题让我疯狂了好几个小时,但我找不到任何解决方案。
所以如果其他人最终在这里感到沮丧,我想我只是 post 我的“解决方案”。
将您的文本(电子邮件的正文)变成简单的 HTML。我将每个段落都包裹在一个简单的 <p>
中,并在需要的地方添加了换行符 (<br>
)(例如我的签名)。
然后,根据 MIMEText(message_text, _subtype="html")
。纯文本仍然不正确 AFAIK,但它有效,我认为没有一个活跃使用的电子邮件客户端不再呈现 HTML。