Skip to Content

Fetch Attachment

Retrieves the content of an email attachment from Gmail. This action downloads the attachment data in base64-encoded format.

Overview

  • Purpose: Download email attachment content from Gmail messages.
  • Use Cases: Downloading invoice PDFs, extracting document attachments, processing image files, archiving attachments, automated document workflows.
  • API Method: GET /attachment
  • Gmail API: Uses Gmail API v3 /gmail/v1/users/me/messages/\{messageId\}/attachments/\{attachmentId\} endpoint.
  • Required Scope: https://www.googleapis.com/auth/gmail.readonly

Required Input Fields

Thread ID: The unique identifier of the email message (not the thread). This is the message ID from Gmail API.

Attachment ID: The unique identifier of the specific attachment within the message. This is obtained from the email’s attachment metadata.

Getting Attachment IDs

  • Email Sync: Use the ‘emails’ sync to retrieve email messages with attachment metadata.
  • Attachment Metadata: Each email’s attachments array contains attachment IDs, filenames, MIME types, and sizes.
  • Message ID: The thread ID parameter actually refers to the message ID (Gmail’s terminology can be confusing).
  • Workflow: First sync emails, then use attachment IDs from the sync data to fetch specific attachments.

Output

  • Base64 String: Returns the attachment content as a base64-encoded string.
  • Decoding Required: You must decode the base64 string to get the actual file content.
  • Binary Data: After decoding, you’ll have the raw binary data of the attachment.

Attachment Processing

  • Decoding: Use Buffer.from(data, ‘base64’) in Node.js or atob() in browsers to decode.
  • File Saving: Write the decoded content to a file with the appropriate extension.
  • MIME Type: Use the MIME type from attachment metadata to determine file type.
  • Filename: Use the filename from attachment metadata for saving the file.
  • Size Limits: Be aware of attachment size limits when processing large files.

Required Permissions

[!IMPORTANT] OAuth Scope: Requires https://www.googleapis.com/auth/gmail.readonly scope.

  • Read-Only: This scope allows reading emails and attachments but not sending or modifying.
  • User Consent: Users must grant permission to read their emails during OAuth flow.

Error Handling

  • Invalid IDs: Returns an error if the message ID or attachment ID doesn’t exist.
  • Permissions: Fails if the user hasn’t granted the required scope.
  • Retry Logic: Automatically retries failed requests up to 3 times.
  • Large Files: Very large attachments may timeout - consider implementing timeout handling.

Common Use Cases

  • Invoice Processing: Download invoice PDFs from emails for automated processing.
  • Document Archival: Save important document attachments to cloud storage.
  • Image Extraction: Extract images from emails for display or processing.
  • Data Import: Download CSV or Excel files from emails for data import workflows.
  • Compliance: Archive email attachments for regulatory compliance.
  • Automated Workflows: Trigger actions based on attachment content (e.g., OCR, analysis).

Tips

[!TIP] First sync emails to get attachment IDs - you can’t fetch attachments without them

[!IMPORTANT] The ‘threadId’ parameter is actually the message ID, not the conversation thread ID

[!TIP] Decode the base64 response before saving or processing the file

[!TIP] Use attachment metadata (filename, MIME type) from the email sync for proper file handling

[!WARNING] Check attachment size before fetching to avoid memory issues with large files

[!TIP] Implement error handling for missing or deleted attachments

[!TIP] Store attachment IDs if you need to fetch the same attachment multiple times

[!TIP] Consider caching downloaded attachments to reduce API calls

[!TIP] Use the MIME type to validate file types before processing

[!TIP] For large attachments, implement streaming or chunked processing if possible

[!IMPORTANT] Ensure your OAuth flow requests the gmail.readonly scope

[!TIP] Test with various file types (PDF, images, documents) to ensure proper handling

Need help? Have feedback?