Random Email Address Generator Using Python

RandomEmailGeneratorFeaturedImage

The user usually sets an email address in a fashion that the more he or she uses the email address, the more he or she remembers the email address. But there are many use cases in which random email addresses are required. In cases where an email address needs to be submitted on an untrusted website or untrusted business, random email addresses play a major role. They provide a way to bypass the website requirements and proceed with the process. Services such as TempMail also provide mailboxes for these random emails. TempMail reads the mail for the user in the randomly generated email’s mailbox and helps the user bypass requirements of the website, such as OTP (One Time Password).

So how to generate a random email address. Generating an inbox for a temporary randomly generated email address is a difficult task, but creating a random email address is cakewalk using Python. The random module of Python is highly useful here. But, before generating random email addresses, one needs to learn how an email address is created.

An email address has two parts: A Name and a Domain Name. The name is an unique identifier for an email address in a domain name. It has certain rules that it needs to follow, such as names cannot start with a numeric character, names cannot contain special characters such as ‘.’,’$’ etc. The domain name hosts the email address. It is registered by an industry and distributes the names to email addresses.

Breakdown Of Email Address
Breakdown Of Email Address

In the above example: [email protected], johnsmit is the name of the email address in the domain example.com . Anything in the address that follows the ‘@’ symbol is domain name. Domain consists of two components: Server name and Top Level Domain. The Server name basically refers to the industry that has registered the domain. The top level domain states the affiliation of the industry.

In a random email generator, there can be two methods. One can create all the three components: Name, Server Name and Top Level Domain randomly or create the Name randomly and use well known Domain Names for email address. Both the examples are covered in this article.

Random Emails With Fixed Domain Names

Many popular domain names are available, allowing users to create free emails. Industries such as Google and Yahoo! allow their users to create free emails with domain names as @gmail.com and @yahoo.com, respectively. Many other popular domains, such as @hotmail.com and @redmail.com popularly used by users.

The emails can also have different Top Level Domains which refer to the industry’s affiliation. Top Level Domains (TLDs) such as .gov imply that the email address owner works with the government, or .ac means that the email address belongs to an academic institution. TLDs such as .in, .us, .ge etc. are Country wise TLDs which represent email addresses belonging to a certain country, for e.g., India, United States of America and Germany in the above example, respectively. The most commonly used TLD, .com, stands for a commercial institution email address. Using these popular server names and TLDs, email addresses can be generated, which can also be signed up for real life usage. The following code demonstrates the same:

import random
validchars='abcdefghijklmnopqrstuvwxyz1234567890'
loginlen=random.randint(4,15)
login=''
for i in range(loginlen):
    pos=random.randint(0,len(validchars)-1)
    login=login+validchars[pos]
if login[0].isnumeric():
    pos=random.randint(0,len(validchars)-10)
    login=validchars[pos]+login
servers=['@gmail','@yahoo','@redmail','@hotmail','@bing']
servpos=random.randint(0,len(servers)-1)
email=login+servers[servpos]
tlds=['.com','.in','.gov','.ac.in','.net','.org']
tldpos=random.randint(0,len(tlds)-1)
email=email+tlds[tldpos]
print(email)

The above code snippet makes use of the random module’s randint() function to generate a random integer. The randint() function generates a random integer between its two inputs, including the two inputs. Here, the length of the Name of email address is fixed between 4 characters long to 15 characters long. It is to avoid creation of very small or very long email addresses. validchars is a string which consists of all the characters which are valid in the email address Name. The code randomly decides the length of the Name first. Then it chooses characters from validchars at random till Name’s length is not equal to the length chosen. servers and tlds are two different lists containing well-known Server Names and TLDs. The code than randomly chooses a server and a TLD. All of these are joined together, and a randomly generated email address is provided as Output.

The following images show, the execution of program twice. Both the times, a different output is produced. It is as follows:

Output Of Random Email Generator When Ran First Time
Output Of Random Email Generator When Ran First Time
Output Of Random Email Generator Ran Second Time Compared To First Output
Output Of Random Email Generator Ran Second Time Compared To First Output

When the Random Email Generator is ran twice, it produces two different outputs: [email protected] and [email protected] . Both the emails differ from each other not only in The Name, but also in the Domain Name.

Random Emails With Randomly Generated Domain Names

Sometimes, using randomly generated emails with fixed domain names are not preferred and randomly generated domain names are preferred. This is an extra step to maintain complete anonymity on untrusted websites and security. Such domain names may or may not exist. This method is also used by the famous TempMail service as well. The following code demonstrates generation of random emails with random domain names:

import random
validchars='abcdefghijklmnopqrstuvwxyz1234567890'
validoms='abcdefghijklmnopqrstuvwxyz'
login=''
server=''
extension=''
loginlen=random.randint(4,15)
serverlen=random.randint(3,9)
tldlen=random.randint(2,3)
for i in range(loginlen):
    pos=random.randint(0,len(validchars)-1)
    login=login+validchars[pos]
if login[0].isnumeric():
    pos=random.randint(0,len(validchars)-10)
    login=validchars[pos]+login
for i in range(serverlen):
    pos=random.randint(0,len(validoms)-1)
    server=server+validoms[pos]
for i in range(tldlen):
    pos=random.randint(0,len(validoms)-1)
    tld=tld+validoms[pos]
email=login+'@'+server+'.'+tld
print(email)

In the above code snippet, the Name of email address is generated the same way as done earlier. But the same procedure is followed for generation of Server Name and TLD. The Server Name length ranges from 3 characters to 9 characters, and the TLD length is either 2 or 3 characters long. The validoms string contains characters that can be used in Domain Names. It does not consist of numerical characters as opposed to validchars used in Email Address Name. The email is formed as [email protected] . The output is shown as below:

Output Of Random Email Generator With Randomly Generated Domain Name
Output Of Random Email Generator With Randomly Generated Domain Name
Output Of Random Email Generator With A New Randomly Generated Domain Name
Output Of Random Email Generator With A New Randomly Generated Domain Name

On running the random email generator twice, it produced two emails: [email protected] and [email protected] . Both the emails differ completely in Length, Name and Domain Names as well.

Conclusion

Generating Random Email Addresses is important today, where privacy and security are frequently breached. These randomly generated email addresses help users get through untrusted websites and businesses, if they have to interact any time. Services such as TempMail , provide with Random Email Addresses and their inboxes as well to the user for a temporary amount of time. These emails are disposed off later on.

Using the random Python module, generating random email addresses is pretty simple. Their inboxes cannot be generated so simply, although. These random email addresses may or may not have fixed domain names. Having randomly generated domain names provide more security over fixed domain names. Hence, completely randomly generated email addresses should be preferred over those with fixed domain names.

References

Python Random Library