Test Recipient Or

testRecipientOr(realEmail) is a small switch for outgoing mail. When the script property TEST_MODE is on, it returns your address instead of the real recipient, plus a subject prefix that names who would have received the message. When TEST_MODE is off or unset, it returns the real address and an empty tag.

When to use

  • Testing a nightly email without sending it to the team
  • Reviewing a mail-merge on a copy, with the intended recipient visible in the subject
  • Any send path you want to be able to flip with a property, not a code edit

How to use this snippet

/** * Route an outgoing email. When script property TEST_MODE is on * (on, true, 1, or yes, case-insensitive), `to` is TEST_EMAIL if set, * otherwise the effective user's email, and `subjectTag` names the * real recipient. When TEST_MODE is off, `to` is the real address * and `subjectTag` is empty. Throws rather than sending to the real * recipient if TEST_MODE is on and no fallback email is available. * * @param {string} realEmail * @return {{to: string, subjectTag: string, testMode: boolean}} */ function testRecipientOr(realEmail) { var props = PropertiesService.getScriptProperties(); var flag = String(props.getProperty('TEST_MODE') || '').trim().toLowerCase(); var on = flag === 'on' || flag === 'true' || flag === '1' || flag === 'yes'; var real = String(realEmail == null ? '' : realEmail).trim(); if (!on) { return { to: real, subjectTag: '', testMode: false }; } var you = String(props.getProperty('TEST_EMAIL') || '').trim(); if (!you) { try { you = String(Session.getEffectiveUser().getEmail() || '').trim(); } catch (e) { you = ''; } } if (!you) { throw new Error('testRecipientOr: TEST_MODE is on, but TEST_EMAIL is empty and no effective user email is available'); } return { to: you, subjectTag: real ? '[TEST for ' + real + '] ' : '[TEST] ', testMode: true }; }

Example

function sendWeeklyReport(row) { var rec = testRecipientOr(row.Email); GmailApp.sendEmail(rec.to, rec.subjectTag + 'Weekly report', row.Body); }

Notes

  • Set TEST_MODE to on in Project settings > Script properties while you test, and remove or set it to off when you are done. Leaving it on silently reroutes production mail to you.
  • Time-driven triggers often have an empty active-user email. Set TEST_EMAIL to the inbox you want in test mode, or the helper falls back to Session.getEffectiveUser().getEmail(). If both are empty, it throws instead of sending to the real recipient.
  • The subject tag is [TEST for ada@x.com] (trailing space included) so you can prepend it to the real subject. The body is unchanged.
  • This only changes the recipient and subject prefix you pass to GmailApp or MailApp. It does not stop a script that already called sendEmail with a hard-coded address.

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