google-apps-scriptgmailspamemail-spam

Trying to auto-move 'Via' spam from Spam folder to trash in my Gmail


So, guess I'm odd or something. I dun like to even see any spam in my spam folder. AT ALL. As in, I want that folder to only occasionally get something due to an error on the sender's behalf, or just nothing there. I like to keep things tidy, I suppose. However, these days, I keep getting spam with this annoying 'via' tag, and there's just no way to filter those addresses, like... no-reply@zeazpokjyelp.bodeel.delicorunni.eu or something like contact@vjtsk.hazydragon.bg.caughtupinyourthoughts.com.

They just randomize it over and over again, but, they have -one- thing in common. They are -all- sent 'via mydns.jp' [A few random other addresses before it, but, the root of it is that 'myDNS' address.]

I would create a filter, and tell it to block anything from said forwarding address... but, as I am painfully aware, it seems Google doesn't have that function in it's blocking feature.

So, I tried to make something on my own, however, that's built to move junk mail from the INBOX to TRASH. Google already knows it's spam, and is labeled/moved into the Spam folder, I just dun wanna see it at all, and do not pass go, do not collect $200, just go to the trash.

I tried to edit the script found here; https://www.geektron.com/2014/01/how-to-filter-gmail-using-email-headers-and-stop-via-spam/ but, again, that's built to move mail from my Inbox to my Spam. I dun wanna see it at all.

function filterViaSpam() {
  var threads = GmailApp.getSpamThreads();
  for (var i = 0; i < threads.length; i++) {
    var messages=threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
      var message=messages[j];
      var body=message.getRawContent();
      if(body.indexOf("X-Forwarded-For: *@*.mydns.jp")>-1){
        GmailApp.moveThreadToTrash(threads[i]);
      }
      Utilities.sleep(1000);
    }
  }
}    

..well, nothing happens. Guess the script doesn't see any emails, but they exist in my Spam folder.

Per request, I logged the output. This is what I got:

[19-07-19 03:31:47:424 PDT] -1.0
[19-07-19 03:31:48:797 PDT] -1.0
[19-07-19 03:31:50:186 PDT] -1.0
[19-07-19 03:31:51:516 PDT] -1.0
[19-07-19 03:31:52:943 PDT] -1.0
[19-07-19 03:31:54:252 PDT] -1.0
[19-07-19 03:31:55:598 PDT] -1.0
[19-07-19 03:31:56:970 PDT] -1.0
[19-07-19 03:31:58:304 PDT] -1.0
[19-07-19 03:31:59:698 PDT] -1.0
[19-07-19 03:32:01:001 PDT] -1.0
[19-07-19 03:32:02:358 PDT] -1.0
[19-07-19 03:32:03:673 PDT] -1.0
[19-07-19 03:32:05:062 PDT] -1.0
[19-07-19 03:32:06:411 PDT] -1.0
[19-07-19 03:32:07:731 PDT] -1.0
[19-07-19 03:32:09:087 PDT] -1.0
[19-07-19 03:32:10:390 PDT] -1.0
[19-07-19 03:32:11:678 PDT] -1.0

Solution

  • Issue:

    Solution:

    Snippet:

    if(/X\-Forwarded\-For: .*?@.*?\.mydns\.jp/.test(body)){
    

    References: