Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I found an easier way to accomplish this task.</p> <p>Use the <strong>htmlBody</strong> advanced argument within the arguments of <strong>sendEmail()</strong>. Heres an example:</p> <pre><code>var threads = GmailApp.search ('is:unread'); //searches for unread messages var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array for (i = 0; i &lt; 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}); } </code></pre> <p>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 <strong>getMessagesForThreads()</strong> method within <strong>GmailApp</strong>. 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 <strong>getBody()</strong> and the subject through <strong>getSubject()</strong>. I use the <strong>sendEmail(recipients, subject, body, optAdvancedArgs)</strong> 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: <a href="https://developers.google.com/apps-script/service_gmail" rel="nofollow">https://developers.google.com/apps-script/service_gmail</a></p> <p>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! </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload