使用Google Apps脚本移除Gmail电子邮件的附件 [英] Remove an attachment of a Gmail email with Google Apps Script

查看:240
本文介绍了使用Google Apps脚本移除Gmail电子邮件的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google Apps脚本( http://script.google.com ),我从文档,如何发送,转发,移动到垃圾邮件等,但我不知道找不到如何删除电子邮件附件,即: 在HTML或纯文本中都可以)

  • 保留原始发件人,保留收件人

  • 保留原始邮件日期/小时(重要! )

  • 删除附件

  • 如果无法通过API,is有一种方法可以将信息重新发送给我自己,同时保持1,2和3?




    注意: GmailAttachment 类看起来很有趣,并允许列出收件人:

      var threads = GmailApp.getInboxThreads(0,10); 
    var msgs = GmailApp.getMessagesForThreads(线程); (var i = 0; j var($ i = 0; i ) attachments = msgs [i] [j] .getAttachments();
    for(var k = 0; k Logger.log('Message'%s包含附件%s(%s bytes)',
    msgs [i] [j] .getSubject(),attachments [k] .getName(),attachments [k] .getSize());
    }
    }
    }

    但我找不到如何删除附件。



    注意:我已经研究了许多其他解决方案,因此我已经阅读了几乎所有关于此的解决方案(解决方案与专门的网络服务,与当地的客户,如雷鸟+附件提取插件等),但他们都不是真的很酷。这就是为什么我在寻找通过Google Apps脚本手动完成的解决方案。

    解决方案

    必须重新创建ish


    消息不可变:只能创建和删除消息。


    使用高级Gmail服务 Gmail API插入()你可以使用下面的方法绕过它: Gmail.Users.Messages.insert(resource,userId )



    这项高级服务 必须已启用 才能使用。 :[用 email_id 或以您希望获取电子邮件的任何方式填写 EMAIL_ID ]

     函数removeAttachments(){
    //获取`raw`电子邮件
    var email = GmailApp。 getMessageBy ID( EMAIL_ID)getRawContent()。

    //查找html或纯文本电子邮件的结束边界
    var re_html = /( - * \ w *)(\r)*(\\\
    )*( ?= Content-Type:text \ / html;)/。exec(email);
    var re = re_html || /( - * \ w *)(\r)*(\ n)*(?= Content-Type:text \ / plain;)/。exec(email);

    //找到消息边界的索引
    var start = re [1] .length + re.index;
    var boundary = email.indexOf(re [1],start);

    //删除附件&编码附件免费的RFC 2822格式的电子邮件字符串
    var base64_encoded_email = Utilities.base64EncodeWebSafe(email.substr(0,boundary));
    //将base64Encoded字符串设置为`raw`所需的属性
    var resource = {'raw':base64_encoded_email}

    //将电子邮件重新插入用户gmail插入时间的帐户
    / * var response = Gmail.Users.Messages.insert(resource,'me'); * /

    //以原始日期/时间重新插入电子邮件
    var response = Gmail.Users.Messages.insert(resource,'me',
    null, {'internalDateSource':'dateHeader'});
    $ b Logger.log(插入的电子邮件ID是:%s,response.id)
    }

    这将从电子邮件中删除附件并将其重新插入到您的邮箱中。



    编辑/更新: 新的RegExp可处理HTML和纯文本电子邮件 - 现在应该可以在多个边界字符串上工作


    Using Google Apps Script (http://script.google.com), I know from the docs, how to send, forward, move to trash messages, etc. but I don't find how to remove a file attachement of an email, i.e.:

    1. keep the text content (either in HTML or just plain text would be fine)
    2. keep the original sender, keep the recipient
    3. keep the original message date/hour (important!)
    4. remove the attachment

    If it's not possible via the API, is there a way to resend the message to myself, while keeping 1, 2 and 3?


    Note: the GmailAttachment class looks interesting and allows to list recipients:

    var threads = GmailApp.getInboxThreads(0, 10);
     var msgs = GmailApp.getMessagesForThreads(threads);
     for (var i = 0 ; i < msgs.length; i++) {
       for (var j = 0; j < msgs[i].length; j++) {
         var attachments = msgs[i][j].getAttachments();
         for (var k = 0; k < attachments.length; k++) {
           Logger.log('Message "%s" contains the attachment "%s" (%s bytes)',
                      msgs[i][j].getSubject(), attachments[k].getName(), attachments[k].getSize());
         }
       }
     }
    

    but I don't find how to remove an attachment.

    Note: I've already studied many other solutions for doing this, I've already read nearly every article about this (solutions with dedicated web services, with local clients like Thunderbird + Attachment extractor plugin, etc.), but none of them are really really cool. That's why I was looking for a solution to do it manually via Google Apps Script.

    解决方案

    Looks like messages will have to be re-created-ish:

    Messages are immutable: they can only be created and deleted. No message properties can be changed other than the labels applied to a given message.

    Using Advanced Gmail Service with the Gmail API insert() you can hack your way around it using: Gmail.Users.Messages.insert(resource, userId)

    This advanced service must be enabled before use.

    Example: [fill in the EMAIL_ID with an email_id or in whatever way you want to get the email]

    function removeAttachments () {
      // Get the `raw` email
      var email = GmailApp.getMessageById("EMAIL_ID").getRawContent();
    
      // Find the end boundary of html or plain-text email
      var re_html = /(-*\w*)(\r)*(\n)*(?=Content-Type: text\/html;)/.exec(email);
      var re = re_html || /(-*\w*)(\r)*(\n)*(?=Content-Type: text\/plain;)/.exec(email);
    
      // Find the index of the end of message boundary
      var start = re[1].length + re.index;
      var boundary = email.indexOf(re[1], start);
    
      // Remove the attachments & Encode the attachment-free RFC 2822 formatted email string
      var base64_encoded_email = Utilities.base64EncodeWebSafe(email.substr(0, boundary));
      // Set the base64Encoded string to the `raw` required property
      var resource = {'raw': base64_encoded_email}
    
      // Re-insert the email into the user gmail account with the insert time
      /* var response = Gmail.Users.Messages.insert(resource, 'me'); */
    
      // Re-insert the email with the original date/time 
      var response = Gmail.Users.Messages.insert(resource, 'me', 
                          null, {'internalDateSource': 'dateHeader'});
    
      Logger.log("The inserted email id is: %s",response.id)
    }
    

    This will remove the attachments from the email and re-insert it into your mailbox.

    edit/update: New RegExp to work with html&plain-text only emails - should now work on multiple boundary strings

    这篇关于使用Google Apps脚本移除Gmail电子邮件的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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