Created
February 10, 2020 22:57
-
-
Save abrahamjuliot/829afd63977dfc62c3c77e8e272a44c7 to your computer and use it in GitHub Desktop.
Log unread gmail messages
This file contains hidden or 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
function logToTextFile(filename, content) { | |
var files = DriveApp.getFilesByName(filename) | |
if (files.hasNext()) { return files.next().setContent(content) } | |
else { return DriveApp.createFile(filename, content) } | |
} | |
function getUnread() { | |
function flatten(arr) { return [].concat.apply([], arr) } | |
function formatDate(d) { return Utilities.formatDate(new Date(d), "PST", "EEE, MMM d @h:mm a")} | |
function removeDomain(strongWithEmail) { return strongWithEmail.replace(/<.+@.+>/gm, '')} | |
var threads = GmailApp.search('is:unread', 0, 100) | |
var messages = flatten(threads.map(function(thread) { return thread.getMessages() })) | |
.filter(function(message) { return message.isUnread() }) | |
var content = 'π©βπβοΈπ₯'+'TOTAL UNREAD: '+messages.length+' - '+formatDate(new Date())+'\n\n' | |
for (var i in messages) { | |
var message = messages[i] | |
var data = { | |
date: formatDate(message.getDate()), | |
from: message.getFrom(), | |
to: message.getTo(), | |
cc: message.getCc(), | |
subject: message.getSubject(), | |
body: message.getPlainBody(), | |
snippet: function() { return this.body.split('\n')[0]+' '+this.body.split('\n')[1]+' '+this.body.split('\n')[3] } | |
} | |
content += '\n\n'+'π₯'+data.date | |
+' - from: '+data.from | |
+', subject: '+data.subject | |
+', snippet: '+data.snippet() | |
} | |
return logToTextFile('Unread Log.txt', removeDomain(content)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment