Send As Alias

sendAsAlias(to, subject, body, opts) sends mail from a Gmail alias after checking GmailApp.getAliases(). If {from} is not on that list, it throws a clear error naming the aliases this account actually has, instead of quietly sending from your personal address. {name} is the display name on the message.

When to use

  • Team or product mail that should come from team@example.com, not your inbox
  • A script you are about to hand to a teammate, so a missing alias fails loudly
  • Any send path where falling back to the main account would be a mistake

How to use this snippet

The alias has to exist in Gmail Settings → Accounts (Send mail as) before the script can use it. The first run from the editor prompts for Gmail access.

/** * Send email from a Gmail alias, or throw if that alias is not on this * account. Checks GmailApp.getAliases() first and will not send from the * main address as a fallback. opts.from is required. opts.name is the * optional display name. opts.htmlBody is passed through when present. * * @param {string} to * @param {string} subject * @param {string} body * @param {{from: string, name?: string, htmlBody?: string}} opts */ function sendAsAlias(to, subject, body, opts) { opts = opts || {}; var from = String(opts.from == null ? '' : opts.from).trim(); if (!from) throw new Error('sendAsAlias: from alias required'); var aliases = []; try { aliases = GmailApp.getAliases() || []; } catch (err) { throw new Error('sendAsAlias: could not read Gmail aliases: ' + (err && err.message ? err.message : err)); } var resolved = ''; for (var i = 0; i < aliases.length; i++) { if (String(aliases[i] || '').trim().toLowerCase() === from.toLowerCase()) { resolved = String(aliases[i]).trim(); break; } } if (!resolved) { throw new Error( 'sendAsAlias: "' + from + '" is not a Gmail alias on this account. ' + 'Add it in Gmail Settings → Accounts, then run the script once from the editor to grant Gmail access. ' + 'This account\'s aliases: ' + (aliases.length ? aliases.join(', ') : '(none)') ); } var options = { from: resolved }; if (opts.name != null && String(opts.name).trim() !== '') options.name = String(opts.name).trim(); if (opts.htmlBody != null) options.htmlBody = opts.htmlBody; GmailApp.sendEmail(to, subject, body, options); }

Example

function sendFromTeam(to, subject, body) { sendAsAlias(to, subject, body, { from: 'team@example.com', name: 'Acme Support' }); }

Notes

  • Aliases are per account. A script running as you cannot send as a teammate's alias.
  • Matching is case-insensitive; the alias string Gmail returned is what gets sent as from.
  • This uses GmailApp.sendEmail with a plain-text body. Pass htmlBody in the options if you need HTML.

Happy Coding!

NitroGAS Chrome Extension

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

Get the Extension