One common thing to do with their files is to create backups so they don’t lose important data.
In this post, we’ll see a quick way to access files received as email attachments and copy them to a folder in Google Drive.
There are two configuration parameters:
- The destination folder to which attachments should be saved.
- A search filter to get emails. This filter follows the same pattern used on Gmail’s web interface, so you can use the interface to create the filter and then just copy it to the script.
function saveEmailsAttachments() {
// Configuration parameters
const destinationFolder = DriveApp.getFolderById('FOLDER_ID');
const emailFilter = 'label:inbox has:attachment label:unread';
for (const thread of GmailApp.search(emailFilter)) {
for (const message of thread.getMessages()) {
for (const attachment of message.getAttachments()) {
const file = attachment.copyBlob();
destinationFolder.createFile(file);
}
}
}
}
After giving the necessary permissions and running the script, you should see the files saved in the specified folder.