Class GmailMessage

GmailMessage

A message in a user's Gmail account.

Methods

MethodReturn typeBrief description
createDraftReply(body)GmailDraftCreates a draft message replying to the sender of this message using the reply-to address.
createDraftReply(body, options)GmailDraftCreates a draft message replying to the sender of this message using the reply-to address, with optional arguments.
createDraftReplyAll(body)GmailDraftCreates a draft message replying to the sender using the reply-to address and all recipients of this message.
createDraftReplyAll(body, options)GmailDraftCreates a draft message replying to the sender of this message using the reply-to address and all recipients, with optional arguments.
forward(recipient)GmailMessageForwards this message to new recipients.
forward(recipient, options)GmailMessageForwards this message to new recipients, with optional arguments.
getAttachments()GmailAttachment[]Gets all the attachments for this message.
getBcc()StringGets the comma-separated recipients bcc'd on this message.
getBody()StringGets the HTML content of the body of this message.
getCc()StringGets the comma-separated recipients cc'd on this message.
getDate()DateGets the date and time of this message.
getFrom()StringGets the sender of this message.
getId()StringGets the ID of this message.
getPlainBody()StringGets the content of the body of this message without HTML formatting.
getRawContent()StringGets the raw content of this message.
getReplyTo()StringGets the reply-to address of this message (usually the sender).
getSubject()StringGets the subject of this message.
getThread()GmailThreadGets the thread that contains this message.
getTo()StringGets the comma-separated recipient(s) of this message.
isDraft()BooleanGets whether this message is a draft.
isInChats()BooleanGets whether this message is a chat.
isInInbox()BooleanGets whether this message is in the inbox.
isInPriorityInbox()BooleanReturns true if this message is in the priority inbox; returns false otherwise.
isInTrash()BooleanGets whether this message is in the trash.
isStarred()BooleanGets whether this message is starred.
isUnread()BooleanGets whether this message is unread.
markRead()GmailMessageMarks the message as read.
markUnread()GmailMessageMarks the message as unread.
moveToTrash()GmailMessageMoves the message to the trash.
refresh()GmailMessageReloads this message and associated state from Gmail (useful in case the labels, read state, etc., have changed).
reply(body)GmailMessageReplies to the sender of this message using the reply-to address.
reply(body, options)GmailMessageReplies to the sender of this message using the reply-to address, with optional arguments.
replyAll(body)GmailMessageReplies to the sender using the reply-to address and all recipients of this message.
replyAll(body, options)GmailMessageReplies to the sender of this message using the reply-to address and all recipients, with optional arguments.
star()GmailMessageStars the message.
unstar()GmailMessageUnstars the message.

Detailed documentation

createDraftReply(body)

Creates a draft message replying to the sender of this message using the reply-to address. The size of the email (including headers) is quota limited.

// Create a draft reply to the original message with an acknowledgment.
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
message.createDraftReply("Got your message");

Parameters

NameTypeDescription
bodyStringthe body of the email

Return

GmailDraft — the newly created draft message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


createDraftReply(body, options)

Creates a draft message replying to the sender of this message using the reply-to address, with optional arguments.

The email can contain both plain text and an HTML body. The size of the email (including headers) is quota limited.

// Create a draft response with an HTML text body.
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
message.createDraftReply("incapable of HTML", {
  htmlBody: "<b>some HTML body text</b>",
  cc: "another@example.com"
});

Parameters

NameTypeDescription
bodyStringthe body of the email
optionsObjecta JavaScript object that specifies advanced parameters, as listed below

Advanced parameters

