【问题标题】:How to forward email to another address with sendEmail(to,replyTo, subject, body)如何使用发送电子邮件将电子邮件转发到另一个地址(收件人、回复、主题、正文)
【发布时间】:2017-05-04 18:41:46
【问题描述】:

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

我需要在我的 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 上收到的原始电子邮件更改回复电子邮件的主题,我也会很高兴

【问题讨论】:

  • 当你说它不起作用时,它会用来包含你运行函数时发生的事情。你有错误吗?您做了哪些更改或修改以使其正常工作?

标签: google-apps-script


【解决方案1】:

你的脚本中的问题在于 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

  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2012-09-11
    • 2013-07-29
    • 2020-03-08
    • 1970-01-01
    相关资源
    最近更新 更多