Class GmailAttachment

  • A GmailAttachment is a Blob with an extragetSize() method that is faster and does not count against the Gmail read quota.

  • You can retrieve information about Gmail attachments, such as name and size, using provided methods.

  • ThegetSize() andgetHash() methods for Gmail attachments do not count against the Gmail read quota and require specific authorization scopes.

  • Several methods are available for interacting with the data of a Gmail attachment, including getting and setting the data as bytes or strings, and converting to different content types.

  • ThegetAllBlobs() method for Gmail attachments is deprecated and should not be used in new scripts.

GmailAttachment

An attachment from Gmail. This is a regularBlob except that it has an extragetSize() method that is faster than callinggetBytes().length and does not count against the Gmail read quota.

// Logs information about any attachments in the first 100 inbox threads.constthreads=GmailApp.getInboxThreads(0,100);constmsgs=GmailApp.getMessagesForThreads(threads);for(leti=0;i <msgs.length;i++){for(letj=0;j <msgs[i].length;j++){constattachments=msgs[i][j].getAttachments();for(letk=0;k <attachments.length;k++){Logger.log('Message "%s" contains the attachment "%s" (%s bytes)',msgs[i][j].getSubject(),attachments[k].getName(),attachments[k].getSize(),);}}}

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.

Deprecated methods

MethodReturn typeBrief description
getAllBlobs()Blob[]Gets all the blobs that are contained within this (possibly composite) blob.

Detailed documentation

copyBlob()

Returns a copy of this blob.

Return

Blob — The new copy.


getAs(contentType)

Return the data inside this object as a blob converted to the specified content type. Thismethod adds the appropriate extension to the filename—for example, "myfile.pdf". However, itassumes that the part of the filename that follows the last period (if any) is an existingextension that should be replaced. Consequently, "ShoppingList.12.25.2014" becomes"ShoppingList.12.25.pdf".

To view the daily quotas for conversions, seeQuotas for GoogleServices. Newly created Google Workspace domains might be temporarily subject to stricterquotas.

Parameters

NameTypeDescription
contentTypeStringThe MIME type to convert to. For most blobs,'application/pdf' is the only valid option. For images in BMP, GIF, JPEG, or PNG format, any of'image/bmp','image/gif','image/jpeg', or'image/png' are also valid. For a Google Docs document,'text/markdown' is also valid.

Return

Blob — The data as a blob.


getBytes()

Gets the data stored in this blob.

Return

Byte[] — The stored bytes.


getContentType()

Gets the content type of the bytes in this blob.

Return

String — The content type of this data, if known, ornull.


getDataAsString()

Gets the data of this blob as a String with UTF-8 encoding.

Return

String — The data as a string.


getDataAsString(charset)

Gets the data of this blob as a string with the specified encoding.

Parameters

NameTypeDescription
charsetStringThe charset to use in encoding the data in this blob as a string.

Return

String — The data as a string.


getHash()

Gets the SHA1 content hash for this attachment. This method does not count against the Gmailread quota.

Return

String — The SHA1 content hash, as a string.

Authorization

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

  • https://mail.google.com/

getName()

Gets the name of this blob.

Return

String — The name of this data, if known, ornull.


getSize()

Gets the size of this attachment. This method is faster than callinggetBytes().length and does not count against the Gmail read quota.

Return

Integer — The size of the attachment, in bytes.

Authorization

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

  • https://mail.google.com/

isGoogleType()

Returns whether this blob is a Google Workspace file (Sheets, Docs, etc.).

Return

Booleantrue if this blob is a Google Workspace file;false if not.


setBytes(data)

Sets the data stored in this blob.

Parameters

NameTypeDescription
dataByte[]The new data.

Return

Blob — This blob, for chaining.


setContentType(contentType)

Sets the content type of the bytes in this blob.

Parameters

NameTypeDescription
contentTypeStringThe new contentType.

Return

Blob — This blob, for chaining.


setContentTypeFromExtension()

Sets the content type of the bytes in this blob based on the file extension. The contentType isnull if it cannot be guessed from its extension.

Return

Blob — This blob, for chaining.


setDataFromString(string)

Sets the data of this blob from a string with UTF-8 encoding.

Parameters

NameTypeDescription
stringStringThe string data.

Return

Blob — This blob, for chaining.


setDataFromString(string, charset)

Sets the data of this blob from a string with the specified encoding.

Parameters

NameTypeDescription
stringStringThe string data.
charsetStringThe charset to use in interpreting the string as bytes.

Return

Blob — This blob, for chaining.


setName(name)

Sets the name of this blob.

Parameters

NameTypeDescription
nameStringThe new name.

Return

Blob — This blob, for chaining.

Deprecated methods

getAllBlobs()

Deprecated. This function is deprecated and should not be used in new scripts.

Gets all the blobs that are contained within this (possibly composite) blob.

Return

Blob[] — The blobs contained within the blob.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.