I am trying to figure out how we can forward the original message and include the recipient email, full chain, so for example:
---------- Forwarded message ---------
From: Ram Sam <[email protected]>
Date: Mon, Apr 4, 2022 at 5:42 PM
Subject: RE: about nurse appreciation day
To: [email protected] <[email protected]>
Receiver-
Sure, that sounds great! Also, share any flyers, brochures, etc. We do currently use
reward programs and this may be similar.
Thank you,
Ram Sam, PHR, SHRM-CP, FLWCS| Vice President of X
2033 Main St, Address| X Healthcare Management, LLC
I am also labeling the ones that were forwarded so they don't get reforwarded on the second run. Here is what I have so far
function Forward() {
var imessagesForwarded = 0;
var label = 'LeadTest';
var doneLabel = GmailApp.getUserLabelByName('LeadFinished');
var threads = GmailApp.getUserLabelByName(label).getThreads();
var iforwarded = 0;
for (var i = 0; i < threads.length; i++) {
var it = threads.length;
if (iforwarded == im ) {
threads[i-1].addLabel(doneLabel);
threads[i-1].removeLabel(GmailApp.getUserLabelByName(label));
}
var messages = threads[i].getMessages();
var im = messages.length;
for (var j = 0; j < messages.length; j++) {
var email = messages[j];
if (!email.isStarred()) { //To keep track of processed emails, I star message when processed.
try {
forwardTo = '[email protected]';
email.forward(forwardTo, {replyTo: email.getFrom(),subject: email.getSubject()});
messages[m].forward(rtm_email,{htmlBody: emailoptions + body , Attachment: attachment});
imessagesForwarded++;
email.star();
iforwarded++;
} catch(e) {
MailApp.sendEmail("[email protected]","Forward failed", "Errormsg:\n" + e.message);
continue;
}
} else {
iforwarded++;
}
}
}
if (iforwarded == im ) {
threads[i-1].addLabel(doneLabel);
threads[i-1].removeLabel(GmailApp.getUserLabelByName(label));
}
}