Customizing Text Styles in Gmail Messages via Google Apps Script

Hey everyone! I’m working on a project where I need to send out emails with some fancy formatting using Google Apps Script. I’m not too familiar with HTML or the Gmail stuff in Apps Script, so I’m hoping someone can point me in the right direction.

Basically, I want to jazz up parts of my email. Like, I want to make some headers big and bold, and maybe have some subheaders in a different color. Oh, and I’d love to throw in a bullet list too!

Is there a way to do this with HTML in the script? Or should I just make a Google Doc with all the formatting and somehow copy that into the email?

I’ve looked around online, but most stuff I’ve found only talks about formatting the whole email body, not specific bits. Any tips would be super helpful! Thanks!

I’ve been in your shoes, ameliat. When I first started customizing emails with Apps Script, it was a bit daunting. But once you get the hang of it, it’s quite powerful.

For your specific needs, HTML is indeed the way to go. You can create a multiline string in your script with all the HTML formatting. Here’s a basic structure I’ve used:

var htmlBody = 
'<h1>Main Header</h1>' +
'<p>Regular text here.</p>' +
'<h2 style=\"color: #4A86E8;\">Colored Subheader</h2>' +
'<ul><li>Bullet point 1</li><li>Bullet point 2</li></ul>';

GmailApp.sendEmail(recipient, subject, '', {htmlBody: htmlBody});

This approach gives you fine-grained control over each element. You can adjust styles, colors, and structure as needed. It takes some trial and error, but the results are worth it. Good luck with your project!

hey ameliat! u can defintely use HTML in ur script to style parts of ur email. heres a quick tip: use

for big headers, for colored text, and
  • for bullet points. just wrap these tags around the text u wanna style in ur email body. hope this helps u get started! lemme kno if u need more help :slight_smile:

Having worked extensively with Gmail and Apps Script, I can confirm that HTML is indeed the most effective way to customize your email formatting. Here’s a practical approach:

Create a template HTML string in your script with placeholders for dynamic content. For instance:

var template = ‘

{header}

{body}

    {bulletPoints}
’;

Then, replace these placeholders with your actual content using string manipulation or a simple replace function. This method allows for easy maintenance and updates to your email structure.

For styling, inline CSS is generally more reliable in emails. Remember to test your emails across different clients, as some may strip out certain HTML elements or CSS properties. Keep it simple for the best cross-client compatibility.