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
- MailApp sends from the account the script runs as. Consumer accounts can send about 100 emails a day, Google Workspace about 1,500.
- Fields written with <?= ?> are escaped, so text from a form can't break the email.
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