3
votes

I am attempting to use Gmail's Resumable option for uploading attachments to an email. Documentation reference: https://developers.google.com/gmail/api/guides/uploads#resumable.

Currently I am able to send the email with the resumable URI, but without an attachment (using Postman). Documentation doesn't provide very clear examples of what the request should specifically look like, and there don't seem to be many examples after scouring the internet.

My requests are in two parts:

  1. Initial request -

    Request URL:
    POST /upload/gmail/v1/users/me/messages/send?uploadType=resumable
    Host: www.googleapis.c om (can't post links so I interrupted the url)
    
    Headers: 
    Authorization: Bearer my_token_here
    Content-Length: 113
    Content-Type: application/json
    X-Upload-Content-Length: 67
    X-Upload-Content-Type: message/rfc822
    
    Body:
    {"raw":"VG86IG5pcnZhbmEucm9ja2VyQGdtYWlsLmNvbQpTdWJqZWN0OiBUZXN0RW1haWxTdWJqZWN0MwoKTWVzc2FnZSBjb250ZW50cyAjMy4"}
    

The body is a 64bit encoded string that include the To, Subject, and email message contents. Then gmail returns a response with an empty body, and a 'location' header that looks like the following: googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=resumable&upload_id=BRnB2UoAsKwzNMoQAy-JtmP6mu5agltqOWZ9uerI3k-KNTDJ73PWEjKuAHpko4RN6weSEysddH2kjj4G24uFw6E9oPv1XP69l7_KcmNuW-RAoz_5oS1T_4_E. (removed https:// because this account can only have one link in a post)

I then follow up with a PUT request to that URL returned in the location header.

  1. The second request looks like the following:

    Request URL: 
    PUT /upload/gmail/v1/users/me/messages/send?uploadType=resumable&upload_id=BRnB2UoAsKwzNMoQAy-JtmP6mu5agltqOWZ9uerI3k-KNTDJ73PWEjKuAHpko4RN6weSEysddH2kjj4G24uFw6E9oPv1XP69l7_KcmNuW-RAoz_5oS1T_4_E
    Host: www.googleapis.c om
    
    Headers: 
    Content-Length: 67
    Content-Type: message/rfc822
    
    Body:
    {"raw":"VG86IG5pcnZhbmEucm9ja2VyQGdtYWlsLmNvbQpTdWJqZWN0OiBUZXN0RW1haWxTdWJqZWN0MwoKTWVzc2FnZSBjb250ZW50cyAjMy4"}
    --- OR ---
    I choose the binary option, and attach the file I am looking to upload via Postman.
    

I receive a response from Gmail with an object like this:

    {
      "id": "159d7ded3125e255",
      "threadId": "159d7ded3125e255",
      "labelIds": [
        "SENT"
      ]
    }

And an email is sent successfully, however there isn't an attachment with the email. When I show the original email in Gmail, there isn't any evidence of an attachment. Original looks like the following:

    Received: from 325276275830 named unknown by gmailapi.google.com with HTTPREST; Wed, 25 Jan 2017 15:03:33 -0800
    To: [email protected]
    Subject: TestEmailSubject3
    Date: Wed, 25 Jan 2017 15:03:33 -0800
    Message-Id: <CEROA6F=0ohk33RD9XyC_gW1DZO88xYF4bXYqrCSct62MUuytDw@mail.gmail.com>
    From: [email protected]

    Message contents #3.

What am I missing? Do I need to encode some particular contents in a different way, or put some data in a different location? I'm not receiving any errors. I've been working on this for a few days now and I just can't figure it out.

1
Try using the code in the related SO post, it is in multipart but it may help you have an idea how attachment works. Hope this helps.Mr.Rebot
Mr Rebot - Thanks for the response. I still haven't come to a working solution. I have been able to get a resumable request to upload a file and email it successfully - however this is when the initiation request is empty and I sent a fully composed message/rfc822 formatted message in the following request, including a base64 encoded file.halldr
Content-Type: multipart/mixed; boundary=foo_bar_baz MIME-Version: 1.0 to: [email protected] from: [email protected] subject: File with attachment - Resumable Upload --foo_bar_baz Content-Type: text/plain; charset=UTF-8 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit This is supposed to be the email message! --foo_bar_baz Content-Type: image/jpeg MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=BIG_PUPPY_PIC.jpg encoded_file_went_here_jkl4fliq43jga43gai --foo_bar_baz--halldr
That is what my message body looks like in the request - sorry that its ugly, comments won't let me format correctly. This follows the same syntax as my original example in my question. It is important that I am able to upload the attachment by sending the metadata for the email in the original request, and then send ONLY file contents in the following request. The gmail documentation explicitly demonstrates this syntax in their example requests. There are a surprising lack of examples of people looking this up online. Even here on SO, there are only a few questions, with no answers.halldr

1 Answers

0
votes

I ran into the same problem, i made it work by using Nodemailer to create the email with the attachments, save the result to a file, then upload it with this.