How to get message ID before or after sending email in gmail?

1k views Asked by At

I am working on a Chrome extension for Gmail. I want to obtain the "Message ID" in my extension before the email is sent or just after email is sent. How can I achieve it?

How is message id generated in Gmail?

Is it generated before sending an email or later?

Note: I am not overriding the email functionality. I just need the unique emailID when email is sent in Gmail.

I want to pass this email ID in a request to Gmail API so that I can get the email data as response.

1

There are 1 answers

2
Mr.Rebot On

Based on my experience, it is created after sending an email. You can notice that if there is a draft message, there is an ID but once it is sent this ID is converted to its messageID (different value) which you can be use to search in the inbox. You can check this related SO post, that talks about the difference between messageID and draftID. As an example, Mogzdad's code snippet in Apps Script using Gmail API advance Service return the id after sending the email.

/**
 * 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;
}

Hope this helps.