NameTypeDescription
attachmentsBlobSource[]an array of files to send with the email
bccStringa comma-separated list of email addresses to BCC
ccStringa comma-separated list of email addresses to CC
fromStringthe address that the email should be sent from, which must be one of the values returned by GmailApp.getAliases()
htmlBodyStringif set, devices capable of rendering HTML will use it instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email
inlineImagesObjecta JavaScript object containing a mapping from image key (String) to image data (BlobSource); this assumes that the htmlBody parameter is used and contains references to these images in the format <img src="cid:imageKey" />
nameStringthe name of the sender of the email (default: the user's name)
replyToStringan email address to use as the default reply-to address (default: the user's email address)
subjectStringa new subject line for the email

Return

GmailDraft — the newly created draft message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


createDraftReplyAll(body)

Creates a draft message replying to the sender using the reply-to address and all recipients of this message. The size of the email (including headers) is quota limited.

// Create a draft response to all recipients (except those bcc'd) with an acknowledgment.
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
message.createDraftReplyAll("Got your message");

Parameters

NameTypeDescription
bodyStringthe body of the email

Return

GmailDraft — the newly created draft message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


createDraftReplyAll(body, options)

Creates a draft message replying to the sender of this message using the reply-to address and all recipients, with optional arguments.

The email can contain both plain text and an HTML body. The size of the email (including headers) is quota limited.

// Create a draft response to all recipients (except those bcc'd) using an HTML text body.
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages[0];
message.createDraftReplyAll("incapable of HTML", {
  htmlBody: "<b>some HTML body text</b>",
  cc: "another@example.com"
});

Parameters

NameTypeDescription
bodyStringthe body of the email
optionsObjecta JavaScript object that specifies advanced parameters, as listed below

Advanced parameters

NameTypeDescription
attachmentsBlobSource[]an array of files to send with the email
bccStringa comma-separated list of email addresses to BCC
ccStringa comma-separated list of email addresses to CC
fromStringthe address that the email should be sent from, which must be one of the values returned by GmailApp.getAliases()
htmlBodyStringif set, devices capable of rendering HTML will use it instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email
inlineImagesObjecta JavaScript object containing a mapping from image key (String) to image data (BlobSource); this assumes that the htmlBody parameter is used and contains references to these images in the format <img src="cid:imageKey" />
nameStringthe name of the sender of the email (default: the user's name)
replyToStringan email address to use as the default reply-to address (default: the user's email address)
subjectStringa new subject line for the email

Return

GmailDraft — the newly created draft message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


forward(recipient)

Forwards this message to new recipients. The size of the email (including headers) is quota limited.

// Forward first message of first inbox thread to recipient1 & recipient2, both @example.com
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
message.forward("recipient1@example.com,recipient2@example.com");

Parameters

NameTypeDescription
recipientStringa comma-separated list of email addresses

Return

GmailMessage — this message, useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


forward(recipient, options)

Forwards this message to new recipients, with optional arguments.

The email can contain both plain text, and also an HTML body. The size of the email (including headers) is quota limited.

var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
message.forward("recipient1@example.com,recipient2@example.com", {
  cc: "myboss@example.com",
  bcc: "mybosses-boss@example.com,vp@example.com"
});

Parameters

NameTypeDescription
recipientStringa comma-separated list of email addresses
optionsObjecta JavaScript object that specifies advanced parameters, as listed below

Advanced parameters

NameTypeDescription
attachmentsBlobSource[]an array of files to send with the email
bccStringa comma-separated list of email addresses to BCC
ccStringa comma-separated list of email addresses to CC
fromStringthe address that the email should be sent from, which must be one of the values returned by GmailApp.getAliases()
htmlBodyStringif set, devices capable of rendering HTML will use it instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email
inlineImagesObjecta JavaScript object containing a mapping from image key (String) to image data (BlobSource); this assumes that the htmlBody parameter is used and contains references to these images in the format <img src="cid:imageKey" />
nameStringthe name of the sender of the email (default: the user's name)
noReplyBooleantrue if the email should be sent from a generic no-reply email address to discourage recipients from responding to emails; this option is only possible for G Suite accounts, not Gmail users
replyToStringan email address to use as the default reply-to address (default: the user's email address)
subjectStringa new subject line for the email

Return

GmailMessage — this message, useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


getAttachments()

Gets all the attachments for this message.

Return

GmailAttachment[] — an array of Blob attachments for this message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

getBcc()

Gets the comma-separated recipients bcc'd on this message.

This will be empty for all received messages, by definition.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log(message.getBcc()); // Log bcc'd addresses

Return

String — the comma-separated recipients bcc'd on this message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


getBody()

Gets the HTML content of the body of this message.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log(message.getBody()); // Log contents of the body

Return

String — the body content of this message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


getCc()

Gets the comma-separated recipients cc'd on this message.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log(message.getCc()); // Log cc'd addresses

Return

String — the comma-separated recipients cc'd on this message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


getDate()

Gets the date and time of this message.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log(message.getDate()); // Log date and time of the message

Return

Date — the date and time of this message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

getFrom()

Gets the sender of this message.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log(message.getFrom()); // Log from address of the message

Return

String — the email address of the message sender

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


getId()

Gets the ID of this message.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
var id = message.getId();
var messageById = GmailApp.getMessageById(id);
Logger.log(message.getSubject() == messageById.getMessage()); // Always logs true

Return

String — the message ID


getPlainBody()

Gets the content of the body of this message without HTML formatting. This is more complex than getBody() and will take longer.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log(message.getPlainBody()); // Log contents of the body

Return

String — the plain body content of this message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


getRawContent()

Gets the raw content of this message. This is equivalent to "Show Original" in the Gmail UI.

Return

String — the raw content of this message.

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

getReplyTo()

Gets the reply-to address of this message (usually the sender).

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log(message.getReplyTo()); // Logs reply-to address

Return

String — the email address for replies

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


getSubject()

Gets the subject of this message.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log(message.getSubject()); // Log subject line

Return

String — the subject of this message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


getThread()

Gets the thread that contains this message.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log(message.getThread().getFirstMessageSubject() ==
           thread.getFirstMessageSubject()); // Always logs true

Return

GmailThread — the GmailThread that contains this message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

getTo()

Gets the comma-separated recipient(s) of this message.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log(message.getTo()); // Log the recipient of message

Return

String — the comma-separated recipient(s) of this message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


isDraft()

Gets whether this message is a draft.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log("is draft? " + message.isDraft());

Return

Boolean — whether this message is a draft

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

isInChats()

Gets whether this message is a chat.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log("is a chat? " + message.isInChats());

Return

Boolean — whether this message is a chat


isInInbox()

Gets whether this message is in the inbox.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log("is in inbox? " + message.isInInbox());

Return

Boolean — whether this message is in the inbox

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

isInPriorityInbox()

Returns true if this message is in the priority inbox; returns false otherwise.

var thread = GmailApp.getPriorityInboxThreads(0,1)[0]; // Get first thread in priority inbox
var messages = thread.getMessages();
for (var i = 0; i < messages.length; i++) {
  // At least one of the messages is in priority inbox
  Logger.log("is in priority inbox? " + messages[i].isInPriorityInbox());
}

Return

Boolean — whether this message is in the priority inbox

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

isInTrash()

Gets whether this message is in the trash.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log("is in the trash? " + message.isInTrash());

Return

Boolean — whether this message is in the trash


isStarred()

Gets whether this message is starred.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log("is starred? " + message.isStarred());

Return

Boolean — whether this message is starred


isUnread()

Gets whether this message is unread.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
Logger.log("is unread? " + message.isUnread());

Return

Boolean — the unread status of this message

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

markRead()

Marks the message as read.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
message.markRead(); // Mark as read

Return

GmailMessage — this GmailMessage, useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


markUnread()

Marks the message as unread.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
message.markUnread(); // Mark as unread

Return

GmailMessage — this GmailMessage, useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


moveToTrash()

Moves the message to the trash.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
message.moveToTrash(); // Move message to trash

Return

GmailMessage — this GmailMessage, useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


refresh()

Reloads this message and associated state from Gmail (useful in case the labels, read state, etc., have changed).

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
// .. Do bunch of stuff here
message.refresh(); // Make sure it's up to date
// Do more stuff to message

Return

GmailMessage — this message for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

reply(body)

Replies to the sender of this message using the reply-to address. The size of the email (including headers) is quota limited.

// Respond to author of message with acknowledgment
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
message.reply("Got your message");

Parameters

NameTypeDescription
bodyStringthe body of the email

Return

GmailMessage — this message, useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


reply(body, options)

Replies to the sender of this message using the reply-to address, with optional arguments.

The email can contain both plain text, and also an HTML body. The size of the email (including headers) is quota limited.

// Respond with HTML body text
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
message.reply("incapable of HTML", {
  htmlBody: "<b>some HTML body text</b>",
  noReply: true
});

Parameters

NameTypeDescription
bodyStringthe body of the email
optionsObjecta JavaScript object that specifies advanced parameters, as listed below

Advanced parameters

NameTypeDescription
attachmentsBlobSource[]an array of files to send with the email
bccStringa comma-separated list of email addresses to BCC
ccStringa comma-separated list of email addresses to CC
fromStringthe address that the email should be sent from, which must be one of the values returned by GmailApp.getAliases()
htmlBodyStringif set, devices capable of rendering HTML will use it instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email
inlineImagesObjecta JavaScript object containing a mapping from image key (String) to image data (BlobSource); this assumes that the htmlBody parameter is used and contains references to these images in the format <img src="cid:imageKey" />
nameStringthe name of the sender of the email (default: the user's name)
noReplyBooleantrue if the email should be sent from a generic no-reply email address to discourage recipients from responding to emails; this option is only possible for G Suite accounts, not Gmail users
replyToStringan email address to use as the default reply-to address (default: the user's email address)
subjectStringa new subject line for the email

Return

GmailMessage — this message, useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


replyAll(body)

Replies to the sender using the reply-to address and all recipients of this message. The size of the email (including headers) is quota limited.

// Respond to all recipients (except bcc'd) of last email in thread with acknowledgment
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
message.replyAll("Got your message");

Parameters

NameTypeDescription
bodyStringthe body of the email

Return

GmailMessage — this message,useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


replyAll(body, options)

Replies to the sender of this message using the reply-to address and all recipients, with optional arguments.

The email can contain both plain text, and also an HTML body. The size of the email (including headers) is quota limited.

// Respond with HTML body text
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages[0];
messageThread.replyAll("incapable of HTML", {
  htmlBody: "<b>some HTML body text</b>",
  noReply: true
});

Parameters

NameTypeDescription
bodyStringthe body of the email
optionsObjecta JavaScript object that specifies advanced parameters, as listed below

Advanced parameters

NameTypeDescription
attachmentsBlobSource[]an array of files to send with the email
bccStringa comma-separated list of email addresses to BCC
ccStringa comma-separated list of email addresses to CC
fromStringthe address that the email should be sent from, which must be one of the values returned by GmailApp.getAliases()
htmlBodyStringif set, devices capable of rendering HTML will use it instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email
inlineImagesObjecta JavaScript object containing a mapping from image key (String) to image data (BlobSource); this assumes that the htmlBody parameter is used and contains references to these images in the format <img src="cid:imageKey" />
nameStringthe name of the sender of the email (default: the user's name)
noReplyBooleantrue if the email should be sent from a generic no-reply email address to discourage recipients from responding to emails; this option is only possible for G Suite accounts, not Gmail users
replyToStringan email address to use as the default reply-to address (default: the user's email address)
subjectStringa new subject line for the email (250 characters maximum)

Return

GmailMessage — this message, useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


star()

Stars the message.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
message.star(); // Star the message

Return

GmailMessage — this GmailMessage, useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also


unstar()

Unstars the message.

var thread = GmailApp.getInboxThreads(0,1)[0]; // Get first thread in inbox
var message = thread.getMessages()[0]; // Get first message
message.unstar(); // Unstar the message

Return

GmailMessage — this GmailMessage, useful for chaining

Authorization

Scripts that use this method require authorization with one or more of the following scopes or appropriate scopes from the related REST API:

  • https://mail.google.com/

See also

Send feedback about...

Apps Script
Apps Script
Need help? Visit our support page.