google-apps-script - 如何使用 send Email(to,reply To, subject, body) 将电子邮件转发到另一个地址

标签 google-apps-script

如果您能提供帮助,我将不胜感激。

我需要在我的 gmail 1@example.com 中找到所有未读的电子邮件,并使用 sendEmail(to,replyTo, subject, body) https://developers.google.com/apps-script/reference/mail/mail-app 将它们全部发送到 2@example.com

我尝试编写一个脚本,但不幸的是它不需要工作。 希望你能帮上忙

function RespondEmail(e) {

//send response email
var threads = GmailApp.search("to:(1@example.com) label:unread");
for (var i = 0; i < threads.length; i++) {
threads[i].sendEmail("1@example.com",
               "2@example.com",
               "TPS report status",
               "What is the status of those TPS reports?")}


// mark all as read
var threads = GmailApp.search("to:(1@example.com) label:unread");
GmailApp.markThreadsRead(threads);
}

如果你能告诉我如何根据我在 1@example.com 上收到的原始电子邮件更改 ReplyTo 电子邮件的主题,我也将很高兴

最佳答案

您脚本中的问题在于 sendEmail() 属于 GmailApp 服务,因此始终需要按以下方式调用:

GmailApp.sendEmail()

根据您的需要,使用 forward() 方法可能更合适。

在下面的示例中,我添加了一个自定义主题,您可以根据需要对其进行编辑和调整。

function RespondEmail() {

  //send response email
  var threads = GmailApp.search("to:origin@gmail.com is:unread");
  var subject = "";
  var msg = "";
  var c = 0; // will be used to count the messages in each thread
  var t = "";
  var attachment = "";
  var forwarded = "";

  for (var i = 0; i < 3 /*threads.length*/ ; i++) { 
    // I set 'i' to 3 so that you can test the function on your 3 most recent unread emails.
    // to use it on all your unread email, remove the 3 and remove the /* and */ signs.                   

    t = threads[i]; // I wanted to avoid repetition of "threads[i]" for the next 2 lines haha
    c = t.getMessageCount() - 1;
    msg = t.getMessages()[c];
    forwarded = msg.getBody(); // that is the body of the message we are forwarding.

    subject = msg.getSubject();
    attachment = msg.getAttachments();

    msg.forward("destination@gmail.com", {
      replyTo: "origin@gmail.com",
      subject: "TPS report status of [" + subject + "]", // customizes the subject
      htmlBody: "What is the status of those TPS reports below?<br><br>" //adds your message to the body
        +
        "<div style='text-align: center;'>---------- Forwarded message ----------</div><br>" + forwarded, //centers
      attachments: attachment
    });
    t.markRead(); // mark each forwarded thread as read, one by one

  }
}

关于google-apps-script - 如何使用 send Email(to,reply To, subject, body) 将电子邮件转发到另一个地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43790496/

相关文章:

javascript - Google 表格中的日期/时间未将正确的时间传输到 Google 日历中

javascript - 将谷歌文档脚本调整为谷歌表格

google-apps-script - Google App Script Execution API 的正确范围?

google-apps-script - EmbeddedChartBuilder.setOption() 函数的许多图表选项不适用于 Google Apps 脚本

google-apps-script - 如何使用 Google Apps 脚本合并 'Google Slide' 中的 2 个表格单元格

javascript - Google 脚本编辑器 getRange 在 1 列上需要 10 秒,在另一列上需要 ~0 秒

google-apps-script - 在一行中的第一个空白单元格中设置时间戳

google-apps-script - Google 电子表格 - 如果有删除线则为行着色

google-apps-script - 在新编辑器中查找应用脚本的过去版本

javascript - 加快数组操作 Google Apps 脚本的执行速度