Skip to content

Instantly share code, notes, and snippets.

@gurdiga
Last active January 3, 2022 14:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gurdiga/20d81fbbfd50d94efee580e48d8c8a23 to your computer and use it in GitHub Desktop.
Google App Script
/*
1. Check if there are any new messages with “DMARC Reports” label in my GMail, and
2. if yes, then get the attached file and put it in the “DMARC Reports” folder in my GDrive.
3. Then send me an email with a report about what happened.
*/
// GmailApp: https://developers.google.com/apps-script/reference/gmail/gmail-app
// DriveApp: https://developers.google.com/apps-script/reference/drive/drive-app
const labelName = "DMARC reports";
const folderName = "DMARC reports";
function importReports() {
const label = GmailApp.getUserLabelByName(labelName);
if (label.getUnreadCount() === 0) {
return;
}
let report = "";
const say = (string) => {
report += `\n${string}`;
Logger.log(string);
};
say(`Found ${label.getUnreadCount()} new messages`);
const unreadThreads = label.getThreads().filter((t) => t.isUnread());
const folder = DriveApp.getFoldersByName(folderName).next();
unreadThreads.forEach((thread) => {
const attachment = thread.getMessages()[0].getAttachments()[0];
const file = DriveApp.createFile(attachment.getAllBlobs()[0]);
folder.addFile(file);
thread.markRead();
say(attachment.getName());
});
GmailApp.sendEmail("gurdiga@gmail.com", "New DMARC reports", report);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment