The response to the Gmail stream sends me an email

Documentation: https://developers.google.com/apps-script/reference/gmail/gmail-message#replybody-options

When it tracks an email that doesn't have a response after the initial email, reply() goes to itself, given that I'm the one who sent the last email.

So, for example, client A sends an email to client B. Client A runs the script below, and the response is sent to client A, but it should go to client B as it only tracks, since there was no initial response from B. At least how this happens in the Gmail interface. I want to send a report.

I could start a separate sendmail, but this will start a new thread if you cannot specify a header in the response message or the user answered an existing thread that they don’t have in my case.

Code example:

  message.reply("incapable of HTML", { htmlBody: "<b>some HTML body text</b>", replyTo: theirEmailAddress, }); 

However, replyTo actually just indicates the response to part of the message being sent, so the response we receive. This has nothing to do with the actual field to aka recipient.

If I do replyAll then the header for the email is:

 from: me@me.com to: them@them.com cc: me@me.com 

So, from this I get a bunch of letters from myself. I tried to specify cc as nobody, but that did not change the cc field.

 threads[0].replyAll("Just wanted to follow up and see if you got my last email.", { htmlBody: followUpText, cc: null, }); 

How do I track email and send it to the original recipient of the last message?

+5
source share
1 answer

Updated. If you initiated a message and didn’t receive a reply, you probably have to use GmailApp.search() to find it in the sent items. After you receive the message, you can simulate the behavior of the Gmail interface with message.forward() . Note that you cannot change the body of the message body using message.forward() , but you can set htmlBody in the parameter object.

eg.

  var unanswered = GmailApp.search('subject:"unanswered" in:sent'); var messages = unanswered[0].getMessages(); var lastMsg = messages[messages.length - 1]; // get the last message in the thread, just in case you have sent multiple reminders with different conent // set your followup text. // + Note that message.forward() doesn't append the thread to the reply, so you'll have to do this yourself var followupHTML = '<p>Your followup message.</p><div class="gmail_quote">' + lastMsg.getBody() + '</div>'; lastMsg.forward(lastMsg.getTo(), {subject: lastMsg.getSubject(), htmlBody: "<p><b>forwarded</b></p>" + followupHTML}); 

Otherwise, for streams present in the Inbox, you can use something like this: Check the latter and use message.forward() , as indicated above, this is the sender, otherwise just reply.

  var firstThread = GmailApp.getInboxThreads(0,1)[0]; var messages = firstThread.getMessages(); var lastMsg = messages[messages.length - 1]; // get the last message in the thread // set your followup text. // + Note that message.reply() & message.forward()` don't append the thread to the reply, so you'll have to do this yourself -- both text & HTML var followupText = "Your followup text.\n" + (lastMsg.getPlainBody()).replace(/^/gm, "> "); var followupHTML = '<p>Your followup message.</p><div class="gmail_quote">' + (lastMsg.getBody()) + '</div>' var email_re = new RegExp(Session.getActiveUser().getEmail(), "i"); if(email_re.test(lastMsg.getFrom())){ lastMsg.forward(lastMsg.getTo(), {lastMsg.getSubject(), htmlBody: followupHTML}); } else{ lastMsg.reply(followupText, {htmlBody: followupHTML}); } 

Alternatively, move the stream and find the last sender, not you, and reply to this email.

  var firstThread = GmailApp.getInboxThreads(0,1)[0]; var messages = firstThread.getMessages(); var followupText = "Your followup text."; var email_re = new RegExp(Session.getActiveUser().getEmail(), "i"); var mIdx = messages.length - 1; // last message index // walk backward through the thread & return the array index of the most recent sender that not you while(email_re.test(messages[mIdx].getFrom())){ --mIdx; } // now, just reply messages[mIdx].reply(followupText); 

One thing that I stumbled upon in my testing was limiting the size of the email quota for the response, so some caution is required when including the body of the message in the response text.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1272832/


All Articles