Gmail Service

Gmail

This Service lets you send email, compose drafts, manage labels, mark messages and threads, and conduct a variety of other Gmail account management tasks. See also Mail Service, a simpler service that only allows the sending of email.

Classes

NameBrief description
GmailAppProvides access to Gmail threads, messages, and labels.
GmailAttachmentAn attachment from Gmail.
GmailDraftA user-created draft message in a user's Gmail account.
GmailLabelA user-created label in a user's Gmail account.
GmailMessageA message in a user's Gmail account.
GmailThreadA thread in a user's Gmail account.

GmailApp

Methods

MethodReturn typeBrief description
createDraft(recipient, subject, body)GmailDraftCreates a draft email message.
createDraft(recipient, subject, body, options)GmailDraftCreates a draft email message with optional arguments.
createLabel(name)GmailLabelCreate a new user label of the given name.
deleteLabel(label)GmailAppDeletes the specified label.
getAliases()String[]Gets a list of the emails that are set up as aliases for this account in Gmail.
getDraft(draftId)GmailDraftRetrieve an email message draft by ID.
getDraftMessages()GmailMessage[]Retrieves all draft messages.
getDrafts()GmailDraft[]Gets all Gmail draft messages.
getInboxThreads()GmailThread[]Retrieves all Inbox threads irrespective of labels.
getInboxThreads(start, max)GmailThread[]Retrieves a range of Inbox threads irrespective of labels.
getInboxUnreadCount()IntegerGets the number of unread threads in the inbox.
getMessageById(id)GmailMessageGets a message by ID.
getMessagesForThread(thread)GmailMessage[]Retrieve all messages in the specified thread.
getMessagesForThreads(threads)GmailMessage[][]Retrieve all messages in the specified threads.
getPriorityInboxThreads()GmailThread[]Retrieves all Priority Inbox threads irrespective of labels.
getPriorityInboxThreads(start, max)GmailThread[]Retrieves a range of Priority Inbox threads irrespective of labels.
getPriorityInboxUnreadCount()IntegerGets the number of unread threads in the Priority Inbox.
getSpamThreads()GmailThread[]Retrieves all spam threads irrespective of labels.
getSpamThreads(start, max)GmailThread[]Retrieves a range of spam threads irrespective of labels.
getSpamUnreadCount()IntegerGets the number of unread threads that are spam.
getStarredThreads()GmailThread[]Retrieves all starred threads irrespective of labels.
getStarredThreads(start, max)GmailThread[]Retrieves a range of starred threads irrespective of labels.
getStarredUnreadCount()IntegerGets the number of unread threads that are starred.
getThreadById(id)GmailThreadGets a thread by ID.
getTrashThreads()GmailThread[]Retrieves all trash threads irrespective of labels.
getTrashThreads(start, max)GmailThread[]Retrieves a range of trash threads irrespective of labels.
getUserLabelByName(name)GmailLabelRetrieves a label given the label name.
getUserLabels()GmailLabel[]Retrieves a list of user-created labels.
markMessageRead(message)GmailAppMarks this message read and forces the message to refresh.
markMessageUnread(message)GmailAppMarks this message unread and forces the message to refresh.
markMessagesRead(messages)GmailAppMarks these messages read and forces the messages to refresh.
markMessagesUnread(messages)GmailAppMarks these messages unread and forces the messages to refresh.
markThreadImportant(thread)GmailAppMarks this thread as important and forces the thread to refresh.
markThreadRead(thread)GmailAppMarks this thread as read and forces the thread to refresh.
markThreadUnimportant(thread)GmailAppMarks this thread as unimportant and forces the thread to refresh.
markThreadUnread(thread)GmailAppMarks this thread unread and forces the thread to refresh.
markThreadsImportant(threads)GmailAppMarks these threads as important and forces the threads to refresh.
markThreadsRead(threads)GmailAppMarks these threads as read and forces the threads to refresh.
markThreadsUnimportant(threads)GmailAppMarks these threads as unimportant and forces the threads to refresh.
markThreadsUnread(threads)GmailAppMarks these threads as unread and forces the threads to refresh.
moveMessageToTrash(message)GmailAppMoves the message to the trash and forces the message to refresh.
moveMessagesToTrash(messages)GmailAppMoves the specified messages to the trash and forces the messages to refresh.
moveThreadToArchive(thread)GmailAppMoves this thread to the archive and forces the thread to refresh.
moveThreadToInbox(thread)GmailAppMoves this thread to the inbox and forces the thread to refresh.
moveThreadToSpam(thread)GmailAppMoves this thread to spam and forces the thread to refresh.
moveThreadToTrash(thread)GmailAppMoves this thread to the trash and forces the thread to refresh.
moveThreadsToArchive(threads)GmailAppMoves these threads to the archive and forces the threads to refresh.
moveThreadsToInbox(threads)GmailAppMoves these threads to the inbox and forces the threads to refresh.
moveThreadsToSpam(threads)GmailAppMoves these threads to spam and forces the threads to refresh.
moveThreadsToTrash(threads)GmailAppMoves these threads to the trash and forces the threads to refresh.
refreshMessage(message)GmailAppReloads the message and associated state from Gmail (useful in case the labels, read state, etc., have changed).
refreshMessages(messages)GmailAppReloads the messages and associated state from Gmail (useful in case the labels, read state, etc., have changed).
refreshThread(thread)GmailAppReloads the thread and associated state from Gmail (useful in case the labels, read state, etc., have changed).
refreshThreads(threads)GmailAppReloads the threads and associated state from Gmail (useful in case the labels, read state, etc., have changed).
search(query)GmailThread[]Search Gmail with the given query.
search(query, start, max)GmailThread[]Search Gmail with the given query.
sendEmail(recipient, subject, body)GmailAppSends an email message.
sendEmail(recipient, subject, body, options)GmailAppSends an email message with optional arguments.
setCurrentMessageAccessToken(accessToken)voidSets the current message access token that enables the script to access the current GmailMessage properties.
starMessage(message)GmailAppAdds a star to this message and forces the message to refresh.
starMessages(messages)GmailAppAdds stars to these messages and forces the messages to refresh.
unstarMessage(message)GmailAppRemoves a star from this message and forces the message to refresh.
unstarMessages(messages)GmailAppRemoves stars from these messages and forces the messages to refresh.

