python – 以用户指定的HTML和明文主体编程方式发布Gmail草稿
作者:互联网
我正在使用Gmail API自动创建Python草稿中的Gmail草稿.我需要创建HTML格式的电子邮件,但我个人也需要创建一个明文后备,因为这是正确的事情.
我以为我已经完成了上述所有工作,直到我尝试使用与HTML相同的明文回调.似乎Google自己为我创建明文后备,而不是使用我提供的那个,所以如果我的html主体是< HTML>< BODY> HTML Body< / BODY>< / HTML>我的明文主体是明文体,最后的明文主体是HTML Body,丢弃我提供的明文.
我的问题:有没有人知道如何让Gmail API使用我提供的纯文本,而不是为我自动生成回退?
我注意到的一个相关项目:如果我以不同的顺序附加HTML和Plaintext主体,则相反的情况发生 – 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="https://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()
更新:以下是我发送给Gmail的example gists与从Gmail发送的内容,以及HTML-then-plaintext和纯文本然后HTML订单(生成不同的结果)
解决方法:
TL; DR:不.
草稿对象与Web UI和移动UI共享,如果text / plain不仅仅是text / html的简单转换,那么只要用户使用任何其他UI来编辑特殊定制将丢失的消息.使用草稿UI的原因是允许用户在其他界面之间共享这些草稿.
如果你不关心/希望这种能力不使用草稿,只需在最后发送()它,这就像SMTP-MSA允许更多的灵活性.
标签:python,gmail-api,mime-message 来源: https://codeday.me/bug/20190628/1312841.html