Skip to content

Instantly share code, notes, and snippets.

@Avi-E-Koenig
Last active November 17, 2025 15:24
Show Gist options
  • Select an option

  • Save Avi-E-Koenig/3cee28794d2a2877943f7e2703f8575f to your computer and use it in GitHub Desktop.

Select an option

Save Avi-E-Koenig/3cee28794d2a2877943f7e2703f8575f to your computer and use it in GitHub Desktop.
google get all attachments
function saveAttachmentsFromSender() {
const SENDER = "******";
const FOLDER_ID = "*******";
const folder = DriveApp.getFolderById(FOLDER_ID);
const threads = GmailApp.search(`from:${SENDER} has:attachment`);
let savedCount = 0;
threads.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(msg => {
const attachments = msg.getAttachments();
attachments.forEach(att => {
const name = att.getName();
// Avoid duplicates
const existing = folder.getFilesByName(name);
if (existing.hasNext()) {
Logger.log(`Skipping duplicate: ${name}`);
return;
}
folder.createFile(att);
savedCount++;
Logger.log(`Saved: ${name}`);
});
});
});
Logger.log(`Done. Total attachments saved: ${savedCount}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment