Make Your Own Email Address Generator for Testing

When building an app you often need to run through parts of the system manually to check how functionality works or checking a bug report.

A lot of the time this will require creating a new user account on the system, following the steps that a normal user would need to follow.

Most online accounts require a unique email address. This constraint leaves demo systems full of email addresses like test1@example.com. Let’s talk about a great way to generate test email addresses.

The problems with example.com

Using fake email addresses hides issues when your development environment sends the email out.

  • You don’t get to see if the email actually shows in an email client
  • If you use a fake domain and that domain starts being used by someone you could end up sending test emails to a real person
  • When you need to follow a link in an email, for example password resets, you have to look through logs

Using a real email address

Using a real email address bypasses these issues. It has the downside of being a finite resource. We don’t have access to thousands of easy to manage email addresses.

Enter Gmail

Gmail has this wonderful feature to create unique email addresses that link to your main email.

If your main account was pixel@gmail.com then you can create append +tag before the @.

These are all valid email addresses that will all go into the same inbox:

  • pixel+test@gmail.com
  • pixel+123@gmail.com
  • pixel+TextExpander@gmail.com

We can use one real account to generate lots of unique emails for our demo systems.

Enter TextExpander

Generating unique email addresses by hand doesn’t scale. This is where TextExpander comes in.

You can use TextExpander to add a custom + to your email address.

pixel+{{RANDOM}}@gmail.com

There are a few ways we can generate something random.

Random Numbers

TextExpander lets you instantly insert Snippets of text as you type—using a quick search or abbreviation. So, we’ll make a “snippet” to help us make new email addresses very quickly.

By writing a snippet that makes use of the bash variable RANDOM we can generate an email address that will be random enough for our purposes.

  1. Open the TextExpander app and create a new snippet
  2. Set the Content type to Shell Script
  3. Paste the following as the body of your snippet
#!/bin/bash

echo -n "pixel+$RANDOM@gmail.com"

This will generate:

pixel+1322@gmail.com

For a longer post on bash commands you will enjoy How to Instantly Insert Your Most Used Bash Commands.

Note: Bash command Snippets only work on macOS. Below we’ll share a way to do this with JavaScript that works on Android, Chrome, iOS, and Windows.

Dates

You can also use date based emails, this will be unique and also let you see at a glance when you created it.

pixel+(Year: 2001)(Month: 01)(Day: 01)(Hour: 01 (00-23))(Minute: 01)(Second: 01)@gmail.com

This will generate:

pixel+20190308174335@gmail.com

JavaScript Random Email Generator

TextExpander Snippets support JavaScript for Automation (JXA). While Bash scripting only works on macOS, JavaScript automations work on every platform TextExpander supports. Here’s how to create the random email generator Snippet with JavaScript:

  1. Open the TextExpander app and create a new Snippet
  2. Set the Content type to JavaScript
  3. Paste the following as the body of your Snippet
function generateRandomEmail() {
    var randomNum = Math.floor(Math.random() * 10000); // Generate a random number
    var email = "pixel+" + randomNum + "@gmail.com"; // Concatenate to create email
    return email; // Return the email string
}


generateRandomEmail(); // Call the function and output the result

Conclusion

Using Gmail and TextExpander, never run into the email problem again when testing the signup flow of your system.

If you’d like to play with the snippets in this post, you can check out our sample bash scripts Public Group.

How are you solving your development issues with TextExpander? Let us know @TextExpander and on Facebook.

Not able to play the video? Click here to watch the video