0

I am trying to upload an email to gmail account using gmail REST API for import message https://developers.google.com/gmail/api/v1/reference/users/messages/import#http-request using python requests module. email is in RAW format (i.e. data of .eml file encoded with base64.b64encode()) with body

POST https://www.googleapis.com/gmail/v1/users/me/messages/import
{
 "raw": ".........."
}

This is working for RAW data size up to few MBs (say 5MB), but I am getting 'Connection Timeout' error if i try to upload bigger data (~10 MB or more).

Can anybody tell me the way to upload bigger email data using this API.

I am using OAuth access_token to call these APIs

1 Answer 1

0

Try setting the uploadType to resumable

Resumable upload

To upload data files more reliably, you can use the resumable upload protocol. This protocol allows you to resume an upload operation after a communication failure has interrupted the flow of data. It is especially useful if you are transferring large files and the likelihood of a network interruption or some other transmission failure is high, for example, when uploading from a mobile client app. It can also reduce your bandwidth usage in the event of network failures because you don't have to restart large file uploads from the beginning.

Example: Resumable session initiation request

POST /upload/gmail/v1/users/userId/messages/send?uploadType=resumable HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer your_auth_token
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: message/rfc822
X-Upload-Content-Length: 2000000

{
"id": string,
"threadId": string,
"labelIds": [
string
],
"snippet": string,
"historyId": unsigned long,
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
"sizeEstimate": integer,
"raw": bytes
}
Sign up to request clarification or add additional context in comments.

2 Comments

@mr-rebot Thanks for the example, I am not trying to send mail but trying to restore/import existing mail with RAW eml stream. While importing using /upload and uploadType=resumable), I am getting error as {u'error': {u'code': 400, u'message': u"Expected a single 'From' header", u'errors': [{u'domain': u'global', u'message': u"Expected a single 'From' header", u'reason': u'invalidArgument'}]}}
@mr-rebot, I have tried this example (send mail). It is returning me empty response with status code 200. I am expecting response like below but getting empty. i.e. no location/session_uri in response. HTTP/1.1 200 OK Location: googleapis.com/upload/gmail/v1/users/userId/messages/… Content-Length: 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.