Essentially, my goal is to save all attachments from received emails to a specific folder in Google Drive (mostly .PDF files). However, I've encountered a limitation with the search function which only allows me to retrieve up to 500 attached files. I came across something called pageToken but I'm unsure how to incorporate it into my code. Any guidance, advice, or examples on how to address this would be greatly appreciated.
function saveGmailtoGoogleDrive() {
const folderId = '1apaQJjDSK-bNfd3ZgiFqK23cE7SCPqoB'; //Google Drive Folder
const searchQuery = 'label:unread has:attachment'; //Filter
const threads = GmailApp.search(searchQuery, 0, 500);
threads.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(message => {
const attachments = message.getAttachments({
includeInlineImages: false,
includeAttachments: true
});
attachments.forEach(attachment => {
// Insert the attachment to google drive folder
Drive.Files.insert(
{
title: attachment.getName(),
mimeType: attachment.getContentType(),
parents: [{ id: folderId }]
},
attachment.copyBlob()
);
});
});
});
};
function saveGmailtoGoogleDrive() {
const folderId = '1apaQJjDSK-bNfd3ZgiFqK23cE7SCPqoB'; //Google Drive Folder
const searchQuery = 'label:unread has:attachment'; //Filter
const threads = GmailApp.search(searchQuery, 0, 500);
threads.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(message => {
const attachments = message.getAttachments({
includeInlineImages: false,
includeAttachments: true
});
attachments.forEach(attachment => {
// Insert the attachment to google drive folder
Drive.Files.insert(
{
title: attachment.getName(),
mimeType: attachment.getContentType(),
parents: [{ id: folderId }]
},
attachment.copyBlob()
);
});
});
});
};