Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ahochsteger committed Feb 11, 2013
1 parent a1ff56f commit a171778
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 2 deletions.
116 changes: 116 additions & 0 deletions Code.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Gmail2GDrive by Andreas Hochsteger
// https://github.com/ahochsteger/gmail2gdrive

/**
* Configuration for Gmail2GDrive
*/
function getGmail2GDriveConfig() {
return {
// Gmail label for processed threads:
"processedLabel": "gmail2gdrive/processed",
// Sleep time in milli seconds between processed messages:
"sleepTime": 100,
// Maximum script runtime in seconds (google scripts will be killed after 5 minutes):
"maxRuntime": 280,
// Only process message newer than (leave empty for no restriction; use d, m and y for day, month and year):
"newerThan": "1m",
// Processing rules:
"rules": [
// Rule documentation:
// * filter (String, mandatory): a typical gmail search expression (see http://support.google.com/mail/bin/answer.py?hl=en&answer=7190)
// * folder (String, mandatory): a path to an existing Google Drive folder
// * archive (boolean, optional): Should the gmail thread be archived after processing? (default: false)
// * filenameFrom (String, optional): The attachment filename that should be renamed when stored in Google Drive
// * filenameTo (String, optional): The new filename for the attachment. You can use the special characters 'YYYY' for year, 'MM' for month and 'DD' for day as pattern in the filename.
{
"filter": "to:my.name+scans@gmail.com",
"folder": "Scans"
},
{
"filter": "from:example1@example.com",
"folder": "Examples/example1"
},
{
"filter": "from:example2@example.com",
"folder": "Examples/example2"
},
{
"filter": "(from:example3a@example.com OR from:example3b@example.com)",
"folder": "Examples/example3ab",
"filenameFrom": "file.txt",
"filenameTo": "file-YYYY-MM-DD.txt",
"archive": true
}
]
};
}

/**
* Main function that processes Gmail attachments and stores them in Google Drive.
* Use this as trigger function for periodic execution.
*/
function performGmail2GDrive() {
var config = getGmail2GDriveConfig();
var gProcessedLabel = config.processedLabel;
var gNewerThan = config.newerThan;
var label = GmailApp.getUserLabelByName(gProcessedLabel);
var end, start;
start = new Date();

Logger.log("INFO: Starting mail attachment processing.");
for (var i=0; i<config.rules.length; i++) {
var rule = config.rules[i];
var gSearchExp = rule.filter + " -label:" + gProcessedLabel;
if (gNewerThan != "") {
gSearchExp += " newer_than:" + gNewerThan;
}
var gFolder = rule.folder;
var doArchive = rule.archive == true;
var threads = GmailApp.search(gSearchExp);
var folder = DocsList.getFolder(gFolder);

Logger.log("INFO: Processing rule: "+gSearchExp);
for (var x=0; x<threads.length; x++) {
var thread = threads[x];
end = new Date();
var runTime = (end.getTime() - start.getTime())/1000;
Logger.log("INFO: Processing thread: "+thread.getFirstMessageSubject() + " (runtime: " + runTime + "s/" + config.maxRuntime + "s)");
if (runTime >= config.maxRuntime) {
Logger.log("WARNING: Self terminating script after " + runTime + "s")
return;
}
var messages = thread.getMessages();
for (var y=0; y<messages.length; y++) {
var message = messages[y];
Logger.log("INFO: Processing message: "+message.getSubject() + " (" + message.getId() + ")");
var att = message.getAttachments();
for (var z=0; z<att.length; z++) {
Logger.log("INFO: Processing attachment: "+att[z].getName());
try {
var file = folder.createFile(att[z]);
if (rule.filenameFrom && rule.filenameTo && rule.filenameFrom == file.getName()) {
var messageDate = message.getDate();
var d = messageDate.getDate();
var m = messageDate.getMonth()+1;
var y = messageDate.getFullYear();
file.rename(rule.filenameTo.replace('YYYY',y).replace('MM',(m<=9?'0'+m:m)).replace('DD',(d<=9?'0'+d:d)));
}
file.setDescription("Mail title: " + message.getSubject() + "\nMail link: https://mail.google.com/mail/u/0/#inbox/" + message.getId());
Utilities.sleep(config.sleepTime);
}
catch (e) {
Logger.log(e);
}
}
}
thread.addLabel(label);
if (doArchive) {
Logger.log("INFO: Archiving thread '" + thread.getFirstMessageSubject() + "' ...");
thread.moveToArchive();
}
}
}
end = new Date();
var runTime = (end.getTime() - start.getTime())/1000;
Logger.log("INFO: Finished mail attachment processing after " + runTime + "s.");
}
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
gmail2gdrive
Gmail2GDrive
============

Google Apps Script which stores and sorts Gmail attachments into Google Drive folders
Gmail2GDrive is a Google Apps Script which stores and sorts Gmail attachments into Google Drive folders.

It does so by defining a list of rules which consist of Gmail search filters and Google Drive destination folders.
This way the attachments of periodic emails can be automatically organized in folders without the need to install and run anything on the client.


Usage
-----

1. Open [Google Apps Script](https://script.google.com/).
2. Create an empty project.
3. Give the project a name (e.g. MyGmail2GDrive)
4. Replace the content of the created file Code.gs with the provided content.
5. Adjust the configuration in the function getGmail2GDriveConfig() to your needs. It is recommended to restrict the timeframe using 'newerThan' to prevent running into API quotas by Google.
6. Save the changes.
7. Test the script by manually executing the function performGmail2GDrive.


Configuration
-------------

The configuration parameters are documented in the files Code.gs.


Feedback and contributions
--------------------------

Direct any feedback and contributions directly to [Github](https://github.com/ahochsteger/gmail2gdrive).


Thanks
------

I'd like to thank [Amit Agarwal](http://www.labnol.org/about/) who provides similar functionality in his article [Send your Gmail Attachments to Google Drive](http://www.labnol.org/internet/send-gmail-to-google-drive/21236/) from which this script evolved to provide more flexibility.

0 comments on commit a171778

Please sign in to comment.