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
-
moveToSpam_main Function: This function searches for Gmail threads and moves them to the spam folder. The
termparameter specifies the search query.function moveToSpam_main(term) { Logger.log("__log__"); var myThreads = GmailApp.search(term, 0, 3); GmailApp.moveThreadsToSpam(myThreads); } -
main Function: The
mainfunction uses multiple search queries to specify emails to move to the spam folder. It calls themoveToSpam_mainfunction 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.