Remove formatting tags from string body of email

前端 未结 5 584
臣服心动
臣服心动 2020-12-03 02:38

How do you remove all formatting tags when calling:

GmailApp.getInboxThreads()[0].getMessages()[0].getBody()

such that the only remainder o

5条回答
  •  既然无缘
    2020-12-03 02:56

    I found an easier way to accomplish this task.

    Use the htmlBody advanced argument within the arguments of sendEmail(). Heres an example:

    var threads = GmailApp.search ('is:unread'); //searches for unread messages   
    var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array
    
    for (i = 0; i < messages.length; ++i)
    {
    j = messages[i].length; //to process most recent conversation in thread (contains messages from previous conversations as well, reduces redundancy
    messageBody = messages[i][j-1].getBody(); //gets body of message in HTML
    messageSubject = messages [i][j-1].getSubject();
    GmailApp.sendEmail("dummyuser@dummysite.com", messageSubject, "", {htmlBody: messageBody});
    }
    

    First I find all the threads containing unread messages. Then I get the messages contained within the threads into a two dimensional array using the getMessagesForThreads() method within GmailApp. Then I created a for loop that runs for all of the threads I found. I set j equal to the threads message count so I can send only the most recent message on the thread (j-1). I get the HTML body of the message with getBody() and the subject through getSubject(). I use the sendEmail(recipients, subject, body, optAdvancedArgs) to send the email and process the HTML body. The result is an email sent properly formatted with all features of HTML included. The documentation for these methods can be found here: https://developers.google.com/apps-script/service_gmail

    I hope this helps, again the manual parsing method does work, but I still found bits and pieces of HTML left hanging around so I thought I would give this a try, It worked for me, if I find any issues in the longrun I will update this post. So far so good!

提交回复
热议问题