Python 2.7 将Google文档转换为PDF并发送带有PDF附件的电子邮件

Python 2.7 将Google文档转换为PDF并发送带有PDF附件的电子邮件,python-2.7,google-drive-api,gmail,Python 2.7,Google Drive Api,Gmail,我正在尝试将Google文档转换为PDF(使用驱动API),然后将文件附加到电子邮件(使用Gmail API) 脚本运行,将Google文档转换为PDF,发送带有附件的电子邮件,但PDF附件为空/已损坏 我怀疑问题出在以下行:msg.set\u有效负载(fh.read()) 相关文件:以及 非常感谢您的指导 import base64 import io from apiclient.http import MediaIoBaseDownload from email.mime.text imp

我正在尝试将Google文档转换为PDF(使用驱动API),然后将文件附加到电子邮件(使用Gmail API)

脚本运行,将Google文档转换为PDF,发送带有附件的电子邮件,但PDF附件为空/已损坏

我怀疑问题出在以下行:
msg.set\u有效负载(fh.read())

相关文件:以及

非常感谢您的指导

import base64
import io
from apiclient.http import MediaIoBaseDownload
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase

fileId = '1234'
content_type = 'application/pdf'

response = drive.files().export_media(fileId=fileId, mimeType=content_type)

fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, response)
done = False
while done is False:
    status, done = downloader.next_chunk()
    logging.info("Download %d%%." % int(status.progress() * 100))

message = MIMEMultipart()
message['to'] = 'myemail@gmail.com'
message['from'] = 'myemail@gmail.com'
message['subject'] = 'test subject'

msg = MIMEText('test body')
message.attach(msg)

main_type, sub_type = content_type.split('/', 1)
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fh.read()) # i suspect the issue is here

msg.add_header('Content-Disposition', 'attachment', filename='an example file name.pdf')
message.attach(msg)

message_obj = {'raw': base64.urlsafe_b64encode(message.as_string())}

service.users().messages().send(userId="me", body=message_obj).execute()

这次修改怎么样?我认为关于从Google Drive下载的内容,您的脚本是正确的。因此,我想建议修改发送带有附件文件的电子邮件的脚本

我认为
msg.set\u payload(fh.read())
是您所说的修改点之一。因此,
getvalue()
检索到的数据被
email.encoders.encode\u base64()
转换。我还修改了
message\u obj

修改脚本: 请修改如下

发件人: 致: 注:
  • 此修改假设您已经能够使用Gmail API发送电子邮件。如果您不能使用Gmail API,请确认范围以及是否在API控制台启用了Gmail API

在我的环境中,我可以确认修改后的脚本工作正常。但是,如果这在您的环境中不起作用,我很抱歉。

我必须做的唯一更改是发送消息。base64.urlsafe_b64encode(message.as_bytes()).decode()为我返回了一个属性错误,但是base64.urlsafe_b64encode(message.as_string())的原始代码工作正常。Python版本中可能存在差异。非常感谢你的帮助@beano感谢您的测试和添加信息。我很高兴你的问题解决了。也谢谢你。
msg = MIMEText('test body')
message.attach(msg)

main_type, sub_type = content_type.split('/', 1)
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fh.read()) # i suspect the issue is here

msg.add_header('Content-Disposition', 'attachment', filename='an example file name.pdf')
message.attach(msg)

message_obj = {'raw': base64.urlsafe_b64encode(message.as_string())}

service.users().messages().send(userId="me", body=message_obj).execute()
from email import encoders  # Please add this.

msg = MIMEText('test body')
message.attach(msg)

main_type, sub_type = content_type.split('/', 1)
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fh.getvalue())  # Modified
encoders.encode_base64(msg)  # Added

msg.add_header('Content-Disposition', 'attachment', filename='an example file name.pdf')
message.attach(msg)

message_obj = {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}  # Modified

service.users().messages().send(userId="me", body=message_obj).execute()