Search code examples
google-apps-scriptgoogle-apigmailsendmailurlfetch

Send email using GMail API in Google Apps Script


I have a raw email (tested on playground and working) and I want to send it with Google's Gmail API from Google Apps Script.

I can't find the right syntax for the request:

var RequestUrl = "https://www.googleapis.com/gmail/v1/users/emailAccount/messages/send";

var RequestArguments = {
    muteHttpExceptions:true,
    headers: {Authorization: 'Bearer ' + token
             'GData-Version': '3.0',
             'Content-Type': "message/rfc822",
             },
    payload: {"raw":raw},
    method:"post"
  };    

var result = UrlFetchApp.fetch(RequestUrl,RequestArguments);

What is wrong with my syntax?


Solution

  • I found the solution to my question:

    var RequestArguments = {
      headers: {Authorization: 'Bearer ' + token},
      method: "post",
      contentType: "application/json",
      payload: JSON.stringify(jsonMessage)
    };
    

    jsonMessage is the whole message, not only the raw part!