简体   繁体   English

MailApp AppScript:如何使用 MailApp 将 email 发送到线程?

[英]MailApp AppScript: How to send an email to a thread using MailApp?

I am using mailApp class of Appscript to send daily emails as shown below:我正在使用Appscript的 mailApp class 发送每日电子邮件,如下所示:

MailApp.sendEmail({to: emails[0],subject:subjectLine,htmlBody: htmlContent,cc: emails.slice(1,emails.length).join(",")});

What I observe is that for subjectline = "TestEmail" if sent more than once it gets sent to a new thread:我观察到,对于subjectline = "TestEmail"如果多次发送它会被发送到一个新线程:

示例图像

What I actually want is to instead of MailApp creating a new thread, it must send it to the old thread if the subject line is the same.我真正想要的是不是 MailApp 创建一个新线程,如果主题行相同,它必须将其发送到旧线程。 Like for example: subjectline = "TestEmail" then all the mails within this subject should fall under the same and not dispersed as shown above.例如: subjectline = "TestEmail"那么这个主题内的所有邮件都应该属于同一个而不是分散的,如上所示。 And only when the subject line changes they should fall into a new email thread.并且仅当主题行更改时,它们才应落入新的 email 线程中。

Has anyone faced the same problem?有没有人遇到过同样的问题? Does anyone know any work around for the same?有谁知道同样的解决方法? Please let me know if you have a solution for the same.如果您有相同的解决方案,请告诉我。

Thanks in Advance.提前致谢。

Class MailApp Class MailApp

  • This service allows scripts to send email on a user's behalf.此服务允许脚本代表用户发送 email。 See also Gmail Service.另请参阅 Gmail 服务。 Unlike Gmail Service, Mail Service's sole purpose is sending email;与 Gmail Service 不同,Mail Service 的唯一目的是发送 email; it cannot access a user's Gmail account.它无法访问用户的 Gmail 帐户。

Workaround:解决方法:

You can prevent creating a new thread when sending emails using Gmail Service您可以在使用 Gmail 服务发送电子邮件时防止创建新线程

Gmail Service Gmail 服务

  • This Service lets you send email, compose drafts, manage labels, mark messages and threads, and conduct a variety of other Gmail account management tasks.此服务允许您发送 email、撰写草稿、管理标签、标记消息和线程,以及执行各种其他 Gmail 帐户管理任务。

What you need to do:你需要做什么:

  1. Search for a thread with a specific subject搜索具有特定主题的线程
  2. Reply to that thread回复那个帖子

Sample Code:示例代码:

  var thread = GmailApp.search('in:anywhere subject:"Test_Email1" ')[0];
  Logger.log(thread);
  
  var invalidThread = GmailApp.search('in:anywhere subject:"Test_Email" ')[0];
  Logger.log(invalidThread);
  
  if (thread){
    thread.reply("This is a reply");
  }else{
    Logger.log("Subject not found in mail");
  }

  var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
  Logger.log("Remaining email quota: " + emailQuotaRemaining);

GmailApp.search() lets you search for specific queries in Gmail. GmailApp.search()可让您搜索 Gmail 中的特定查询。 This will return a GmailThread[] type which is an array of Gmail threads matching this query这将返回一个GmailThread[]类型,它是与此查询匹配的 Gmail 线程数组

  • In this sample code, I already created a Thread with Subject:"Test_Email1" and tried to access that thread using the Gmail search operator "in:anywhere subject:"Test_Email1""在这个示例代码中,我已经创建了一个主题为“Test_Email1”的线程,并尝试使用Gmail 搜索运算符“in:anywhere subject:”Test_Email1“”访问该线程
  • This search operator will search for threads with Test_Email1 subject anywhere in your Gmail, including spam and thrash folder.此搜索运算符将在 Gmail 的任何位置搜索主题为 Test_Email1 的线程,包括垃圾邮件和 thrash 文件夹。
  • I also included a sample code where the subject "Test_Email" doesn't exist which will return an empty array.我还包含了一个示例代码,其中主题“Test_Email”不存在,它将返回一个空数组。
  • Once you have found your thread, you can use GmailThread.reply() to reply in your current thread.找到线程后,您可以使用GmailThread.reply()在当前线程中回复。

There are other reply options available in GmailThread. GmailThread 中还有其他回复选项。 For more information, you can visit this link: https://developers.google.com/apps-script/reference/gmail/gmail-thread有关更多信息,您可以访问此链接: https://developers.google.com/apps-script/reference/gmail/gmail-thread

By this, you may decide whether you need to create a new thread or reply with an existing thread in your Gmail.这样,您可以决定是否需要在 Gmail 中创建新线程或使用现有线程回复。


Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Google Apps 脚本中使用 MailApp 发送带有附件的 HTML email? - How to send a HTML email with attachment using MailApp in Google Apps Script? 如何使用 MailApp 在电子邮件中包含内嵌图像 - How to include inline images in email using MailApp 如何使用 mailapp 发送图像和图表? - How to send image and charts using mailapp? 您如何使用 google Mailapp 回复电子邮件主题? - How do you reply to email thread with google Mailapp? 使用MailApp将链接嵌入电子邮件中 - Embedding a link in an email using MailApp 如何使用 Gmail email 别名发送批量电子邮件(将 Mailapp.SendEmail 更改为 Gmailapp.SendEmail) - How to send bulk emails using a Gmail email alias (changing Mailapp.SendEmail to Gmailapp.SendEmail) 在没有托管电子邮件的情况下使用 GSuite,如何使用 gscript MailApp 发送消息 - Using GSuite without hosted email, how to send messages using gscript MailApp 使用 mailApp 发送带有图像和其他内容的电子邮件 - Use mailApp to send email with images and other from 如何使用MailApp.sendEmail发送整个工作表 - how to send a whole sheet using MailApp.sendEmail MailApp.sendEmail =:在通过 MailApp 发送 email 之前验证用户输入 email 的可用性 - MailApp.sendEmail =: validate the input email availability from user before send email through MailApp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM