GoogleAppsScript GAS spam folder email forwarding

Forwarding Spam Folder Emails in Google Apps Script (GAS)

This article explains how to forward emails to the spam folder using GoogleAppsScript (GAS).

Shou Arisaka
2 min read
Nov 19, 2025

Forwarding Spam Folder Emails in Google Apps Script (GAS)

In JavaScript programming, this article introduces how to forward emails to the spam folder using GoogleAppsScript (GAS).

Script Overview

The following script shows how to move Gmail threads matching specific search criteria to the spam folder. The script searches for emails within a specific period and moves them to the spam folder.

function moveToSpam_main(term) {
  Logger.log("__log__");
  var myThreads = GmailApp.search(term, 0, 3);
  GmailApp.moveThreadsToSpam(myThreads);
}

function dev_main() {}

function main() {
  moveToSpam_main("newer_than:1d Help us protect your account");
  moveToSpam_main("newer_than:1d from:(ใƒคใƒžใƒˆ้‹่ผธ) ๅ—ๅ–ใ‚Šใฎๆ—ฅๆ™‚ใ‚„ๅ ดๆ‰€ใ‚’ใ”ๆŒ‡ๅฎšใใ ใ•ใ„");
  moveToSpam_main("newer_than:1d from:(ๆฅฝๅคฉ้Š€่กŒๆ ชๅผไผš็คพ) Visaใƒ‡ใƒ“ใƒƒใƒˆใ‚ซใƒผใƒ‰ๅผ•่ฝ");
  moveToSpam_main("newer_than:1d ใ‚ขใƒณใ‚ฑใƒผใƒˆใ”ๅ”ๅŠ›ใฎใŠ้ก˜ใ„");
  moveToSpam_main("newer_than:1d New message from AliExpress supplier");
  moveToSpam_main("newer_than:1d ้‹่กŒๆƒ…ๅ ฑ Yahoo!่ทฏ็ทšๆƒ…ๅ ฑ");
  moveToSpam_main("newer_than:1d from:(amaten.com OR ใ‚ขใƒžใƒ†ใƒณ) ใƒใƒฃใƒผใ‚ธ็”ณ่ซ‹ใ‚’ๅ—ไป˜ใ„ใŸใ—ใพใ—ใŸ");
}

Detailed Explanation

  1. moveToSpam_main Function: This function searches for Gmail threads and moves them to the spam folder. The term parameter specifies the search query.

    function moveToSpam_main(term) {
      Logger.log("__log__");
      var myThreads = GmailApp.search(term, 0, 3);
      GmailApp.moveThreadsToSpam(myThreads);
    }
  2. main Function: The main function uses multiple search queries to specify emails to move to the spam folder. It calls the moveToSpam_main function to move threads matching each search query to the spam folder.

    function main() {
      moveToSpam_main("newer_than:1d Help us protect your account");
      moveToSpam_main("newer_than:1d from:(ใƒคใƒžใƒˆ้‹่ผธ) ๅ—ๅ–ใ‚Šใฎๆ—ฅๆ™‚ใ‚„ๅ ดๆ‰€ใ‚’ใ”ๆŒ‡ๅฎšใใ ใ•ใ„");
      moveToSpam_main("newer_than:1d from:(ๆฅฝๅคฉ้Š€่กŒๆ ชๅผไผš็คพ) Visaใƒ‡ใƒ“ใƒƒใƒˆใ‚ซใƒผใƒ‰ๅผ•่ฝ");
      moveToSpam_main("newer_than:1d ใ‚ขใƒณใ‚ฑใƒผใƒˆใ”ๅ”ๅŠ›ใฎใŠ้ก˜ใ„");
      moveToSpam_main("newer_than:1d New message from AliExpress supplier");
      moveToSpam_main("newer_than:1d ้‹่กŒๆƒ…ๅ ฑ Yahoo!่ทฏ็ทšๆƒ…ๅ ฑ");
      moveToSpam_main("newer_than:1d from:(amaten.com OR ใ‚ขใƒžใƒ†ใƒณ) ใƒใƒฃใƒผใ‚ธ็”ณ่ซ‹ใ‚’ๅ—ไป˜ใ„ใŸใ—ใพใ—ใŸ");
    }

By executing this script, you can automatically move emails matching the specified search criteria to the spam folder. This saves you the trouble of manually managing unwanted emails.

Share this article

Shou Arisaka Nov 19, 2025

๐Ÿ”— Copy Links