1
votes

I am editing online available snooze script for gmail, where i would like to bring back an email unread, in inbox and on top of the list. I am able to make it unread, and bring back to inbox based on the available script, but to bring on top of the inbox, i am forwarding the email to myself. By this, I am able to get the email thread on top of the list, but when an email which is snoozed has an attachment, it forwards the email with attachment. I have tons of emails, and have limited available space. Hence i am ending up using extra storage for duplicate attachment which is unneeded.

I would like to forward the email, without attachment. Below is my current script:

          var firstThread = page[i];
          var giattachment = null;
          var sub = firstThread.getFirstMessageSubject();
          var newsub = "Reminder: " + firstThread.getFirstMessageSubject() + " : ";

          var id = newsub + firstThread.getPermalink();

          var messages = firstThread.getMessages();
          var totalThreadCount= firstThread.getMessageCount();

          for (var j = 0; j < messages.length; j++) {
            var forwardlength=messages.length-1;
            if(j == forwardlength) {
              if (MARK_UNREAD) {
                messages[j].markUnread();
              } 
              messages[j].forward(userEmail,{subject: newsub, from:userEmail, name: EmailFrom, body: newbody, attachments:giattachment});
            }
          }

Inspite of this, the email gets forwarded with an attachment. Please advise..

Thanks in advance

2
To clarify, attachments: null (since giattachment = null) doesn't help and neither does attachments= [] (which I've tried). Obviously, while not documented as such, attachments is only used for extra attachments. The answer should be how to manipulate parameters like the string htmlBody (which the poster mistakenly called body) to remove existing attachments. - LWC

2 Answers

0
votes

To selectively forward messages, use filters to create rules that forward in response to message attributes or content.

You can filter messages by properties such as the sender, subject date, size, and message contents. Any query using Gmail's advanced search syntax can also be used in a filter. Try to set criteria.hasAttachment=true to false.

For more information, follow this link: https://developers.google.com/gmail/api/v1/reference/users/settings/filters

0
votes

You can't forward using Class GmailMessage because it forces you to forward the original attachments. While unforunately undocumented, its advanced parameter attachments can only add new attachments.

However, you can forward by creating a new message using Class MailApp instead, in your case it would be:

      var from, firstThread = page[i];
      var sub = firstThread.getFirstMessageSubject();
      var newsub = "Reminder: " + firstThread.getFirstMessageSubject() + " : ";

      var id = newsub + firstThread.getPermalink();

      var messages = firstThread.getMessages();
      var totalThreadCount= firstThread.getMessageCount();

      for (var j = 0; j < messages.length; j++) {
        var forwardlength=messages.length-1;
        if(j == forwardlength) {
          if (MARK_UNREAD) {
            messages[j].markUnread();
          }
          from = message.getHeader('From');
          if (from.indexOf(' <') > -1)
              from = from.substring(0, from.indexOf(' <'));
          else
              from = '';
          MailApp.sendEmail(userEmail, newsub, '', {htmlBody: messages[j].getBody(), name: from, replyTo: message[j].getFrom()});
        }
      }

Changes:

  1. Removed your var giattachment = null;
  2. Replaced your messages[j].forward with MailApp.sendEmail
  3. Used messages[j].getBody() to simulate the original body
  4. Added the original sender's name as the "sender's" name
  5. Added the original sender's address as reply-to

The last 2 points mean the forwarded message will arrive from "someone else's name" <your@address> which for most recipients will appear like a true forward.