问题描述
我有一个与How do I send HTML Formatted emails,through the gmail-api for python中的最后一个答案有关的问题,但不幸的是,该答案对我不起作用。如果我同时附加“普通”和“ html”部分,则它仅接受我进行的 LAST “附加”呼叫。也就是说,如果我在“ html”之后附加为“纯”,则仅以“普通”发送(在带有HTML呈现的设备/应用上看起来不吸引人)。但是,如果我在“平原”之后附加“ html”,它只会发送“ html”格式(在没有HTML呈现的设备/应用上看起来很糟糕)。与发布该问题的人不同,我确实需要两个部分,因为接收我的电子邮件的某些设备/应用程序不呈现HTML,而是需要纯文本部分。
如果我使用“ smtplib”而不是GMAIL-API,这不是问题,但是我想使用gmail api来提高应用程序的安全性。
这是我的代码:
message = MIMEMultipart('alternative')
message['to'] = to_email
message['from'] = from_email
message['subject'] = subject
body_plain = MIMEText(email_body,'plain')
message.attach(body_plain)
body_html_format="<u><b>html:<br>"+email_body+"</b></u>"
body_html = MIMEText(body_html_format,'html')
message.attach(body_html) # PROBLEM: Will only send as HTML since this was added LAST.
raw_string = base64.urlsafe_b64encode(message.as_bytes()).decode()
request = service.users().messages().send(userId='my.email@gmail.com',body={'raw':raw_string})
message = request.execute()
感谢和问候, 道格
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)