【问题标题】:Replying to Gmail thread sends email to myself回复 Gmail 线程会向我自己发送电子邮件
【发布时间】:2018-04-04 10:01:03
【问题描述】:

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

跟进一封电子邮件在最初的电子邮件之后没有回复时,reply() 会发给我自己,因为我是发送最后一封电子邮件的人。

例如,客户端 A 向客户端 B 发送电子邮件。客户端 A 运行下面的脚本并回复客户端 A,但它应该发送给客户端 B,因为它只是跟进,因为没有来自 B 的初始回复。至少这就是它在 Gmail 界面中的方式。我想发送跟进

我可以运行一个单独的发送邮件,但这会启动一个新线程,除非你可以specify the in-reply-to header 或者用户已经回复了现有线程,而在我的情况下他们没有。

示例代码:

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

但是,replyTo 实际上只是指定了正在发送的电子邮件的回复部分,即我们收到的响应。它与实际的to 字段又名收件人无关。

如果我使用replyAll,那么电子邮件的标题是:

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

因此,我收到了一堆来自自己的电子邮件。我尝试将 cc 指定为 none,但这并没有改变 cc 字段。

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

我如何跟进一封电子邮件,并将其发送给上一封电子邮件的原始收件人?

【问题讨论】:

  • @Desire 让我澄清一下。从 A 向 B 发送电子邮件。然后在 A 的帐户上运行脚本。
  • 我在这个相关的SO post 中遇到了这个问题。我使用getMessages()getTo() 来获取特定线程的电子邮件。您现在可以过滤它,这样您就不会回复自己的电子邮件。希望这会有所帮助。
  • @Desire 忽略那部分。这是文档中显示对象类型的示例的一部分。我调整了代码。
  • @Mr.Rebot 谢谢,但在我的示例中,线程中只有 1 封电子邮件,因此附加的 SO 帖子与我的不同。 B没有以前的通信。没有其他消息。 A 向 B 发送电子邮件。 A 运行上面的脚本,他在收件箱中收到电子邮件,但我希望它只发送到 B - 因为这就是跟进。当有人忘记回复时,您会发送一个跟进邮件,将其返回到他们收件箱的顶部。
  • @User 你找到解决方案了吗?我也有同样的问题,我有点想把头发拔掉。

标签: javascript google-apps-script


【解决方案1】:

已更新。 如果您已发起消息但未收到回复,您可能必须使用 GmailApp.search() 在已发送项目中找到它。收到消息后,您可以使用 message.forward() 模仿 Gmail 用户界面的行为。请注意,您不能使用message.forward() 修改电子邮件的文本正文,但您可以在选项对象中设置htmlBody

例如

  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});

否则,对于收件箱中的线程,您可以使用以下内容: 检查最新的并使用message.forward() 如上是您是发件人,否则,只需回复。

  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});
  }

或者,遍历线程并找到不是您的最新发件人并回复该电子邮件。

  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's not you
  while(email_re.test(messages[mIdx].getFrom())){ --mIdx;  }
  // now, just reply
  messages[mIdx].reply(followupText);

我在测试中遇到的一件事是回复电子邮件大小上的quota limit,因此在回复文本中包含消息正文时需要谨慎。

希望这会有所帮助。

【讨论】:

  • 对。该脚本的主要目的实际上是为了在这种情况下运行 onlysendEmail 的主要问题是它启动了一个新线程,而不是附加到现有线程。
  • 明白。我提供了第二个示例,它将通过查找除您之外的其他人发送的最新电子邮件来保留线程。有趣的是,第一种方法破坏了运行脚本的帐户的线程,但没有破坏 B 方。
  • 让我再说一遍,除了我之外没有其他发件人。没有。来自 OP:So for example, client A sends email to client B. Client A runs script below and reply goes to client A, but it should go to client B as it's only following up since there was no initial reply from B
  • 如果线程中只有一项已发送,我不确定您是否能够使用 getInboxThreads() 检索未答复的电子邮件。
【解决方案2】:

这有点晚了,但我正在寻找答案,并添加以防其他人正在搜索并遇到此问题。我不知道这是一个新选项还是一直存在,但我发现这是解决收件人是谁或在回复线程中更改收件人的能力的问题:

问题:我向收件人发送了一封电子邮件,然后又发送了一封,但希望将其保留在同一个线程中,因此唯一的方法是通过回复,但回复会回复发件人(正确)。在这种情况下,我是发件人并且没有收到回复,然后回复被视为对我的电子邮件的回复,这导致我的电子邮件被使用(原始发件人)这是不正确的。我希望将其发送给原始收件人。奇怪的是,gmail UI 足够聪明,当您在 UI 中单击回复时,它会自动回复原始人。但是使用脚本应用程序并不那么聪明,并且会回复发件人,在这种情况下,发件人是原始发件人而不是原始收件人。这有点像发送一封电子邮件,然后在您没有收到回复时向同一个人发送另一封“碰撞”电子邮件。或者只是发送另一封后续电子邮件而不等待回复。

无论如何,我发现您可以创建要发送的草稿回复,更新草稿,然后发送。 所以像这样(在这种情况下,线程中只有 1 条消息):

var thread = GmailApp.search("in:sent subject:" + subject, 0, 1);
var msgs = thread[i].getMessages();
var thedraft = msgs[0].createDraftReply("Doesnt matter what goes here",
  {
     htmlBody:"the email message for the body",
  }
);
       
thedraft.update(<recipientEmail>, <subject>, <message>); 
thedraft.send(); // if want to auto send. leave out to manually send

您可以从最初创建的草稿中获取主题和消息以重复使用,只需在第一个参数中更改收件人电子邮件即可。我对此进行了测试,现在当它盲目地抓取发件人电子邮件时,我可以覆盖它以使用实际的原始电子邮件收件人。

我希望这可以帮助任何在此特定情况下回复线程的相同问题的人。

【讨论】:

    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多