Search code examples
google-apps-scriptgmailgmail-api

Formatting Specific Portions of a Gmail Email Created with Apps Script


I'm trying to find a way to create emails using Google Apps Script and format specific words or phrases (not the whole body) in the email body. I want to automate creating some emails that have specific formatting needs.

I want to be able to make a header for a section that is bolded, underlined, and size 13.5pt and a sub-header that is bolded, size 10pt, with font color #990000. I also want to be able to do a bulleted list.

Can I do this with html or is there a way to create the email body with the specific formatting I want in a google doc and then copy and paste this into a email with apps script?

I've used apps script for a while but have not used the gmail classes and am unfamiliar with the html language. From what I've seen online you can format the whole body with html but I have yet to find information on formatting specific portions of the body.


Solution

  • Send Email with Specific Format

    Formatting specific parts of an email is possible by using sendEmail(recipient, subject, body, options). Here's a sample script that does I want to be able to make a header for a section that is bolded, underlined, and size 13.5pt and a sub-header that is bolded, size 10pt, with font color #990000. I also want to be able to do a bulleted list. of the question. Kindly check if it works on your end.


    Script:

      function sendEmail() {
      var htmlBody = "";
      htmlBody += "<h1 style='font-size: 13.5pt; font-weight: bold; text-decoration: underline'>Header</h1>";
    
      htmlBody += "<h2 style='font-size: 10pt; font-weight: bold; color: #990000'>Sub-header</h2>";
    
      htmlBody += "<ol>";
      htmlBody += "<li>Item 1</li>";
      htmlBody += "<li>Item 2</li>";
      htmlBody += "<li>Item 3</li>";
      htmlBody += "</ol>";
    
      var options = {
        htmlBody: htmlBody
      };
      GmailApp.sendEmail("[email protected]", "Subject", "", options);
    }
    

    Output:

    sampleoutput