Send an HTML Email with GmailApp

GmailApp.sendEmail can send HTML — you just need htmlBody (and a plain-text fallback for picky clients). This helper wraps the options bag so cc/bcc/from-name stay optional and clean.

What you'll need

  • A script authorized for Gmail
  • An HTML string (keep it simple — many clients strip fancy CSS)
  • Optional name, cc, bcc, replyTo

How to use this snippet

function sendHtmlEmail(to, subject, htmlBody, options) { options = options || {}; var msg = { to: to, subject: subject, htmlBody: htmlBody, name: options.name || undefined, cc: options.cc || undefined, bcc: options.bcc || undefined, replyTo: options.replyTo || undefined }; Object.keys(msg).forEach(function (k) { if (msg[k] === undefined) delete msg[k]; }); // 3rd arg is the plain-text body; advanced options carry htmlBody / cc / etc. GmailApp.sendEmail(msg.to, msg.subject, stripHtml_(htmlBody), msg); } function stripHtml_(html) { return String(html) .replace(/<[^>]+>/g, ' ') .replace(/\s+/g, ' ') .trim(); }

Tips:

  • Don't put secrets in the HTML body — treat email like a log that someone will forward.
  • Quotas are real: batch thoughtfully and prefer one digest over a hundred one-off mails.
  • For MailApp-only projects the options shape is similar; stick to GmailApp when you need the richer client.

Example

function notifySignup(user) { sendHtmlEmail( user.email, 'Welcome aboard', '<p>Hi <b>' + user.name + '</b>,</p><p>Your workspace is ready.</p>', { name: 'Bootstrapping Tools', bcc: 'ops@example.com' } ); }

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free themes and snippets — plus optional Co-Pilot when you want a boost (1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension