Creating a temporary email service

A long time ago I created a temporary email service (Fake Mail Generator) using an open source script. Shortly after, my shared hosting provider shut me down because it was using too many resources.

This weekend I finally got it up and running again, and I’d like to share the basics of how I did it. You’ll still have to figure some parts out on your own, but at least you’ll have a basic idea of what to do.

First, I needed somewhere to host my email service. I know from experience that some hosts aren’t too pleased with this type of service, so I didn’t want to put it on my main dedicated server. Instead, I found a great VPS (virtual private server) at AlienVPS for only $6.75/month. I get shell access, plenty of disk space and bandwidth, and 512MB of RAM.

There is always the risk that they’ll decide to cancel my service without warning, so I store all my code (including config files and database schema) in a git repository. I think it’d take me less than an hour to get completely up and running on a new host if I had to.

Second, I needed a domain name to use for the email service. These need to be changed every now and then, so I couldn’t use the domain. I’ve had problems with domain registrars in the past getting upset over my usage of the domain, too, so I went to a registrar I’ve never used before. I chose because they have a history of standing up for the rights of their users, and I registered

Last, I needed a way to process incoming email. This was the part that took a little effort to figure out because I lost the original instructions to the open source script I was using.

The first step was to install postfix. I’m using CentOS so this was as easy as yum install postfix.

Next, some basic configuration. In /etc/aliases I had to add a mailbox that sends the mail to a PHP script. For me this looks like:

fmg: "| php -q /www/fakemailgenerator/mail-handler.php"

This tells postfix to send all mail addressed to fmg to a PHP script called mail-handler.php. Once I made this change to aliases, I had to run newaliases (no parameters required).

Next I needed to setup a catch-all for my domain. This is done in /etc/postfix/virtual: fmg

This tells postfix to accept mail for, and send it to the fmg account I configured in /etc/aliases.

If your /etc/postfix/ doesn’t already have it set, it needs the following lines:

alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
smtpd_banner = ESMTP $mail_name
myhostname =
virtual_alias_domains =
virtual_alias_maps = hash:/etc/postfix/virtual

Last, run the following to enable your new configuration (assuming postfix is already running):

postmap /etc/postfix/virtual
postfix reload

Yay! We now have mail addressed to any address at going to a PHP script. Lucky for me, I already have a basic handler. Unluckily for you, you don’t.

To point you in the right direction, this is how you get the content of the email into a variable called $email:

$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)&&!(strlen($email)>=5000)) {
 $email .= fread($fd, 1024);

Once you have the email in a variable, you can parse out the to, from, and the body. You can save it to a database or save to a file or do whatever you want. You can check if the to address has been activated, and if not, you can cause a “user not found” message to be sent by doing something like die(‘550 User not found.’). It is entirely up to you.

Anyways, it was fun to get going again, and I have a lot of ideas on how to improve the code I already have.

One comment

  1. Thanks for this article! It is exactly what I was looking for to get my own disposable email service started.

Leave a Reply

Your email address will not be published. Required fields are marked *