1

I am using this interface: https://developers.google.com/gmail/api/v1/reference/users/messages/send#examples

I was testing API request from the form on the website. It requires raw Base64 encoded string such as:

TUlNRS1WZXJzaW9uOiAxLjANClJlY2VpdmVkOiBieSAxMC4xOTQuNjguMjI3IHdpdGggSFRUUDsgV2VkLCAyNCBBdWcgMjAxNiAwMjo1OToxNyAtMDcwMCAoUERUKQ0KRGF0ZTogV2VkLCAyNCBBdWcgMjAxNiAxMTo1OToxNyArMDIwMA0KRGVsaXZlcmVkLVRvOiBrcnVub0B1bGl4dHJhdmVsLmNvbQ0KTWVzc2FnZS1JRDogPENBSzB5QmZZeEgwS1dGbng2TzNfZ3ZNRkxtVlB5RVY9NjdnQW5qRWdPaHIzSGZOTkJCUUBtYWlsLmdtYWlsLmNvbT4NClN1YmplY3Q6IFRoaXMgaXMgYSBzdWJqZWN0DQpGcm9tOiBLcnVub3NsYXYgVUxJWFRyYXZlbCA8a3J1bm9AdWxpeHRyYXZlbC5jb20-DQpUbzogS3J1bm9zbGF2IEJhbm92YWMgPGt1bm9rZGV2QGdtYWlsLmNvbT4NCkNvbnRlbnQtVHlwZTogbXVsdGlwYXJ0L2FsdGVybmF0aXZlOyBib3VuZGFyeT0wNDdkN2JhZTQ5NjRhMmQ3MzAwNTNhY2U1NWUxDQoNCi0tMDQ3ZDdiYWU0OTY0YTJkNzMwMDUzYWNlNTVlMQ0KQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PVVURi04DQoNClRoaXMgaXMgYSBib2R5DQoNCi0tMDQ3ZDdiYWU0OTY0YTJkNzMwMDUzYWNlNTVlMQ0KQ29udGVudC1UeXBlOiB0ZXh0L2h0bWw7IGNoYXJzZXQ9VVRGLTgNCg0KPGRpdiBkaXI9Imx0ciI-VGhpcyBpcyBhIGJvZHk8YnI-PC9kaXY-DQoNCi0tMDQ3ZDdiYWU0OTY0YTJkNzMwMDUzYWNlNTVlMS0t

How do I instead use basic parameters (such as subject, sender, reciver, message body) to send mail?

Kunok
  • 8,089
  • 8
  • 48
  • 89

1 Answers1

3

You first build the message with headers likeFrom and Subject as you mentioned, but you have to encode the message before sending it. There is no way around that.

The following is an example in JavaScript, with the use of JQuery:

// Base64-encode the mail and make it URL-safe 
// (replace all "+" with "-" and all "/" with "_")
var encodedMessage = btoa([
  "From: sender@gmail.com\r\n",
  "To: reciever@gmail.com\r\n",
  "Subject: Subject of the message\r\n\r\n",

  "This is the text of the message"
].join("")).replace(/\+/g, '-').replace(/\//g, '_');

$.ajax({
  method: "POST",
  url: "https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token={access_token}",
  contentType: "application/json",
  data: JSON.stringify({           
    raw: encodedMessage
  })
});
Tholle
  • 108,070
  • 19
  • 198
  • 189
  • But I do not understand why do we need to replace these characters? (`+` to `-`, etc...) – Kunok Aug 24 '16 at 10:41
  • 1
    It's a form of base64 encoding called url safe. It is sometimes needed because some characters can not be transferred safely otherwise. [This answer](http://stackoverflow.com/questions/1374753/passing-base64-encoded-strings-in-url) might explain it some. – Tholle Aug 24 '16 at 10:45
  • May I ask you, is JavaScript code example from https://developers.google.com/gmail/api/v1/reference/users/messages/send#examples legit when used for Node.js that is executed from the terminal such as `node script.js` ? – Kunok Aug 24 '16 at 10:48
  • @Kunok Yeah, sure. `gapi.client.gmail.users.messages.send` basically takes care of the `access_token` above, under the hood. It is pretty [difficult to setup](https://developers.google.com/gmail/api/quickstart/nodejs), but it works great. I like to get an `access_token` from the [playground](https://developers.google.com/oauthplayground/) with the Gmail scopes and use that like above, instead. It is easier for testing purposes. – Tholle Aug 24 '16 at 10:52
  • Yeah, I am trying to set this up but I am calling Node script from PHP code and OAuth setup didn't go well for me, http://stackoverflow.com/questions/39118733/when-oauth2-google-api-script-is-called-from-other-script-it-is-not-possible-to – Kunok Aug 24 '16 at 10:54