GmailAttachment

An attachment from Gmail. This is a regular [`Blob`](https://developers.google.com/apps-script/reference/base/blob) except that it has an extra `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getSize()` method that is faster than calling
`https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getBytes()``.length` and does not count against the Gmail read quota.

```javascript
// Logs information about any attachments in the first 100 inbox threads.
const threads = GmailApp.getInboxThreads(0, 100);
const msgs = GmailApp.getMessagesForThreads(threads);
for (let i = 0; i < msgs.length; i++) {
  for (let j = 0; j < msgs[i].length; j++) {
    const attachments = msgs[i][j].getAttachments();
    for (let k = 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

| Method | Return type | Brief description |
|---|---|---|
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#copyBlob()` | `https://developers.google.com/apps-script/reference/base/blob.html` | Returns a copy of this blob. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getAs(String)` | `https://developers.google.com/apps-script/reference/base/blob.html` | Return the data inside this object as a blob converted to the specified content type. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getBytes()` | `Byte[]` | Gets the data stored in this blob. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getContentType()` | `String|null` | Gets the content type of the bytes in this blob. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getDataAsString()` | `String` | Gets the data of this blob as a String with UTF-8 encoding. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getDataAsString(String)` | `String` | Gets the data of this blob as a string with the specified encoding. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getHash()` | `String` | Gets the SHA1 content hash for this attachment. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getName()` | `String|null` | Gets the name of this blob. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getSize()` | `Integer` | Gets the size of this attachment. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#isGoogleType()` | `Boolean` | Returns whether this blob is a Google Workspace file (Sheets, Docs, etc.). |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#setBytes(Byte)` | `https://developers.google.com/apps-script/reference/base/blob.html` | Sets the data stored in this blob. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#setContentType(String)` | `https://developers.google.com/apps-script/reference/base/blob.html` | Sets the content type of the bytes in this blob. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#setContentTypeFromExtension()` | `https://developers.google.com/apps-script/reference/base/blob.html` | Sets the content type of the bytes in this blob based on the file extension. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#setDataFromString(String)` | `https://developers.google.com/apps-script/reference/base/blob.html` | Sets the data of this blob from a string with UTF-8 encoding. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#setDataFromString(String,String)` | `https://developers.google.com/apps-script/reference/base/blob.html` | Sets the data of this blob from a string with the specified encoding. |
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#setName(String)` | `https://developers.google.com/apps-script/reference/base/blob.html` | Sets the name of this blob. |

### Deprecated methods

| Method | Return type | Brief description |
|---|---|---|
| `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getAllBlobs()` | `https://developers.google.com/apps-script/reference/base/blob.html` | Gets all the blobs that are contained within this (possibly composite) blob. |

## Detailed documentation

### `copyBlob()`

Returns a copy of this blob.

#### Return


`https://developers.google.com/apps-script/reference/base/blob.html` --- The new copy.

*** ** * ** ***

### `getAs(contentType)`

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

To view the daily quotas for conversions, see [Quotas for Google
Services](https://developers.google.com/apps-script/guides/services/quotas). Newly created Google Workspace domains might be temporarily subject to stricter
quotas.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `contentType` | `String` | The 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


`https://developers.google.com/apps-script/reference/base/blob.html` --- 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|null` --- The content type of this data, if known, or `null`.

*** ** * ** ***

### `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

| Name | Type | Description |
|---|---|---|
| `charset` | `String` | The 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 Gmail
read quota.

#### Return


`String` --- The SHA1 content hash, as a string.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes) or appropriate scopes from the [related REST API](https://developers.google.com/identity/protocols/googlescopes#gmailv1):

- `https://mail.google.com/`

*** ** * ** ***

### `getName()`

Gets the name of this blob.

#### Return


`String|null` --- The name of this data, if known, or `null`.

*** ** * ** ***

### `getSize()`

Gets the size of this attachment. This method is faster than calling `https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getBytes()``.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 following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes) or appropriate scopes from the [related REST API](https://developers.google.com/identity/protocols/googlescopes#gmailv1):

- `https://mail.google.com/`

*** ** * ** ***

### `isGoogleType()`

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

#### Return


`Boolean` --- `true` if this blob is a Google Workspace file; `false` if not.

*** ** * ** ***

### `setBytes(data)`

Sets the data stored in this blob.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `data` | `Byte[]` | The new data. |

#### Return


`https://developers.google.com/apps-script/reference/base/blob.html` --- This blob, for chaining.

*** ** * ** ***

### `setContentType(contentType)`

Sets the content type of the bytes in this blob.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `contentType` | `String` | The new contentType. |

#### Return


`https://developers.google.com/apps-script/reference/base/blob.html` --- This blob, for chaining.

*** ** * ** ***

### `setContentTypeFromExtension()`

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

#### Return


`https://developers.google.com/apps-script/reference/base/blob.html` --- This blob, for chaining.

*** ** * ** ***

### `setDataFromString(string)`

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

#### Parameters

| Name | Type | Description |
|---|---|---|
| `string` | `String` | The string data. |

#### Return


`https://developers.google.com/apps-script/reference/base/blob.html` --- This blob, for chaining.

*** ** * ** ***

### `setDataFromString(string, charset)`

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

#### Parameters

| Name | Type | Description |
|---|---|---|
| `string` | `String` | The string data. |
| `charset` | `String` | The charset to use in interpreting the string as bytes. |

#### Return


`https://developers.google.com/apps-script/reference/base/blob.html` --- This blob, for chaining.

*** ** * ** ***

### `setName(name)`

Sets the name of this blob.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `name` | `String` | The new name. |

#### Return


`https://developers.google.com/apps-script/reference/base/blob.html` --- 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


`https://developers.google.com/apps-script/reference/base/blob.html` --- The blobs contained within the blob.