GAS AI BuilderApps Script Assistant Sign in

Send an HTML email from a template

Apps Script recipes › Email, Docs, Forms and PDF

The email body is an HTML file with <?= name ?> fields; checks the daily email quota first.

File Code.gs

function sendWelcome_(to, name) {
  if (MailApp.getRemainingDailyQuota() < 1) throw new Error('The email limit for today is used up.');
  const page = HtmlService.createTemplateFromFile('mail/welcome');
  page.name = name;
  MailApp.sendEmail({
    to,
    subject: 'Welcome, ' + name,
    htmlBody: page.evaluate().getContent(),
    name: 'My app',
  });
}

File mail/welcome.html

<div style="font-family: Arial, sans-serif; font-size: 15px; color: #202124">
  <p>Hi <?= name ?>,</p>
  <p>Your account is ready.</p>
  <p style="color: #5f6368; font-size: 13px">This email was sent by My app.</p>
</div>

Good to know

Let the AI put it in your appGAS AI Builder knows every recipe here. Describe your app and it uses this code where it fits, in a Google Apps Script project in your own Drive.

Use it in a project

More in Email, Docs, Forms and PDF

Fill a Doc template and save it as PDFCopies a Google Doc with {{placeholders}}, fills them in, saves a PDF in a folder and removes the copy.Make a real Google Form with codeCreates a Google Form once, keeps its ID, sends its responses to the project's Sheet and returns its links.