Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nm authored and nm committed Apr 26, 2017
0 parents commit c4aab20
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions saveGmailAsEml.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function saveGmailAsEml(){
// Set the 'filter' variable to the Gmail-syntaxed search criteria for the target messages.
var filter = "-has:attachment in:inbox -has:label to:me older_than:21d after:2017/01/01";

// Set the 'start' variable to the integer for the index of the item to which the script shall apply.
// Index '0' starts the script at the first result.
var start = 0;

// Set the 'max' variable to the integer for the maximum number of results to which the script shall apply.
var max = 10;

// DO NOT ALTER. This sets the 'threads' variable to be the return of running GmailApp's 'search' method
// using the 'filter', 'start', and 'max' parameters you set above.
var threads = GmailApp.search(filter,start,max);
for(i in threads){
// The return of 'threads' is the array 'i', for each item of which the script shall apply the sub-function
// 'messages', which yields the return of running GmailApp's 'getMessages()' method on 'i'.
var messages = threads[i].getMessages();
for(j in messages){
// The return of 'messages' is the array 'j' for each item of which the script shall perform two methods:
// first, GmailApp's 'getId()' method to retrieve the Message-ID (held in the variable 'id');
var id = messages[j].getId();
// second, GmailApp's 'getRawContent()' method to retrieve the rfc822-standardized message content (held
// in the variable 'rawContent').
var rawContent = messages[j].getRawContent();
// Finally, using DriveApp's 'createFile' method, the script compiles the rfc822-content ('rawContent')
// and assigns a file name based on the Message-ID with the .eml extension ('id' + '.eml').
DriveApp.createFile(id + '.eml', rawContent, 'message/rfc822');
}
}
}

0 comments on commit c4aab20

Please sign in to comment.