> For the complete documentation index, see [llms.txt](https://doc.duaer.com/zh/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/zh/recipes/gmail-attachments-to-google-drive.md).

# 在 Duaer 里把 Gmail 附件自动存到 Google Drive

在 Duaer 里，带附件的新邮件一到，就把每个附件按原文件名上传到 Google Drive 的指定文件夹。
## 在 Duaer 里搭附件归档

1. 加 [Gmail Trigger](/zh/integrations/builtin/trigger-nodes/duaer-nodes-base.gmailtrigger.md)，关掉 Simplify，在 Options 里打开 Download Attachments；Filters 的 Search 写 has:attachment。
2. 附件会放在 attachment_0、attachment_1 …… 上。接 [Code](/zh/build/code.md)，粘贴下面的代码，一个附件一条，文件都放在 data 上。
3. 只要 PDF 时接 [If](/zh/build/if.md)，条件 {{ $json.fileName.toLowerCase().endsWith('.pdf') }} 为 true。
4. 接 [Google Drive](/zh/build/google-drive.md)，Resource 选 File，Operation 选 Upload，Input Data Field Name 填 data，File Name 填 {{ $json.fileName }}，选 Parent Drive 和 Parent Folder。
5. 发布数字组织。

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

## 按发件人或月份分文件夹

File Name 可以带上日期，例如 {{ $now.toFormat('yyyy-MM') }}-{{ $json.fileName }}。要真正分文件夹，先用 Google Drive 的 Folder 创建操作建好，再把 Parent Folder 改成表达式。
## Questions

### Duaer 会重复保存同一封邮件的附件吗？

不会。Duaer 的 Gmail Trigger 每封新邮件只触发一次；手动重跑旧执行时才会再传一遍。

### Duaer 能存企业邮箱或 QQ 邮箱的附件吗？

能。把 Gmail Trigger 换成 Email Trigger (IMAP)，打开下载附件，后面的 Duaer 步骤不变。

## 相关

- [在 Duaer 里用 Google Drive 上传文件](https://doc.duaer.com/zh/build/google-drive.md)
- [在 Duaer 里用 Gmail 发邮件](https://doc.duaer.com/zh/build/gmail.md)
- [在 Duaer 里处理文件和图片](https://doc.duaer.com/zh/data/binary-data.md)
- [在 Duaer 里解决 Google 凭证 invalid_grant 和 7 天失效](https://doc.duaer.com/zh/troubleshooting/google-oauth-invalid-grant.md)

