0

Using labels and filters I set up a label for all incoming mail.This code 1. Generates an email receipt, generates, sends it // 2. Generates a task with the Username as the title, date, html link to the email, and email as the notesI am working from the dashboard, and have created the following script. However, it doesn't seem to refresh. I keep getting tasks and emails generated off of emails I received and removed the label from.


function EmailReceiptFunction(){
let receiptsub = "Email Receipt"; 
let receiptbody ="I've received your email!";
let lbltxt = 'zzCogEmail'; // cog= zzCogEmail intro=zzIntroEmail
let tlid_txt = 'tasklistid'; //
var rpljunk = ['Listerservecrap','othercrap','reply@aplace.com','"','"','>'];  //things I need to remove from the sender

var label = GmailApp.getUserLabelByName(lbltxt);  // Log the subject lines of the threads labeled with MyLabel
var threads = label.getThreads();

  For (var i = 0; i < threads.length; i++) { 
    var message = threads[i].getMessages()[i];  //get the message at index i of label array 
    var mydate = message.getDate();
    let a_name = message.getFrom();
    let mysub = message.getSubject();
    var mybody = message.getPlainBody();
    let myid = message.getHeader("Message-ID"); //this gets the messages id

    if (mysub.includes('TODAYS EMAIL CHECK')){  //This is supposed to eliminate the email that prints out my daily agenda of how to check my email
      message.removeLabel(label);
      }else if(mysub.includes(receiptsub)){
      message.removeLabel(label);
      }else{

        for (var s = 0; s < 10; s++){  //myAgenda.length, replaces double spaces and carriage returns in email body
          var mybody = mybody.replace(/\n|
          myid = myid.replace('\>','');
          myid = myid.replace('\<','');
          myid = 'https://mail.google.com/mail/u/0/?tab=wm#search/rfc822msgid:' + myid; //this creates a link in tasks to the email
          let tasknotes = mydate + '\n\n' + myid + '\n\n' + mybody;
            for (var r = 0; r < rpljunk.length; r++){
              a_name = a_name.replace(rpljunk[r],'');
               }
          a_name = a_name.replace('<',' - '); 
          let indexaname = a_name.split('-');
         var  j = indexaname.length - 1; 
          let mysender = indexaname[j]; //sets the last line of the array to string variable mysender
          var pos = mysender.lastIndexOf('@');
            if (mysender.includes('dontacceptemailfromme@gmail.com')){
              message.markRead();
              } else if(mysender.includes('dontacceptemailfromme@sc.edu')) {       
              message.markRead();
             } else if(mysender.includes('dontaccepteemailfromotherme')) {       
             message.markRead();
             }else if(pos < 4) {      
            message.markRead();
             } else {
    GmailApp.createDraft(mysender, receiptsub, receiptbody);
    var draft = GmailApp.getDrafts()[0]; // The first draft message in the drafts folder
    var msg = draft.send(); // Send it
   // Logger.log(tasknotes);
      var taskListId = tlid_txt; //tasklist id for CogRec
      var task = {
         title: mysender,
         notes: tasknotes,
         };
          //Adds a task with the email in this code created in IntroRec
         var taskListId = tlid_txt; 
         task = Tasks.Tasks.insert(task, taskListId);
         Logger.log(task);
         
      }  
 threads[i].removeLabel(label);
 threads[i].refresh();
  }
  }
 }

JenV
  • 21
  • 1
  • Have you tried to debug and see if the code is entering two times inside that loop, or the Task being insert? Could you maybe try to provide more information about your problem? – Raserhin Oct 13 '20 at 16:03
  • It doesn't seem to be looping, but the logs aren't propagating. I have an email that crops up in the tasklist 4-8 times. Not in succession. Searching on the label gives me no results. – JenV Oct 14 '20 at 13:14
  • It doesn't seem to be looping (logs aren't propagating). Here is what I've noticed about the behavior. 1. Email crops up in the tasklist several times, but not in succession. Searching on the label gives me no results. 2. Bad embedded logic (I checked each step loop from the inside out) 3. It could also be that my refresh is occurring too late to be useful. 4. Am I searching for threads beyond the inbox? 5. My big thoughts are that is has to do with the link I'm embedding in the notes. I set the embedded link before I remove the label (maybe the label lives on in something I don't see). – JenV Oct 14 '20 at 13:29
  • Okay, the problem appears to be that every time the code is triggered it searches all mail instead of inbox. How do I fix that? – JenV Oct 14 '20 at 13:49
  • Couldn't you just check the labels of the threads you are iterating ? – Raserhin Oct 14 '20 at 15:37
  • I do that. I left a filter in for debugging, and just realized that my email receipts were going there, and that seemed to be causing the problem. BUT, the receipts don't have the label. In addition, the email receipt doesn't go to the task list --> the original email does. – JenV Oct 14 '20 at 16:05
  • Hello there, sorry for the delay. Could you maybe post the logs for that execution? Is hard to follow the path of information flow without it. – Raserhin Oct 22 '20 at 15:03

0 Answers0