Last active
January 3, 2022 14:37
-
-
Save gurdiga/20d81fbbfd50d94efee580e48d8c8a23 to your computer and use it in GitHub Desktop.
Google App Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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("[email protected]", "New DMARC reports", report); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment