Thursday, January 7, 2016

Scripts for Gmail :: Find If You Receive Too Big Attachment

Why not to set up the Gmail?

Write and run your scripts here: https://script.google.com/
Full documentation for gmail is here: https://developers.google.com/apps-script/reference/gmail/

You need to be sign into your gmail account, and if sending the e-mail it will be from your g-mail account.

function CheckSize() {

 var threads = GmailApp.getInboxThreads(0,20);

//Max of threads are 500 :o

 var msgs = GmailApp.getMessagesForThreads(threads);
  

 for (var i = 0 ; i < msgs.length; i++) {
   for (var j = 0; j < msgs[i].length; j++) {
     var attachments = msgs[i][j].getAttachments();
       
       for (var k = 0; k < attachments.length; k++) {
         var ssize=attachments[k].getSize();
       infosize="BIG ATTACHMENTS \n"

//Here the size is in bytes//

    if (ssize >= 2621440) {

       Logger.log('Message from date "%s", with name "%s" contains the attachment "%s" (%2.4s MB)',
                  msgs[i][j].getDate(), msgs[i][j].getSubject(), attachments[k].getName(), ssize/(1024*1024));

//Regarding the formatting %2.4s: 4 is the whole length including decimal point, and 2 is the number of decimal digits
     
    var infosize = infosize + Logger.getLog();

       } //endif
    
     } //for k
   } //for j
 } //for i
  if (infosize.length>20) {
  MailApp.sendEmail("xyz@xyz.com",
                    "WARNING :: BIG SIZE OF ATTACHMENT",
                   infosize);
  }
} //program

Small modifications: 

You can use GmailApp.search (it so useful utility!!!)

start=0
var threads = GmailApp.search('has:attachment larger:1m', label:unread', start, 25);
var msgs = GmailApp.getMessagesForThreads(threads);

Thank you google!

No comments:

Post a Comment