简体   繁体   English

通过Python API中的Gmail API发送大量电子邮件

[英]Send large email via Gmail API in Python

Recently Google disabled their "legacy" upload capability, so I was forced to change our applications to use the Google users().messages().send api. 最近谷歌禁用了他们的“遗留”上传功能,因此我被迫更改我们的应用程序以使用Google用户()。messages()。发送api。

However it doesn't work on anything over 5mb. 但它不适用于超过5mb的任何东西。 I've seen one post on the www about how to do it in PHP, but nothing in Python. 我在www上看过一篇关于如何用PHP做的文章,但在Python中没有。

Here's my existing code: 这是我现有的代码:

http        =   #My login routine
service     =   build('gmail','v1',http=http)
email       =   {'raw':base64.urlsafe_b64encode(message)}
reply       =   (service.users().messages().send(userId=user,body=email).execute())

Google has extensive documentation, but NOT on the specific ability to send large emails in Python. Google提供了大量文档,但没有提供使用Python发送大型电子邮件的具体功能。 In fact at this page: 实际上在这个页面:

https://developers.google.com/gmail/api/v1/reference/users/messages/send https://developers.google.com/gmail/api/v1/reference/users/messages/send

They talk about using the base API to send messages as large as 35MB, but that simply doesn't work. 他们谈论使用基本API发送大小为35MB的消息,但这根本不起作用。 I receive a "ResponseNotReady()" error. 我收到“ResponseNotReady()”错误。

I've been reading about chunked uploads, resumable uploads, but nothing has come to fruition. 我一直在阅读关于分块上传,可恢复上传的内容,但没有任何结果。

Any thoughts out there? 有什么想法吗?

I had the same issue two years ago , and I still think the only way to send messages with large attachments is to circumvent the official client library. 我有同样的问题,两年前 ,我仍然认为与大附件发送消息的唯一方法是绕过官方的客户端库。

This is an example using the requests library instead: 这是使用请求库的示例:

url = 'https://www.googleapis.com/gmail/v1/users/me/messages/send?uploadType=multipart'
headers = { 
  'Authorization': 'Bearer ' + accessToken, 
  'Content-Type': 'message/rfc822'
}
requests.post(url, headers=headers, payload=message)

The message is not encoded. 邮件未编码。 I think you can extract the access token from the service object, but I'm not sure exactly how. 我认为你可以从服务对象中提取访问令牌,但我不确定如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM