'GMAIL EMAILS LABELLING AND MOVE TO LABELLED FOLDERS

I am trying to automate our daily incoming emails which consumes the whole inbox, What I want is to label all incoming different emails with almost common subjects and want to move it to label folder after but when I tried to create a script it does not have errors but error rate is 100%

function setUpTrigger() {

  ScriptApp.newTrigger('getGmailEmails')
  .timeBased()
  .everyMinutes(10)
  .create();


}

function getGmailEmails() {
const query = "from:[email protected] AND subject:Blocking of"; 
const query2 = "from:name3.com AND subject:Blocking of";
const query3 = "from:[email protected] AND subject:Blocking of";
const query4 = "from:[email protected] AND subject:Blocking of";
const query5 = "from:ticketing@ gmail.com AND subject:Block Malicious";
const query6 = "from:[email protected] AND subject:has been assigned to group your team";
const query7 = "from:[email protected] AND subject:Events";   
const query8 = "from:[email protected] AND subject:Events";
const query9 = "from:[email protected] AND subject:DAILY ENDORSED ACTIVITIES";
const query10 = "from:[email protected] AND subject:SLA Breached";



let trail = GmailApp.search(query);
let trail2 = GmailApp.search(query2);
let trail3 = GmailApp.search(query3);
let trail4 = GmailApp.search(query4);
let trail5 = GmailApp.search(query5);
let trail6 = GmailApp.search(query6);
let trail7 = GmailApp.search(query7);
let trail8 = GmailApp.search(query8);
let trail9 = GmailApp.search(query9);
let trail10 = GmailApp.search(query10);


let label10 = GmailApp.getUserLabelByName("Services/SLA");
for (var o = 0; o < trail10.length; o++) {


  let messages10 = [];

trail10.forEach(thread => {
    messages10.push(thread.getMessages()[0].getPlainBody());
    label10.addToThread(thread);

  Logger
});



let label9 = GmailApp.getUserLabelByName("SIZE/SIZE IP");
for (var n = 0; n < trail9.length; n++) {


  let messages9 = [];

trail9.forEach(thread => {
    messages9.push(thread.getMessages()[0].getPlainBody());
    label9.addToThread(thread);
});

let label8 = GmailApp.getUserLabelByName("Detected/Events");
for (var m = 0; m < trail8.length; m++) {

  let messages8 = [];

trail8.forEach(thread => {
    messages8.push(thread.getMessages()[0].getPlainBody());
    label8.addToThread(thread);
});


let label7 = GmailApp.getUserLabelByName("Detected/Events");
for (var l = 0; l < trail7.length; l++) {

  let messages7 = [];

trail7.forEach(thread => {
    messages7.push(thread.getMessages()[0].getPlainBody());
    label7.addToThread(thread);
});

let label6 = GmailApp.getUserLabelByName("SURGE/Services");
for (var f = 0; f < trail6.length; f++) {

  let messages6 = [];

trail6.forEach(thread => {
    messages6.push(thread.getMessages()[0].getPlainBody());
    label6.addToThread(thread);
});

let label5 = GmailApp.getUserLabelByName("Detected/Detection");
for (var g = 0; g < trail5.length; g++) {

  let messages5 = [];

trail5.forEach(thread => {
    messages5.push(thread.getMessages()[0].getPlainBody());
    label5.addToThread(thread);
});

let label4 = GmailApp.getUserLabelByName("Detected/Detection");
for (var h = 0; h < trail4.length; h++) {

  let messages4 = [];

trail4.forEach(thread => {
    messages4.push(thread.getMessages()[0].getPlainBody());
    label4.addToThread(thread);
});

let label3 = GmailApp.getUserLabelByName("Detected/Detection");
for (var j = 0; j < trail3.length; i++) {

  let messages3 = [];

trail3.forEach(thread => {
    messages3.push(thread.getMessages()[0].getPlainBody());
    label3.addToThread(thread);
});
let label2 = GmailApp.getUserLabelByName("Detected/Detection");
for (var i = 0; i < trail2.length; i++) {

  let messages2 = [];

trail2.forEach(thread => {
    messages2.push(thread.getMessages()[0].getPlainBody());
    label2.addToThread(thread);
});


let label = GmailApp.getUserLabelByName("Detected/Detection");
for (var k = 0; k < trail.length; k++) {
  

let messages = [];

trail.forEach(thread => {
    messages.push(thread.getMessages()[0].getPlainBody());
    label.addToThread(thread);
    
});


return messages;



}
}
}
}
}
}
}
}
}
}
}

I am trying to automate our daily incoming emails which consumes the whole inbox, What I want is to label all incoming different emails with almost common subjects and want to move it to label folder after but when I tried to create a script it does not have errors but error rate is 100%



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source