Bry*_*ant 5 python mime-message gmail-api
我正在使用 Gmail API 在 Python 中自动创建 Gmail 草稿。我需要创建 HTML 格式的电子邮件,但我个人也需要创建纯文本回退,因为这是正确的做法。
我以为我已经完成了上述所有工作,直到我尝试使纯文本后备与 HTML 略有不同。似乎谷歌自己为我创建了明文后备,而不是使用我提供的,所以如果我的 html body is<HTML><BODY>HTML Body</BODY></HTML>
和我的明文 body is Plaintext body
,最终的明文正文将是HTML Body
,丢弃我提供的明文。
我的问题:有没有人知道让 Gmail API 使用我提供的明文而不是自动生成回退的方法?
我注意到的一个相关项目:如果我以不同的顺序附加 HTML 和纯文本正文,则会发生相反的情况 - GMail 将根据我的纯文本自动生成 HTML 正文。所以它似乎只关注最后一个附着的身体。
我正在使用的代码的精简版:
import base64
import os
import httplib2
import oauth2client
from oauth2client import client
from oauth2client import tools
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from pathlib import Path
from apiclient import errors
from apiclient import discovery
SCOPES = 'https://mail.google.com/'
CLIENT_SECRET_FILE = 'client_id.json'
APPLICATION_NAME = 'Test Client'
def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'gmail-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def CreateDraft(service, user_id, message_body):
message = {'message': message_body}
draft = service.users().drafts().create(userId=user_id, body=message).execute()
return draft
def CreateTestMessage(sender, to, subject):
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to
plain_text = "Text email message. It's plain. It's text."
html_text = """\
<html>
<head></head>
<body>
<p>HTML email message</p>
<ol>
<li>As easy as one</li>
<li>two</li>
<li>three!</li>
</ol>
<p>Includes <a href="http://stackoverflow.com/">linktacular</a> goodness</p>
</body>
</html>
"""
# Swapping the following two lines results in Gmail generating HTML
# based on plaintext, as opposed to generating plaintext based on HTML
msg.attach(MIMEText(plain_text, 'plain'))
msg.attach(MIMEText(html_text, 'html'))
print('-----\nHere is the message:\n\n{m}'.format(m=msg))
encoded = base64.urlsafe_b64encode(msg.as_string().encode('UTF-8')).decode('UTF-8')
return {'raw': encoded}
def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
my_address = 'example@gmail.com' # Obscured to protect the culpable
test_message = CreateTestMessage(sender=my_address,
to='example@gmail.com',
subject='Subject line Here')
draft = CreateDraft(service, my_address, test_message)
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
更新:以下是我发送到Gmail 的内容与从Gmail发送的内容的示例要点,分别是 HTML-then-plaintext 和 plaintext-then-HTML 订单(生成不同的结果)
长话短说:没有。
草稿对象与 Web UI 和移动 UI 共享,如果文本/纯文本不仅仅是文本/html 的简单转换,那么一旦用户使用任何其他 UI 编辑消息,特殊定制就会丢失。使用草稿 UI 的原因是允许用户在其他界面之间共享这些草稿。
如果您不关心/想要这种能力,请不要使用草稿,只需在最后发送()它,这与 SMTP-MSA 一样允许更大的灵活性。
归档时间: |
|
查看次数: |
1056 次 |
最近记录: |