Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to `insert` a `full` format message using the Gmail API?

I've had success cloning a 'raw' formatted message by using get with format='raw' and re-inserting it just like this:

params = {'userId':'me", 'id': msg_id, 'format':'raw'}
mesg = service.users().messages().get(**params).execute()

body = {'raw': mesg['raw']}
service.users().messages().insert(userId='me', body=**body).execute()

But I'm keen to do the same thing using the json format that get can alternatively return via format='full'. Something like this:

params = {'userId':'me", 'id': msg_id, 'format':'full'}
mesg = service.users().messages().get(**params).execute()

body = mesg
service.users().messages().insert(userId='me', body=**body).execute()

See below for the format of mesg[1]. Doing the above gives me the error:

HttpError: <HttpError 400 when requesting 
  https://www.googleapis.com/gmail/v1/users/me/messages?alt=json 
  returned "
  'raw' RFC822 payload message string or uploading message via /upload/* 
   URL required
">

So the question is:

How do I insert a message via the 'full' json format?

The format is 'full' not 'raw' so should I use the upload url? How? Do we continue with a json payload in the raw or body somehow?
Can we convert it to raw format then do same as before?
Should I try and work out how to use the upload with this format?
Should I give it up and work with the raw format?
Will I just hear crickets in response to this question?
So many questions.


The messages get documentation is here
The messages insert documentation is here

[1] A full format get returns this kind of thing. And this is what I'm hoping to insert somehow.

{u'historyId': u'5226', u'id': u'148af993efc00bce',
 u'snippet': u'Hi Joe Get the official Gmail app The best features of Gmail are only available on your phone and',
 u'sizeEstimate': 4809, u'threadId': u'148af993efc00bce', u'labelIds': [u'INBOX'],
 u'payload': {u'mimeType': u'multipart/alternative', u'headers': [
      {u'name': u'MIME-Version', u'value': u'1.0'},
      {u'name': u'x-no-auto-attachment', u'value': u'1'},
      {u'name':
           {u'historyId': u'5226',
            u'id': u'148af993efc00bce',
            u'snippet': u'Hi Joe Get the official Gmail app The best features of Gmail are only available on your phone and',
            u'sizeEstimate': 4809,
            u'threadId': u'148af993efc00bce',
            u'labelIds': [u'INBOX'], u'payload': {
               u'mimeType': u'multipart/alternative',
               u'headers': [{u'name': u'MIME-Version',
                             u'value': u'1.0'}, {
                                u'name': u'x-no-auto-attachment',
                                u'value': u'1'},
                            {u'name': u'Received',
                             u'value': u'by 10.31.41.213; Thu, 25 Sep 2014 18:35:28 -0700 (PDT)'},
                            {u'name': u'Date',
                             u'value': u'Thu, 25 Sep 2014 18:35:28 -0700'},
                            {u'name': u'Message-ID',
                             u'value': u'<CAJvL7e8jz9WYNUjHgnmYcyFgySXxjLiH1zjMxOfopURZmAy4iA@mail.gmail.com>'},
                            {u'name': u'Subject',
                             u'value': u'The best of Gmail, wherever you are'},
                            {u'name': u'From',
                             u'value': u'Gmail Team <[email protected]>'},
                            {u'name': u'To',
                             u'value': u'Joe Test <[email protected]>'},
                            {u'name': u'Content-Type',
                             u'value': u'multipart/alternative; boundary=bcaec547c84f9cba4a0503edee6b'}],
               u'parts': [{u'mimeType': u'text/plain',
                           u'headers': [
                               {u'name': u'Content-Type',
                                u'value': u'text/plain; charset=UTF-8'},
                               {
                                   u'name': u'Content-Transfer-Encoding',
                                   u'value': u'quoted-printable'}],
                           u'body': {
                               u'data': u'IFRoZSBiZXN0IG9mIEdtYWlsLCB3aGVyZ...
like image 313
John Mee Avatar asked Sep 30 '22 14:09

John Mee


1 Answers

You cannot insert a FULL formatted message. If you use the /upload URL then you need an uploadType string and you should have a content-type of message/rfc822. If you're not using /upload then you simply post something like:

{
  'message': 
  {
     'raw': base64url("From: me\r\nTo: someguy\r\nSubject: here it is\r\n\r\nbody after blank line.")
  }
}

you can use attachments but then you'll likely want some mime email libraries to help you generate that email message string that gets base64url encoded in the raw field.

like image 146
Eric D Avatar answered Oct 05 '22 07:10

Eric D