如何使用sendEmail(收件人,replyTo,主题,正文)将电子邮件转发到另一个地址 [英] How to forward email to another address with sendEmail(to,replyTo, subject, body)

查看:196
本文介绍了如何使用sendEmail(收件人,replyTo,主题,正文)将电子邮件转发到另一个地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您能提供帮助,我表示感谢.

I appreciate if you could help.

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

I need to find all unread emails in my gmail 1@example.com and send them all to 2@example.com by using sendEmail(to,replyTo, subject, body) https://developers.google.com/apps-script/reference/mail/mail-app

我试图编写一个脚本,但不幸的是它不起作用. 希望你能帮忙

I tried to write a script but unfortinately it does not want work. I hope you could help

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电子邮件的主题,我也将很高兴

I also would be happy if you could advise me how I can change the subject of the ReplyTo email according to original email which I receive on 1@example.com

推荐答案

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

The problem in your script lies in the fact that sendEmail() belongs to the GmailApp Service, so it always needs to be called the following way:

GmailApp.sendEmail()

GmailApp.sendEmail()

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

For your needs, it may be more appropriate to use the forward() method.

在下面的示例中,我添加了一个自定义主题,您可以对其进行编辑并使其适应您的需求.

In the following example, I added a custom subject which you can edit and adapt to your needs.

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

  }
}

这篇关于如何使用sendEmail(收件人,replyTo,主题,正文)将电子邮件转发到另一个地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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