Gmail API playground: Send method, converted MIME raw header not populating email fields on send

2.7k views Asked by At

I'm using the Google OAuth 2.0 Playground and attempting to send an email. The autentication is working fine. Here is the very simple message I'm trying to send (email address changed to prevent spam):

 to: 'Michael To' <[email protected]>
 from: 'John From' <[email protected]>
 subject: 'Test Message'
 htmlBody: '<b>HI!</b><br>Test Message'

I convert that to Base64 RFC 822 using VBA which gets me this (I've tried swapping the "+" and the "-" per other StackOverflow posts but to no avail):

dG86ICdNaWNoYWVsIFRvJyA8RmFrZU1pY2hhZWxAZ21haWwuY29tPg1mcm9tOiAnSm9obiBGcm9tJyA8Sm9obkZAbXlkb21haW4uY29tPg1zdWJqZWN0OiAnVGVzdCBNZXNzYWdlJw1odG1sQm9keTogJzxiPkhJITwvYj48YnI-VGVzdCBNZXNzYWdlJw==

In the Playground my method is POST and I've added 2 headers:

raw: and the Base64 string above (no quotes or anything)
Content-Type: message/rfc822 <I added this because I kept getting an a different error. Putting this prevetned that error>

Request URI (removed the https cause SO won't let me post more than 2 links)://www.googleapis.com/upload/gmail/v1/users/me/messages/send

I click "send the request" and get an OK response:

Here is my request:

POST /upload/gmail/v1/users/me/messages/send HTTP/1.1
Host: www.googleapis.com
Raw: <string above>
Content-length: 0
Content-type: message/rfc822
Authorization: Bearer <my token>

Response:

HTTP/1.1 200 OK
Alternate-protocol: 443:quic,p=1
Content-length: 91
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Vary: Origin, X-Origin
Server: UploadServer ("Built on Jun 6 2015 11:14:45 (1433614485)")
Etag: "YAnoF_dHYOakPARISZQhTvRsqto/nwevNUuzaUU_lB19L-UhrwaaUSM"
Pragma: no-cache
Cache-control: no-cache, no-store, max-age=0, must-revalidate
Date: Wed, 10 Jun 2015 15:59:36 GMT
Content-type: application/json; charset=UTF-8
{
  "labelIds": [
    "SENT"
  ], 
  "id": "14dde32bc92c9XYZ", 
  "threadId": "14dde32bc92c9XYZ"
}

However, when I go to my Gmail sent mail folder, the message is there but nothing is in the To, Subject, or Body is field: See Screenshot

I have to imagine this is something simple, but as I'm new to the Google Gmail API, MIME, and dealing with Raw Base64 stuff, I'm not having much luck.

Thanks in advance for any assistance.

--- EDIT PER THOLLE'S Response ---

That helps! I removed the raw base64 string header and put:

From: 'John From' <[email protected]>
Subject: Test Message
To: 'Michael To' <[email protected]>
Test Message

into the "Enter request body" and it sends, which is great!

Three Follow up questions:

  1. Are there any security risks or limitations (max length? I see there might be a 2mb limitation but that would be a lot of text.) sending it this way (in the body) opposed to a raw Base64 string in the header?
  2. (I'll dig more on this) How do I make the message body HTML? Does the content type of "Content-Type: message/rfc822" prevent me from being able to send it HTML? Sending it HTML is a requirement for this application and I can't have two content types, is there an HTML parameter I can use or am I out of luck?
  3. (I'll do homework on this as well) How do I include an attachment, say a PDF file, with the email?

Thanks again!

1

There are 1 answers

3
Tholle On BEST ANSWER

I think you are violating a few minor details of the RFC 822 standard:

It is recommended that, if present, headers be sent in the order "Return- Path", "Received", "Date", "From", "Subject", "Sender", "To", "cc", etc.

I can't find it for the life of me, but I also think that the headers has to have their first character capitalized. Try this:

From: John From <[email protected]>
Subject: Test Subject
To: Michael To <[email protected]>

Test Message

You also don't want to send the base64-encoded mail if you choose message/rfc822 as your Content-Type. Just supply the example mail above as is.

POST /upload/gmail/v1/users/me/messages/send HTTP/1.1
Host: www.googleapis.com
Content-length: 108
Content-type: message/rfc822
Authorization: Bearer {YOUR_ACCESS_TOKEN}

From: John From <[email protected]>
Subject: Test Subject
To: Michael To <[email protected]>

Test Message

If you want HTML, just modify your message to this:

From: John From <[email protected]>
Subject: Test Subject
To: Michael To <[email protected]>
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<b>Test Message</b>