Reputation: 31
Can I ask you for advice. I have a Google script that does this: 1) check whether there is any conversation marked by an asterisk 2) if so, deselect it, and then forward this conversation to that address
It works well, but I need to easily pimp. Rather than forward, I need to send the message to the address, which would be the subject of a report and found the body of a message by message body was found under the link to the thread found messages and the icing on the cake would have been references to attachments in the message.
function TJ() {
var threads = GmailApp.search('is:starred');
for (var h = 0; h < threads.length; h++) {
var messages = threads[h].getMessages();
for (var i = 0; i < messages.length; i++) {
if (messages[i].isStarred())
{
Logger.log(messages[i].getSubject());
messages[i].unstar();
messages[i].forward("--email-address--", {
cc: "",
bcc: ""
});
}
}
}
}
THX!
Upvotes: 0
Views: 6642
Reputation: 400
function autoForwardZoom() {
var s = "";
var m = "";
var c = 0;
var a = "";
var f = "";
var label = 'IT/Zoom';
var subject = 'Disconnected';
var from = '[email protected]';
var threads = GmailApp.search("from:" + from + " label:" + label + " is:unread subject:(" + subject + ")", 0, 1);
for (var i = 0; i < threads.length; i++) {
var message = threads[i].getMessages();
c = threads[i].getMessageCount() - 1;
m = threads[i].getMessages()[c];
b = m.getBody();
s = m.getSubject();
a = m.getAttachments();
var returnData = [];
var html = HtmlService.createTemplateFromFile('Temp');
html.data = returnData;
var template = html.evaluate().getContent();
m.forward("[email protected]", {
replyTo: "[email protected]",
subject: s,
htmlBody: template + "<br>" + b,
attachments: a
});
GmailApp.markThreadsRead(threads);
}
}
My HTML Template,
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div>Dear Team</div>
<div> </div>
<div>Please kindly check zoom controller issue</div>
<div> </div>
<div>Regards</div>
<div><i>please reply this email with word "SOLVED" to earn credits, once you has solved this issue</i></div>
<div> </div>
<div style='text-align: center;'>---------- Forwarded message from Zoom ----------</div>
</body>
</html>
Upvotes: 0
Reputation: 185
Please try the below code , You can add 5 mins time driven event trigger to run the script
function MailForward() {
try{
var glabel = createLabel_("Mail-Forwarded");
var rtm_email = '[email protected]';
var from_email = Session.getActiveUser().getEmail();
var threads = GmailApp.search('in:inbox is:unread newer_than:1d');
var mForward = 0;
for (var i=0;i<threads.length;i++) {
var messages=threads[i].getMessages();
for (var m = 0; m < messages.length; m++){
if (messages[m].isUnread()){
mForward = 0;
var mlabels = threads[i].getLabels();
for (var j = 0; j < mlabels.length; j++) {
Logger.log(mlabels[j].getName());
if (mlabels[j].getName() === "Mail-Forwarded") {
mForward = 1;
}
}
if (mForward===0) {
// Logger.log(messages.length)
// Logger.log(messages[m].getFrom());
var from = messages[m].getFrom();
//Logger.log(messages[m].getDate());
var date = messages[m].getDate();
// Logger.log(messages[m].getSubject());
var subject = messages[m].getSubject();
// Logger.log(messages[m].getTo());
var to = messages[m].getTo();
var body = messages[m].getBody();
var attachment = messages[m].getAttachments();
var emailoptions = ("---------- Forwarded message ----------" +'<br>'+'From: '+from+ "<'" + from.replace(/^.+<([^>]+)>$/, "$1") +"'>"+'<br>'+ 'Date: '+date+'<br>'+ 'Subject: ' +subject+'<br>'+
'To: ' +to+ "<'" + to.replace(/^.+<([^>]+)>$/, "$1") +"'>"+'<br>'+'<br>'+'<br>');
messages[m].forward(rtm_email,{htmlBody: emailoptions + body , Attachment: attachment});
glabel.addToThread(threads[i]);
Logger.log(glabel.getName());
messages[m].markRead();
mForward = 1;
}
}
}
}
} catch(e) {
MailApp.sendEmail("[email protected]", "Exception found in Sript", e );
Logger.log(e);
}
}
Upvotes: 0
Reputation: 31
This is perfect!
function AddSM2GTDApp() {
var threads = GmailApp.search('is:starred');
for (var h = 0; h < threads.length; h++) {
var messages = threads[h].getMessages();
for (var i = 0; i < messages.length; i++) {
if (messages[i].isStarred())
{
Logger.log(messages[i].getSubject());
var subject = messages[i].getSubject();
Logger.log(messages[i].getBody());
var body = messages[i].getBody();
Logger.log(messages[i].getId());
var id = messages[i].getId();
var label = GmailApp.getUserLabelByName("Add2GTDApp");
threads[h].addLabel(label);
messages[i].unstar();
MailApp.sendEmail({
to: "[email protected]",
subject: subject,
htmlBody: "https://mail.google.com/mail/u/0/#inbox/"+id+"<br>--------------------------------------------------------------------------------------<br>"+body+"<br>--------------------------------------------------------------------------------------<br>Mail to Nirvana Script ",
});
}
}
}
}
Upvotes: 3