获取通过谷歌脚本发送的邮件的线索ID [英] Getting thread id of a mail sent through google scripts

查看:130
本文介绍了获取通过谷歌脚本发送的邮件的线索ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能获得通过MailApp.sendEmail()发送的邮件的线程ID。我想在发送邮件之后用标签标记发送的邮件。

  MailApp.sendEmail(samplemail@gmail.com,试用者需要卖家,msg_to_bd); 
//获得此邮件的线程ID,例如线程1
thread1.addLabel(labll);


解决方案

首先,由于您想要将标签添加到线程您刚发送,您必须使用GmailApp。 MailApp只允许您发送邮件,而不是与用户的收件箱进行交互。

正如您所见,GmailApp.sendEmail()不返回消息或线程ID。在这种情况下,您可以搜索刚刚发送的线索,但必须说明您何时向该人发送了多条消息。



只要您不要很快发送重复的邮件,您可以依靠调用GmailApp.search()将以与Web UI相同的顺序返回线程。因此,搜索'from:me到:customer123@domain.net'可能会返回多个线索,但第一个结果将成为最近发送的消息的线索。



一个玩具的例子,我们发送邮件到一个名为收件人的列表中的一堆地址上:

var recipients = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName('Recipients')
.getDataRange()
.getValues();
recipients.shift(); //仅当收件人工作表包含标题时,您需要删除
var d = new Date();
var dateText = d.toLocaleString(); //漂亮的时间戳
var newLabel = GmailApp.createLabel(dateText); //标签对应于我们运行这个
的时候(var i = 0; i< recipients.length; i ++){
GmailApp.sendEmail(recipients [i],'This is a test'), '紧急广播系统');
var sentThreads = GmailApp.search('from:me to:'+ recipients [i]);
var mostRecentThread = sentThreads [0];
mostRecentThread.addLabel(newLabel);
}


Is it possible to get the thread id of a mail sent through MailApp.sendEmail().I want to tag the sent mail with a label just after it is sent.

MailApp.sendEmail("samplemail@gmail.com","Sellers Required for pilot",msg_to_bd);
//get thread id for this mail, say thread 1
thread1.addLabel(labll);

解决方案

First, since you want to add labels to the thread you just sent, you must be using GmailApp. MailApp only allows you to send mail, not interact with the user's inbox.

As you've seen, GmailApp.sendEmail() does not return a message or thread ID. In this case, you can search for the thread you just sent, but you must account for when you've sent several messages to this person.

As long as you are not sending duplicate mails very quickly, you can rely on the fact that a call to GmailApp.search() will return threads in the same order as the web UI. So a search for 'from:me to:customer123@domain.net' might return many threads, but the first result will be the thread for the most recently sent message.

A toy example where we send a mail to a bunch of addresses listed in a tab called Recipients:

var recipients = SpreadsheetApp.getActiveSpreadsheet()
                               .getSheetByName('Recipients')
                               .getDataRange()
                               .getValues();
recipients.shift();  // Only if the Recipients sheet contained a header you need to remove
var d = new Date();  
var dateText = d.toLocaleString(); // Pretty-printed timestamp
var newLabel = GmailApp.createLabel(dateText); // Label corresponding to when we ran this
for (var i = 0; i < recipients.length; i++) {
  GmailApp.sendEmail(recipients[i], 'This is a test', 'Of the emergency broadcast system');
  var sentThreads = GmailApp.search('from:me to:' + recipients[i]);
  var mostRecentThread = sentThreads[0];
  mostRecentThread.addLabel(newLabel);
}

这篇关于获取通过谷歌脚本发送的邮件的线索ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