How to Create a Disposable Email Address with Gmail

H

Temporary, disposable email addresses help keep spam out of your main inbox and are really useful when you don't want to give your real email address on the Internet

Published in: Gmail - Google Apps Script

A temporary email address is very useful when you don’t want to share your real email address with a website. There are a plethora of services - 10 Minute Email, Mailinator and MailDrop to name a few - that will mask your main email address behind a temporary alias and thus save your inbox from potential spam.

The only problem with using disposable email addresses is that it requires some work. You first need to generate a temporary address, paste it into the web form and then manually check the disposable inbox for any new email. And if you require another temporary address the next day, the whole exercise has to be repeated.

Disposable Email Addresses with Gmail

Here’s a new, simplified workflow that will let you use Gmail itself as a disposable email service provider.

You’ll have just one temporary email address to remember and every time your share that email on the Internet, you attach an expiry date. Any email messages sent to your disposable Gmail account after the expiry date are discarded automatically else they are forwarded to your main account.

Gmail Disposable Email

Let’s say your temporary email address is amit@gmail.com. A website form requires your email address so you can put amit+mmdd@gmail.com in the sign-up field where mmdd is the month and date till when that disposable email will stay valid.

For instance, if you specify the email address as amit+0623@gmail.com - that alias will be valid until June 23 and any emails sent to that alias after that date are ignored else they are forwarded to your main Gmail address. You can only specify the year in your temporary email amit+12312020@gmail.com in mmddyyyy format.

Make your own Temporary Email System with Gmail

Follow these steps to set up your own disposable email system in 2 minutes. Internally, it uses the Gmail plus trick and some Google Apps Script magic.

  1. Create a new Gmail address (link).

  2. While you are logged into your new Gmail account, click here to make a copy of the Google Script in your account.

  3. Inside the Google Script, go to line #13 and replace my email with the email address where you would like the temporary emails to be forwarded.

  4. Next, go to the Run menu, choose Run Function and select Initialize. Authorize the Google script and your disposable email system is up and running.

The script will check your Gmail inbox every five minutes and process messages based on the expiry date in the message To field. You just have to set it once and forget about it.

Your disposable address will last forever and yet it will protect your main inbox from spam. That’s it!

Under the Hood - How it works?

The Google Script is monitoring the temporary inbox using the Gmail API. If it discovers a new email that has an expiry date in the future, it simply forwards it to your main email account else it archives the message.

Here’s the source code:

/** Check if an email message should be forward from the
 * temporary inbox to the main Gmail inbox based on the
 * date in the TO field of the incoming message
 */
const isAllowed = (email = '') => {
  const [, mm, dd, yyyy] = email.match(/\+(\d{2})(\d{2})(\d{4})?@/) || [];
  if (mm) {
    const now = new Date();
    const date = new Date([yyyy || now.getFullYear(), mm, dd].join('/'));
    return date > now;
  }
  return false;
};

/**
 * Fetch the 10 most recent threads from Gmail inbox,
 * parse the To field of each message and either forward it
 * or archive the emssage
 */
const checkTemporaryInbox = () => {
  GmailApp.getInboxThreads(0, 10).forEach((thread) => {
    thread.getMessages().forEach((message) => {
      if (isAllowed(message.getTo())) {
        message.forward(RECIPIENT);
      }
    });
    thread.moveToArchive();
  });
};
Published in: Gmail - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Tech Columnist
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in GSuite and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. Read more on Lifehacker and YourStory

Get in touch