GmailAttachment

Methods

MethodReturn typeBrief description
copyBlob()BlobReturns a copy of this blob.
getAs(contentType)BlobReturn the data inside this object as a blob converted to the specified content type.
getBytes()Byte[]Gets the data stored in this blob.
getContentType()StringGets the content type of the bytes in this blob.
getDataAsString()StringGets the data of this blob as a String with UTF-8 encoding.
getDataAsString(charset)StringGets the data of this blob as a string with the specified encoding.
getHash()StringGets the SHA1 content hash for this attachment.
getName()StringGets the name of this blob.
getSize()IntegerGets the size of this attachment.
isGoogleType()BooleanReturns whether this blob is a Google Workspace file (Sheets, Docs, etc.).
setBytes(data)BlobSets the data stored in this blob.
setContentType(contentType)BlobSets the content type of the bytes in this blob.
setContentTypeFromExtension()BlobSets the content type of the bytes in this blob based on the file extension.
setDataFromString(string)BlobSets the data of this blob from a string with UTF-8 encoding.
setDataFromString(string, charset)BlobSets the data of this blob from a string with the specified encoding.
setName(name)BlobSets the name of this blob.

GmailDraft

Methods

MethodReturn typeBrief description
deleteDraft()voidDeletes this draft message.
getId()StringGets the ID of this draft message.
getMessage()GmailMessageReturns a GmailMessage representing this draft.
getMessageId()StringReturns the ID of the GmailMessage representing this draft.
send()GmailMessageSends this draft email message.
update(recipient, subject, body)GmailDraftReplaces the contents of this draft message.
update(recipient, subject, body, options)GmailDraftReplaces the contents of this draft message using optional arguments.

