By using this script you can archive Gmail messages to Google Drive including message attachments. First of all create a copy of the following spreadsheet document: Archive Gmail messages to Google Drive
The spreadsheet document should contain a new menu item called “Gmail Archiver”. Click on ”Gmail Archiver” menu and select “Initialize”. You should see an authorization request like the following:
Press “OK” button to authorize and follow the authorization process. Now click again on ”Gmail Archiver” menu and select “Initialize”. You should see now a confirmation box about the successful initialization of the script.
The script created for you a new Gmail label called “Archive to Drive” and a new drive folder called “Email Archive” where Gmail messages will be stored.
Now go to Gmail and add the Gmail label “Archive to Drive” to each message you want to archive:
Now go back to the spreadsheet document and click on ”Gmail Archiver” menu and select “Archive Gmail Messages”. After some seconds you should see a message like:
Now go to Google Drive inside folder “Email Archive” and you should see a new folder representing the exported email message:
Now entering the newly created folder you should see the email messages content saves as a PDF file and all message attachments saved as separate files:
Enjoy and happy scripting!








This is a great idea. I have been looking for a way to archive certain labeled gmail folders…your definitely on to something! For my needs I need to filter and apply formulas to the contents of the archived gmail emails once they have been archived into the spreadsheet. Is it possible to do this using your script?
Hello Richard,
actually this script doesn’t use Spreadsheet as a bridge to archive Gmail messages. They are directly stored inside Google Drive folders. An intermediate step could be added to record archived messages on a Spreadsheet document and then apply needed calculations and formulas there. Right now this is not implemented since this was mainly a base where to build on.
Is it possible to store the the messages as native Google Drive Documents? Doing so would not count to the Drive storage I think..
Hello Luciano, I don’t think so, since Google changed the Documents format from HTML to another proprietary format. Anyway this is a good point I’ll will investigate about this possibility.
Excelent job. I’ve expecting this solution for while and I´d like to ask how hard it’d be to “filter” specifically email keys (as in Gmail filter) to archive into a specifically GDocs folder?
This is great!!
Thanks
how did you get it to work / i copied it /it opened spreadsheet but
thats all it did/ ?
chap further down in comments also it would appear has the same question
Hello John, please make sure to use the menu item File -> Make a copy of the linked spreadsheet file at: https://docs.google.com/spreadsheet/ccc?key=0AiVfk2mjBcimdF80RzMxeFd1YWdjWGlxWVctYUFqeGc
Also note that the menu item could take some time load, in some situations it could take 1 minute.
If still nothing happens the only thing I can suggest is to use the “Tools -> Script Manager…” menu item, select the “onOpen” item and click the Run button.
Hope this helps.
Thanks Robert works like a charm!
Here an additional usage tip, as not everybody seems to be aware of this.
If you want to move emails with large attachments you can use “size:” as a search criteria in gmail, like so:
size:26214400
This will list all emails equal or larger then 25MB, note the size given should be in bytes (MB x 1024 x 1024).
Then apply the “Archive to Drive” label, go back to the spreadsheet and apply “Gmail archiver => Archive gmail messages.”
Oops!
My apologies to Marcello, I daftly echoed “thanks Robert” from a pingback whereas the thanks should go to Marcello Scacchetti.
No problem
Not working properly,
showing some error.
“Email Archive Folder not found”
Rocky, have you run the “Initialize” menu item before running the “Archive Gmail Messages”?
“Gmail Archiver” does not show up as a new menu item on the spreaddsheet document ARCHIVE GMAIL MESSAGES TO GOOGLE DRIVE.
????
Christy, please check the answer I gave to John who has the same issue.
Ran script after setting up archiver.
Google indicates I’ve run the script too many times today. Ran it once.
Am I doing something wrong?
John, seems to be a Google issue, make sure you made a copy of the spreadsheet document
Hi Marcello
The new menu item “Gmail Archiver” does not appear for me either. Any suggestions?
Many thanks
Dave
Dave, please check the answer I gave to John who has the same issue.
Thanks for such professional responses to question. You make the web work!
John
First, this is brilliant and a lifesaver. Thank you so much for sharing this.
One question, is it possible to change the naming convention to start with Year/Month/Day instead of Month/day/Year so they can archive can be sorted chronologically?
Hello Joshua,
yes it’s definitely possible, but you have to edit the script. You have to change line 45 from:
var messageDate = Utilities.formatDate(messagesArr[k].getDate(), Session.getTimeZone(), “dd/MM/yyyy – HH:mm:ss”);
to:
var messageDate = Utilities.formatDate(messagesArr[k].getDate(), Session.getTimeZone(), “yyyy/MM/dd – HH:mm:ss”);
Thanks!
That’s an asset!
I love what this is doing but is there any way to only get attachments? I get 100 emails a day where I have to individually open attachments and print them. Looking for a workaround.
It works great. However, it timed out after a pretty short amount of time. Is that a problem with the script or something on Google’s end? If it has a short run length it actually won’t be terribly useful for backing up emails considering I have a ton of emails to archive (and I’m sure I’m not alone!).
Hi there,
This works great in gmail accounts, however I run google apps on my domain name and my email although it is not @gmail.com I can access it through the gmail website..
So it really is gmail.
When I try to do this, the gmail archive menue does not appear.
Any workaround???
Great idea by the way
This is a great tool. a lot of my customers will be happy to see this. Thanks for writing this.
I love the concept of the script, however I am having some issues.
I’ve followed the instructions to the letter, but when I attempt to run the script, I get the error: “Authorization is required to perform that action. Please run the script again to authorize it.”
I’ve attempted many combinations of choosing “initialize” and refreshing, trying different browsers on different systems, and trying different accounts, no dice.
I have 2-step verification turned on all the accounts if that makes a difference. Would love some assistance. If we can get it working reliably, it would make my day. Thanks!
-K
Google docs do not take up any space in Google Drive! I am keen to find an add-on or a Script which allows for gmail messages and attachments to be saved into Google Drive in ‘Google doc’ format. Any ideas whether such a script exists?
Hi I am getting authorisation issues an no label created in Gmail when I run both scripts ? Any ideas ? Thank you very much,
I think I found the solution to my own question. I was testing on my personal Gmail account. When I tested on my Google Apps account it appears to be working great. Thanks!
Hey Marcello! Great script!
Can I kindly ask you to if it is possible to add all names of attached lables from gmail to the folder in drive?
I have checked the code a bit – but me being a beginner it is to hard for me to change anything..
Thanks!
Cheers, Philip
Philip, thanks
Yes, adding labels to the directory created inside Google Drive it’s possible but requires some work on the script, it’s not achievable by a quick hack. If you are really interested in that feature just contact me here
The date format of the saved file is dd-mm-yyyy and this is not sort-order-friendly, both into drive and into local file systems, it should be just yyyymmdd.
Yep, that’s the Italian time date format, switching to the “international” standard would make it sortable. It is doable by making a small script modification.
What about putting the script, once properly initialized, amongst those scripts triggered every 15 minutes into google drive? This way you wouldn’t have to open the spreadsheet every time. Just an idea.
Hello Tiziano,
you can set the trigger configuration manually from the Google Apps Script editor under the menu item: Resources->Current project’s triggers… so you can adapt it to your specific time sync needs.
Thank you. I am posting a comment today I tried to post but failed some days ago.
Thank you Marcello, this is pure gold.
I am writing in English for other’s commenters sake.
I run the script and, even if I tagged just 3 messaged, the whole threads where those messages were into were saved. Now, I do not know if this is a gmail feature or the script’s, I disabled threaded view into gmail. I guess that since the script is mainly for attachment that being able to focus on single messages would be much better than saving the whole thread (which is cluttering).
e-mail archive to drive how to move back in inbox ?
Hello, there is no way to import them back to gmail since this solution is just for archival not a complete gmail backup solution.
This is not working for me. It says, ‘Service invoked too many times for one day: docslist create.’
How do I fix it?
Hello Matt, Google imposed over Google Apps Script some quota limitations. If you are trying to archive too many messages a day or in a single run you will get that message. I can suggest to execute the archival procedure in small batches. Hope this helps.
I successfully used the program to archive my messages. However, it still appears that they are in my gmail account taking up room. When the program runs the messages move out of the archive to drive folder. Is there any way to automatically delete the archived mail from my gmail or keep them in the archive to drive folder so that it is easier for me to delete them?
Is it possible to share the email archive folder and have the script running on several gmail accounts and archiving to the shared folder? I ran a few test runs to different accounts and it didn’t appear to be able to work, however, maybe I am not going about it in the right way.
I haven’t used this myself (yet) because I’m not near the limit. I do, however, definitely see the advantage of this tool.
One suggestion for a common request here: When you run the portion of the tool that marks the emails for Archive to Drive, go to your Gmail account and select on that label. Then add a 2nd label to those emails (any unique label that is not used by the script or any other emails). After the Archive to Drive is complete, you can then go back to Gmail and select all emails from that label and delete them. That will complete the cycle and remove them from your storage requirements in Gmail. This is not done in the script, I presume, for the very solid reason that they do not wish to monkey around with deleting anyone’s precious emails. You confirm that all have been properly archived, then you control the delete function.
Value resource – Thank you Marcello. I’ll be back here.
It works for a while and then I start getting an error that the Drive folder “Email Archive” cannot be found. But the folder is there.
Ryan, that is a known issue from Google I know that they are working on it
i can not preview images/video in google drive folder via this scripts.but i can view images/video if upload directly from my PC.
please advise,thank you for your great scripts.
Works like a charm.
I think Google should pay you for the good work you have done and have them add it straight into Gmail. (Minus the spread sheet of course)
Thanks very much!
Can the script be modified to save the emails into a folder structure that matches my label structure? In other words, if I have a label with three nested labels, and I use the script to archive all messages, can it be saved in Drive as a folder with three sub-folders?
Great job on the script. A couple of questions.
1) I am getting the error message “Deserialization of continuation failed” at the end of the scrip run although all seems to work ok. Is this an issue?
2) I realise that the “Archive to Drive” label is removed during the archive process but is it possible to easily add another label such as “Archived”?
Thanks again.
Hello Peter,
that error you see probably is due to google apps script timeout in the export operation. Regarding point number 2, yes it’s possible but the script logic has to be changed.
Thanks for this!
I have published a (probably poorly) hacked version that uses the first three columns of the spreadsheet to get the source label, the destination drive folder, and the destination label.
The published version is here. No promises, but you might be able to make it work for you.
https://docs.google.com/spreadsheet/pub?key=0Ai_QkvQx5r18dFU5VVBzUVpkc2ZKRGJ2Y1pmdDZ4Tnc&output=html
Everything time i go to intialize i get the same popupytime i try
It’s like it’s resetting that setting everytime i try to intialize.
You consider the introduction of software purchase of services to assist the user, G-mail transfer to the driver.
Use the script in Google will be limited, and I can not store large amounts of mail at once.
This is pretty sweet
Is there a way or line to add that also grabs the headers?
Thanks
thank you very much.
THANK YOU!!!!!!!!!!!!!!!!!!!!!!!
Mario – i now get this message from time to time:
Service invoked too many times in a short time: driveWriteVolume rateMax. Try Utilities.sleep(1000) between calls
Do you get this too?
Rob, it happens if you have a lot of messages to archive, Google has setup a sort of protection to now overload their servers.
i found it happens when trying to copy a particularly large attachment (10 MB). when using the google APIs (https://code.google.com/apis/console) you can increase per-user limits above the standard. Is there a way to recreate the script to use this?
folder in google drive is created as also the folder in gmail, but the folder in google drive is empty ! the mail does not get transferred to the google drive. what could be the reason ?
Hi Marcello,
Your script provides a very essential service. Thank you for making it available!
I’ve tried it with an email with pictures attached in jpg format. Unfortunately it created corrupted image and produced the error message “Unexpected exception upon serializing continuation”. Have I done anything wrong?
Many thanks, Amnon
Great Script!! Works like a charm!
Thanks a lot for this great tool.
I modified your scripts to better fit my needs. First, instead of creating a folder for each message, my script creates a folder for each sender; second, my script does not archive email messages, only the attached files.
I put my script on github with ackowledgement for your original contribution. Hopeful you are happy with that: https://github.com/songqiang/gmailarchiver
thanks again!
Hi,
Works well. Thank you
I have noticed that the name on some e-mails are very long in drive. If I try to open in Windows, it says the name is too long and it won’t open the PDF. Of course I can manually shorten the name but if there is a workaround for this let me know.
Thanks,
Badi