Archiving Google Mail To Files
/I was recently tasked with extracting 2-300 gmail messages including attachments. The requirements seemed simple enough:
- Every message must include original meta data (from/to/cc/bcc/date etc)
- all attachments must be saved in original format
Obviously one way would have been to sync the messages to outlook, create a folder in outlook, copy and/or move the messages to that folder, then export it to PST.
But it seemed like there should be a 'native' gmail way to do it. So I started poking around and found a lot of Google Apps Scripts had been written that would bulk forward or archive messages. This one by Marcello Scacchetti caught my eye.
But as I started using it, I realized there were a few aspects I didn't really like:
- It doesn't save the meta data
- It creates temp files in Google Drive
- Hard coded naming conventions
- Hard coded to PDF output.
- It hard coded labels
- It was built in a spreadsheet.
To keep this post short, I will break this into several posts and only address one issue here. Since #1 is easy enough for the average bear to fix themselves so I won't bother addressing that. The first real problem was with #2. Here we see the offending code:
// Create the message PDF inside the new folder var htmlBodyFile = newFolder.createFile('body.html', messageBody, "text/html"); var pdfBlob = htmlBodyFile.getAs('application/pdf'); pdfBlob.setName(newFolderName + ".pdf"); newFolder.createFile(pdfBlob); htmlBodyFile.setTrashed(true);
Before it creates the pdf file, it creates an HTML file. Why is this a problem? Well in scripts (particularly individual not paid corporate gmail accounts) there are quotas on how many files you can create via script per day - 250 as of the time of this writing. That means that you could only archive 125 emails with this, and then only if none of them had attachments. Luckily this is easy enough to fix:
var output = HtmlService.createHtmlOutput(messageBody) var pdfBlob=output.getAs('application/pdf'); pdfBlob.setName(newFolderName + ".pdf"); newFolder.createFile(pdfBlob);
Here we use the HTMLService to create an object rather than a file. Easy fix and doubles the number of emails we can process per day.
I'll post some solutions to my other issues with the script in the next few days.