> For the complete documentation index, see [llms.txt](https://doc.duaer.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.duaer.com/recipes/gmail-attachments-to-google-drive.md).

# Save Gmail attachments to Google Drive automatically with Duaer

In Duaer, each new email with attachments uploads every file under its original name to a Google Drive folder.
## Build the attachment archive in Duaer

1. Add [Gmail Trigger](/integrations/builtin/trigger-nodes/duaer-nodes-base.gmailtrigger.md), turn off Simplify, turn on Download Attachments under Options, and set Search under Filters to has:attachment.
2. Attachments land on attachment_0, attachment_1, and so on. Add [Code](/build/code.md) with the code below for one item per file, each on data.
3. To keep only PDFs, add [If](/build/if.md) with {{ $json.fileName.toLowerCase().endsWith('.pdf') }} is true.
4. Add [Google Drive](/build/google-drive.md) with Resource File and Operation Upload; Input Data Field Name data, File Name {{ $json.fileName }}, then pick Parent Drive and Parent Folder.
5. Publish the digital organization.

```
const out = [];
for (const item of $input.all()) {
  for (const key of Object.keys(item.binary ?? {})) {
    out.push({
      json: { ...item.json, fileName: item.binary[key].fileName },
      binary: { data: item.binary[key] },
    });
  }
}
return out;
```

## Folders by sender or month

Prefix File Name with a date, such as {{ $now.toFormat('yyyy-MM') }}-{{ $json.fileName }}. For real folders, create them with the Google Drive Folder create operation first, then set Parent Folder by expression.
## Questions

### Does Duaer save the same attachments twice?

No. The Duaer Gmail Trigger fires once per new email; only a manual rerun uploads again.

### Can Duaer save attachments from a non-Gmail mailbox?

Yes. Swap Gmail Trigger for Email Trigger (IMAP) with attachment download on; the rest of the Duaer flow stays the same.

## Related

- [Upload a file with Google Drive in Duaer](https://doc.duaer.com/build/google-drive.md)
- [Send mail with Gmail in Duaer](https://doc.duaer.com/build/gmail.md)
- [Work with files and images in Duaer](https://doc.duaer.com/data/binary-data.md)
- [Fix Google invalid_grant and 7-day token expiry in Duaer](https://doc.duaer.com/troubleshooting/google-oauth-invalid-grant.md)

