0

I am trying to create the "raw" parameter from the solution that Mogsdad provided here.

I have come to the below solution

/**
 * Send a raw RFC 2822 formatted and base64url encoded email
 * using the Advanced Gmail service.
 *
 * From http://stackoverflow.com/a/35073785/1677912
 *
 * @param {String}  raw  RFC 2822 formatted and base64url encoded message
 *
 * @returns {String}     Message ID of the message (now in Sent Messages).
 */
function sendRawMessage( raw ) {
  var message = Gmail.newMessage();
  message.raw = raw;
  var sentMsg = Gmail.Users.Messages.send(message, 'me');
  return sentMsg.id;
}

function rawMessage(){

  const messageBody = `
  this is a test message
  `;

  var raw = (
  "From: foo@gmail.com\r\n" +
  "To: foo@gmail.com\r\n" +
  "Subject: Subject Text\r\n\r\n" +

  messageBody
);

var encodedMsg = Utilities.base64Encode(raw).replace(/=+$/, ''); //the replace removes the equals after
var myId = sendRawMessage( encodedMsg );
  Logger.log(myId);
}

But when I run the function I get the below error and I can't seem to figure out what the unexpected token is.

API call to gmail.users.messages.send failed with error: Invalid JSON payload received. Unexpected token.

My end goal is to be able to send an email through the Gmail API but not have to "...fuss with the Web API directly."

wjwelch1
  • 79
  • 1
  • 12
  • `sendRawMessage()` is undefined – Cooper Feb 05 '21 at 16:29
  • @Cooper I am using the function that Mogsdad provided in the linked post. I will update my post to include that function so there is no confusion. – wjwelch1 Feb 05 '21 at 16:35
  • It looks that you haven't enabled the Gmail Advanced Service as was instructed in the referred Mogdad's answer. – Rubén Feb 06 '21 at 00:02

0 Answers0