代码之家  ›  专栏  ›  技术社区  ›  tim

谷歌脚本:在新邮件中转发收到的邮件附件

  •  0
  • tim  · 技术社区  · 7 年前

    我想转发标记为star的某些邮件。我只想发送普通正文(而不是HTML正文),并转发最初附加的文件(如果有的话)。

    我当前的代码如下所示:

    function getMailsAndForward() {
      var thread, subject, body_PLAIN, emails, i;
    
      emails = GmailApp.search("in:SomeLabel AND in:starred");  
      var count = emails.length;
    
      if (count == 0)
        return; 
    
      for (i=0; i<count; i++) {    
        if (emails[i]) 
        {
          thread     = emails[i].getMessages()[0];
          subject    = "[AUTO-FORWARD] " + thread.getSubject();
          body_PLAIN = thread.getPlainBody(); 
    
          GmailApp.sendEmail("", subject, body_PLAIN, {"bcc": "xx@yy.com, fewmore@here.com"});
    
          // Unstar Mail... 
          GmailApp.unstarMessage(thread); 
        }
      }
    }  
    

    我在网上找到了一些解决方案,例如如何从收到的邮件中提取附件到GoogleDrive,以及如何将GoogleDrive文件附加到新创建的邮件。

    但是在走这条路之前,我想问一下: 有没有更简单的方法不需要事先将附件保存到GoogleDrive中的文件?很方便。。。

    提前感谢!

    1 回复  |  直到 7 年前
        1
  •  1
  •   Tanaike    7 年前

    下面的修改怎么样?

    您可以使用检索附件文件 getAttachments() 作为一个包含斑点的数组。带有BLOB的数组用于 GmailApp.sendEmail() . 这样,就不需要将附件文件作为文件保存到Google Drive。

    发件人:

    GmailApp.sendEmail("", subject, body_PLAIN, {"bcc": "xx@yy.com, fewmore@here.com"});
    

    收件人:

    GmailApp.sendEmail("", subject, body_PLAIN, {"bcc": "xx@yy.com, fewmore@here.com", "attachments": thread.getAttachments()});
    

    注:

    • 在这个修改后的脚本中,如果没有附件文件,则发送没有附件文件的邮件。

    如果我误解了你的问题,对不起。