Thursday, September 17, 2015

Remove SPAM from gmail automatically 1

How to clear out your gmail spam box.

With a gmail account your life can become a lot easier... You make your rules and your mail gets cleaned of junk relatively easy. But what if we get a spammer that decides to send his mail via something else? This mail is called the via spam. Sadly enough google can not filter these mails out with their rules.

Do you have a regular load of spammers in your spam box? Well you can automatically clear them out.

Are your rules not catching a via spam? Well you can actually clear those out too.

All thanks to the totally great google apps.
All you need to do is add this script. You can do that by going to your google drive, in your My Drive right click and choose new file. Then choose google apps script and you are done. Just copy this code over what is given and save it under a name. You will see a little clock with a popup 'current program triggers' which you can use to setup a daily (which should suffice) trigger to walk through your spam box and basically delete all spam from which you know it is really junk spam. This way the other spam (which may inadvertently hold mail that are not spam) gets automatically deleted. Thanks google... :)


function FilterViaSpam() {
  //get all spam threads
  var threads= GmailApp.getSpamThreads();
  var spamcnt=0;
  for (var t=0;t<threads.length;t++){
    //get all messages
    var messages=threads[t].getMessages();
    for (var m=0;m<messages.length;m++){
      var mail_replyto=messages[m].getReplyTo();
      var mail_from=messages[m].getFrom();
      var mail_subject=messages[m].getSubject().toLowerCase();
      var mail_rawcontent=messages[m].getRawContent();  //the raw email
      var mail_rawcontent_lowercase=mail_rawcontent.toLowerCase();
      if (mail_from.indexOf('.gov.cn')!=-1){
        messages[m].moveToTrash();
      }else if (mail_from.indexOf('.ru')!=-1){
        messages[m].moveToTrash();
      }else if (mail_subject.indexOf('million ') || mail_subject.indexOf(' beneficiary') || mail_subject.indexOf(' nigeria')){
        messages[m].moveToTrash();
      }else if (mail_subject.indexOf('money gram')){
        messages[m].moveToTrash();
      }else if (mail_rawcontent.indexOf('Return-Path: <>')!=-1){
        messages[m].moveToTrash();
      }else if (mail_rawcontent.indexOf('User-Agent: Roundcube Webmail')!=-1){  //filter out spam mailer: User-Agent: Roundcube Webmail
        messages[m].moveToTrash();
      }else if (mail_rawcontent.indexOf('X-CTCH-Spam:  Bulk')!=-1){
        messages[m].moveToTrash();
      }else if (mail_rawcontent.indexOf('Received-SPF: softfail')!=-1){
        messages[m].moveToTrash();
      }else if (mail_rawcontent_lowercase.indexOf('lottery')!=-1){
        messages[m].moveToTrash();
      }else if (mail_rawcontent_lowercase.indexOf('inheritance')!=-1){
        messages[m].moveToTrash();
      }else if (mail_rawcontent_lowercase.indexOf('nigeria')!=-1){
        messages[m].moveToTrash();
      }else if (mail_rawcontent_lowercase.indexOf('bank draft')!=-1){
        messages[m].moveToTrash();
      }else if (mail_rawcontent.indexOf('Content-Type: application/msword;')!=-1){ //if there are attachments then doc and docx are not allowed
        messages[m].moveToTrash();
      }else if ((mail_rawcontent_lowercase.indexOf(' million ')!=-1) || (mail_rawcontent_lowercase.indexOf(' thousand ')!=-1)){
        if (mail_rawcontent_lowercase.indexOf(' usd')!=-1){
          messages[m].moveToTrash();
        }else if (mail_rawcontent_lowercase.indexOf(' dollar')!=-1){
          messages[m].moveToTrash();
        }
      }else if (mail_rawcontent_lowercase.indexOf(' western union')!=-1){
          messages[m].moveToTrash();
      }else if ((mail_rawcontent_lowercase.indexOf(' visa ')!=-1) || (mail_rawcontent_lowercase.indexOf(' master ')!=-1) || (mail_rawcontent_lowercase.indexOf(' atm card')!=-1)){
          messages[m].moveToTrash();
      }
    }
  }
}