如何创建带有特定标签的 Gmail 邮件?

Gre*_*ott 3 gmail google-reader google-api gmail-imap google-apps-script

我一直在使用RSS2Email 将 Gmail 变成 Google Reader。

我想创建一个 Google Apps 脚本来执行此操作,以获得两个优势:

  1. 调度将由 Google Apps 脚本处理。(无需使用专用计算机来运行脚本。)

  2. 由此产生的电子邮件将被标记为 per-feed,很好地组织事物。

使用 Google Apps Script 与Gmail Snooze具有相同的实现精神。


我了解如何获取提要。我遇到的问题是如何使用特定标签创建电子邮件。到目前为止我研究过的方法:

  1. Google Apps Scripts的Gmail 服务不允许这样做,至少显然不允许。
    • GmailApp.sendEmail不允许您指定标签。这是有道理的,因为这是用于通用电子邮件发送。但 ...
    • GmailApp.sendEmail 返回 GmailApp,而不是您可以用来标识消息并稍后更改其标签的东西。
    • 该服务似乎也不允许您以编程方式创建 Gmail 过滤器,因此排除了解决此问题的另一种可能方法。
  2. 在Gmail中迁移API将是完美的做到这一点-但它不工作正常,消费者的Gmail帐户。嘎。
  3. Google 的IMAP 扩展可能允许这样做,但我不清楚。

我想我可以使用 1,将一些 UID 放在我可以用来查找传递的消息的主题中,添加标签(并希望通过删除 UID 来取消主题的丑化)。但这似乎有点笨拙。

使用 3,IMAP 扩展似乎可能不那么笨拙,尽管编码和测试的工作可能要多得多。

其中有什么推荐吗?是否有其他 API 或策略?

Mog*_*dad 5

Google Apps Script GmailApp API 没有提供在您发送邮件时为其添加标签的方法,您可以从 UI 执行此操作。这在问题跟踪器中显示为问题 1859,请访问并为更新加星标。

解决方法是利用线程 api。如果邮件被密件复制(bcc'd)给发件人,它也会在他们的收件箱中显示为新邮件,我们可以在那里getInboxThreads()找到它。将标签添加到此线程,并可选择将其标记为已读,然后将其存档。

代码也可作为 gist 使用。

/**
 * An alternative to GmailApp.sendEmail(), which applies a
 * label to the message thread in the sender's account.
 *
 * Sends an email message with optional arguments. The email can
 * contain plain text or an HTML body. The size of the email
 * (including headers, but excluding attachments) may not
 * exceed 20KB.
 *
 * @param {String} recipient    the addresses of the recipient
 * @param {String} subject      the subject line
 * @param {String} body         the body of the email
 * @param {Object} options      a JavaScript object that specifies
 *                              advanced parameters, as documented
 *                              for GmailApp.sendEmail()
 * @param {String} label        the label to be applied
 */
function sendAndLabel(recipient, subject, body, options, label) {
  var sender = Session.getActiveUser().getEmail();

  // Add sender to bcc list
  if (options.bcc) {
    options.bcc = options.bcc.split(',').concat(sender).join(',');
  }
  else {
    options.bcc = sender;
  }

  GmailApp.sendEmail( recipient, subject, body, options )

  // Check if label already exists, create if it doesn't
  var newLabel = GmailApp.getUserLabelByName(label);
  if (!newLabel) newLabel = GmailApp.createLabel(label);

  // Look for our new message in inbox threads
  Utilities.sleep(2000); // Wait for message to be received
  var inboxThreads = GmailApp.getInboxThreads();
  for (var t = 0; t < inboxThreads.length; t++) {
    var foundSubject = inboxThreads[t].getFirstMessageSubject();
    var numLabels = inboxThreads[t].getLabels().length;  // Could add more criteria
    if (foundSubject === subject && numLabels === 0) {
      // Found our thread - label it
      inboxThreads[t].addLabel(newLabel)
                     .markRead()
                     .moveToArchive();
      break;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

使用示例:

function test_sendAndLabel() {
  var recipient = Session.getActiveUser().getEmail();
  var subject = "Test Labelling";
  var body = "This email is testing message labelling.";
//  var options = {bcc:"someone@example.com"};
  var options = {};
  var label = "LabelTest";
  sendAndLabel(recipient, subject, body, options, label);
}
Run Code Online (Sandbox Code Playgroud)