GmailLabel

Methods

MethodReturn typeBrief description
addToThread(thread)GmailLabelAdds this label to the given thread and forces the thread to refresh (GmailThread.refresh()).
addToThreads(threads)GmailLabelAdds this label to the given threads and forces the threads to refresh.
deleteLabel()voidDeletes this label.
getName()StringGets the name of this label.
getThreads()GmailThread[]Gets the threads that are marked with this label.
getThreads(start, max)GmailThread[]Gets a range of threads marked with this label.
getUnreadCount()IntegerGets the number of unread threads tagged with this label.
removeFromThread(thread)GmailLabelRemoves this label from the given thread and forces the thread to refresh.
removeFromThreads(threads)GmailLabelRemoves this label from the given threads and forces the threads to refresh.

GmailMessage

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.
getAttachments(options)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.
getHeader(name)StringGets the value of an RFC 2822 header given the header name.
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 recipients 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.

GmailThread

Methods

MethodReturn typeBrief description
addLabel(label)GmailThreadAdds this label to the thread.
createDraftReply(body)GmailDraftCreates a draft message replying to the sender of the last message in this thread using the reply-to address.
createDraftReply(body, options)GmailDraftCreates a draft message replying to the sender of the last message in this thread using the reply-to address, with optional arguments.
createDraftReplyAll(body)GmailDraftCreates a draft message replying to the sender of the last message in this thread, using the reply-to address and all recipients of this message.
createDraftReplyAll(body, options)GmailDraftCreates a draft message replying to the sender of the last message in this thread, using the reply-to address and all recipients, with optional arguments.
getFirstMessageSubject()StringGets the subject of the first message in the thread.
getId()StringGets the ID of this thread.
getLabels()GmailLabel[]Returns the user-created labels on this thread.
getLastMessageDate()DateGets the date of this thread's most recent message.
getMessageCount()IntegerReturns the number of messages in the thread.
getMessages()GmailMessage[]Gets the messages in this thread.
getPermalink()StringGets a permalink for this thread.
hasStarredMessages()BooleanReturns whether the thread has any starred messages.
isImportant()BooleanReturns whether the thread is marked as important.
isInChats()BooleanReturns whether the thread is labeled a chat.
isInInbox()BooleanReturns whether the thread is in the inbox.
isInPriorityInbox()BooleanReturns true if this thread is in the priority inbox; returns false otherwise.
isInSpam()BooleanReturns whether the thread is marked as spam.
isInTrash()BooleanReturns whether the thread is in the trash.
isUnread()BooleanReturns whether the thread has any unread messages.
markImportant()GmailThreadMarks this thread as important.
markRead()GmailThreadMarks this thread as read.
markUnimportant()GmailThreadMarks this thread as unimportant.
markUnread()GmailThreadMarks this thread as unread.
moveToArchive()GmailThreadMoves this thread to the archive.
moveToInbox()GmailThreadMoves this thread to the inbox.
moveToSpam()GmailThreadMoves this thread to spam.
moveToTrash()GmailThreadMoves this thread to the trash.
refresh()GmailThreadReloads this thread, and associated state from Gmail (useful in case the labels, read state, etc., have changed).
removeLabel(label)GmailThreadRemoves this label from the thread.
reply(body)GmailThreadReply to the sender of the last message on this thread using the replyTo address.
reply(body, options)GmailThreadReply to the sender of the last message on this thread using the replyTo address, with optional arguments.
replyAll(body)GmailThreadReply to the sender (using the replyTo address), and all recipients of the last message on this thread.
replyAll(body, options)GmailThreadReply to the sender (using the replyTo address), and all recipients of the last message on this thread, with optional arguments.