回复Gmail线程会向我自己发送电子邮件



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

跟踪一封电子邮件时,如果在最初的电子邮件之后没有回复,reply()会转到我自己,因为我是最后一封电子邮件的发送者。

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

我可以运行一个单独的sendmail,但这将启动一个新的线程,除非你可以指定回复头,或者用户已经对现有线程做出了响应,而在我的情况下,他们没有。

示例代码:

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

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

已更新如果您已经启动了消息&没有收到回复,您可能必须使用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);

我在测试中遇到的一件事是回复电子邮件大小的配额限制,因此在将邮件正文包含在回复文本中时需要谨慎。

希望这能有所帮助。

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

问题:我向收件人发送了一封电子邮件,然后再发送一封,但希望将其保持在同一个线程中,所以唯一的方法是通过回复,但回复会回复发件人(这是正确的)。在这种情况下,我是发件人,但没有收到回复,然后回复被视为对我的电子邮件的回复,这导致我的电子邮件被使用(原始发件人),这是不正确的。我希望它发送给原始收件人。奇怪的是,gmail UI足够聪明,当你在UI中单击"回复"时,它会自动回复原始人。但使用脚本应用程序就不那么智能了,它会回复发送者,在这种情况下,发送者是原始海报,而不是原始接收者。这有点像发送一封电子邮件,然后再发送另一封"邮件";凸起";当你们并没有得到回复时,给同一个人发电子邮件。或者只是发送另一封后续电子邮件而不等待回复。

无论如何,我发现你可以创建一个草稿回复发送,更新草稿,然后发送。所以类似这样的东西(在这种情况下,线程中只有一条消息):

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

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

我希望这能帮助任何在这种特殊情况下回复帖子的人。

相关内容

最新更新