1

Recently Google disabled their "legacy" upload capability, so I was forced to change our applications to use the Google users().messages().send api.

However it doesn't work on anything over 5mb. I've seen one post on the www about how to do it in PHP, but nothing in 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. In fact at this page:

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. I receive a "ResponseNotReady()" error.

I've been reading about chunked uploads, resumable uploads, but nothing has come to fruition.

Any thoughts out there?

AaronF
  • 41
  • 1
  • 6
  • I had the same problem [two years ago](http://stackoverflow.com/questions/24908700/mail-attachment-wrong-media-type-gmail-api/24957873#24957873), and I had to go outside the official client library (see edit at the end of the answer). In short: Don't encode the message, and use the simple upload url with Content-Type `message/rfc822` instead. – Tholle Jul 27 '16 at 13:13
  • Any thoughts on being able to do the URL format from Python? – AaronF Jul 28 '16 at 11:52

1 Answers1

0

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.

Community
  • 1
  • 1
Tholle
  • 108,070
  • 19
  • 198
  • 189
  • Can you think of a translation from the requests library to the native HTTPSConnection object? I'm loathe to install another library...it's just another lib to manage, though I do see it's benefits. – AaronF Jul 28 '16 at 13:53
  • @AaronF I usually make silly mistakes using a lower level request library, so I don't think I would be of any help, I'm afraid. – Tholle Jul 28 '16 at 13:56
  • Hah, I know what you mean. Thanks anyways :) – AaronF Jul 28 '16 at 15:29