Search code examples
javascriptgoogle-apps-scriptgmailemail-attachments

Google app Script delete specific attachment from gmail


I'm bit new to google app script and try figure out how to resolve my google mail problem with google app script


Update : only particular attachment need to be remove rest of the structure and attachment need to same. provided solution remove all the attachments !

Problem :

I daily get email with pdf attachments from my vendors and clients some are send to me using automatic systems and some from manual some of this email has attachment called "noname" which I cant open or read. I'm using automated application to download this pdf attachments when there is "noname " attachment that email fully ignored so I miss some pdf daily


Planed Solution :

using google app script I'm try to capture the "noname" attachment and delete only that file rest of the email content and pdf need to be same .

I have manage to implement code to capture specific email which send noname attachment and if they unread then check attachments and delete noname


Where I need advice : how to delete selected attachment ??

function myFunction() {

  Logger.log("unread emails: " +
             GmailApp.search('is:unread from:[email protected]'));

  var deta = GmailApp.search('is:unread from:[email protected]')

  var msgs = GmailApp.getMessagesForThreads(deta);

        for (var i = 0 ; i < msgs.length; i++) {
              Logger.log("msg object lenth " + msgs.length)

            for (var j = 0; j <  msgs.length; j++) {
                Logger.log("msg i object lenth " + msgs[i].length)
                var attachments = msgs[i][j].getAttachments();
                var m_id = msgs[i][j].getId()

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

                      if (attachments[k].getName() == "noname" ){

                     //  attachments[k].deleteAttachment();
                    Logger.log('Message "%s" contains the attachment "%s" (%s bytes)',  msgs[i] 
                      [j].getSubject(), attachments[k].getName(),attachments[k].getSize());

              }       
   }
  }
 }
}

Solution

  • GMail messages are immutable - that is, you cannot modify them. However, you can re-create them by downloading their content, modifying it, and inserting the modified message back into your account.

    I have modified your code so that it does what you asked for, using the method explained. It iterates over the search results and checks whether the message contains a filename called noname. In case it does, it inserts a modification of the original message that does not contain the noname attachment.

    Additionally, you may opt to uncomment two different lines so that:

    1. The new message gets inserted with INBOX label (so that it shows up in your inbox) and UNREAD labels.
    2. The original message gets trashed after inserting the new one.
    var BOUNDARY_REGEX = new RegExp('boundary=\"(.+)\"');
    
    function removeAttachment(msg) {
      var emailContent = msg.getRawContent();
      if (emailContent.indexOf('filename=noname') == -1) return;
    
      var boundary = BOUNDARY_REGEX.exec(emailContent)[1];
      var boundaryLine = '--' + boundary;
    
      var sections = emailContent.split(boundaryLine);
    
      var filteredSections = sections.filter(function(value, index, arr) {
        return value.indexOf('filename=noname') == -1;
      });
    
      var resultBody = filteredSections.join(boundaryLine);
      var encodedResultBody = Utilities.base64EncodeWebSafe(resultBody);
      var resource = {'raw': encodedResultBody};
      // OPTIONAL - LABEL YOUR NEWLY CREATED MAIL AS 'INBOX' & 'UNREAD'
      // resource['labelIds'] = ['INBOX', 'UNREAD'];
      var response = Gmail.Users.Messages.insert(resource, 'me', null, {'internalDateSource': 'dateHeader'});
      Logger.log("Inserted email id is: %s", response.id);
      // OPTIONAL - TRASH ORIGINAL MESSAGE
      // GmailApp.moveMessageToTrash(msg);
      return response.id;
    }
    
    function myFunction() {
      Logger.log("unread emails: " +
                 GmailApp.search('is:unread from:[email protected]'));
    
      var deta = GmailApp.search('is:unread from:[email protected]')
    
      var msgs = GmailApp.getMessagesForThreads(deta);
    
      for (var i = 0 ; i < msgs.length; i++) {
        for (var j = 0; j < msgs[i].length; j++) {
          Logger.log("msg object length " + msgs[j].length);    
          removeAttachment(msgs[i][j]);
        }
      }
    }
    

    Before executing this code, please make sure that you have the GMail Advanced service enabled.