Categories
Code

Gmail Search by Size with Google Apps Script

The following Google Apps Script will help you find all Gmail messages that have file attachments greater than 1 MB. Should be useful when you are running out of space in Gmail.

Also see: Sort Gmail Messages by Size


function Scanning_Gmail_Mailbox() {    
  
  if (!UserProperties.getProperty("start")) {
    UserProperties.setProperty("start", "0");    
  }
  
  var start   = parseInt(UserProperties.getProperty("start"));
  var sheet   = SpreadsheetApp.getActiveSheet();
  var row     = getFirstRow();
  var ss      = SpreadsheetApp.getActiveSpreadsheet()
  
  for (;;) {
  
  ss.toast("Now finding all the big emails in your Gmail mailbox. Please wait..", "Scan Started", -1);
  
  // Find all Gmail messages that have attachments
  var threads = GmailApp.search('has:attachment larger:1m', start, 100);
  
  if (threads.length == 0) {
    ss.toast("Processed " + start + " messages.", "Scanning Done", -1); 
    return;
  }
    
  for (var i=0; i 1 MB, log the messages
     // You can change this value as per requirement.
      
      if (size >= 1) {      
        sheet.getRange(row,1).setValue(Utilities.formatDate(messages[m].getDate(),"GMT", "yyyy-MM-dd"));
        sheet.getRange(row,2).setValue(messages[m].getFrom());        
        sheet.getRange(row,3).setValue(messages[m].getSubject());
        sheet.getRange(row,4).setValue(size);        
        var id = "https://mail.google.com/mail/u/0/#all/" + messages[m].getId();
        sheet.getRange(row,5).setFormula('=hyperlink("' + id + '", "View")'); 
        row++;
      }
    }            
  }    
  }
}


// Compute the size of email attachments in MB

function getMessageSize(att) {
  var size = 0;
  for (var i=0; i