Search code examples
google-apps-scriptgmailemail-attachments

How to make getAttachments() in gmailapp to exclude images from signature?


I am trying to read Gmail attachments and push it to Google Drive. It works great except for one minor issue/behavior.

Whenever I call the getAttachments() method, it includes the inline signature image which is irrelevant in the drive folder. Is there any way to exclude the signature image (or inline images altogether) so that I will be able to push only the attached files?

Below is my code if you want to review.

var threads = myLabel.getThreads(0,500); 
for (var threadIdx=0; threadIdx<threads.length; threadIdx++) {
    var thread = threads[threadIdx];
    var messages = thread.getMessages();
    for (var msgIdx=0; msgIdx<messages.length; msgIdx++) {
       var message = messages[msgIdx];
       var attachments = message.getAttachments();
    Logger.log(attachments.length);
    }
}

So I always get the attachments.length to be one more than the actual number of attachments, when there is a signature image.

Can someone help?


Solution

  • Sorry for late reply but I was facing the same issue and found a workaround,

    Your message.getBody() method will contain whole message body. This will also contain signature image name. So what we can do we can check signature name from attachment and if my body text contains this signature name then I should emit that attachment. Here's the code snippet,

    var textMessage = message.getBody();
       if (attachments.length > 0) {
            for (var z=0; z<attachments.length; z++) {
                var attachment = attachments[z];    
               if(textMessage.indexOf(attachment.getName()) === -1)
                 {
                   folder.createFile(attachment);
                 }
            }
        }
    

    I've checked for Inline attachments as well, this code considers those as an attachment only.