Skip to content

Instantly share code, notes, and snippets.

@bhowe
Forked from gowtham1337/temp_mail_server.md
Created June 14, 2018 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhowe/bc1ea578deb6db86cee15a67c7d98c92 to your computer and use it in GitHub Desktop.
Save bhowe/bc1ea578deb6db86cee15a67c7d98c92 to your computer and use it in GitHub Desktop.
A quick guide for creating your own temporary mail server

##Overview You might have a need for a temporary mail service like mailinator.com on your own machine. This guide provides the minimum required steps to do that. The setup we will use is as follows

Procmail (Receive mail and forward to script)--->PHP Script(Parse Mail and put in Database)--->MySQL(Store the mails)--->JSON

Note: This is intended as a quick DIY for simple projects. Hence, we will not go into more details like spam detection and memory optimization.

##Instructions ####Setup Infrastructure

  1. Get a Top level domain name (Note: Getting this to work with a sub-domain will require more work and setup)
  2. Change the MX record to point to the public IP of your machine

####Setup Database

  1. Install mysql on your machine and setup a database to hold your mails.
  2. Create a table with all the fields you want. A few example fields can be Date, From, Subject, Body etc.

####Setup PHP Script

  1. Install php on your machine and php-mime-mail-parser. We will be using this library to parse the incoming mails.
  2. Write a simple file to parse emails from the stdin stream. You can look at the sample file of the library for this.
  3. You can store the extracted fields into your MySQL database.

####Setup Mail

  1. Install procmail and postfix
  2. Edit /etc/postfix/virtual file and add the line @yourdomain.com forward. This will send any mail addressed to any user on yourdomain.com to the user forward.
  3. Execute postmap /etc/postfix/virtual
  4. In your /new/aliases file, add a forward to your PHP file. This can be done like this -> forward: "|/usr/bin/php -q /var/www/html/proc_mail.php"
  5. Run newaliases to update your aliases
  6. Edit the file at /etc/postfix/main.cf and make this change virtual_alias_maps = hash:/etc/postfix/virtual
  7. Restart postfix by running service postfix reload

####Finish

  1. You can see the incoming mails at /var/log/mail.log
  2. You can setup a simple php file to retrieve all the emails for a given id and provide the data through JSON.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment