Skip to content

Instantly share code, notes, and snippets.

@gowtham1337
Last active March 1, 2024 12:53
Show Gist options
  • Save gowtham1337/3e0fb194ffd3158bbe40 to your computer and use it in GitHub Desktop.
Save gowtham1337/3e0fb194ffd3158bbe40 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.
@1x6
Copy link

1x6 commented Aug 2, 2021

bruh

@gatosyperros
Copy link

Hi
I just wanted to let you know that You can easily do that with Roundcube mail in combination with iRedmail you get a nice looking front page local or online, and a control panel to manage the accounts.

The key is adding a small script on your server that updates some values in the SQL database.
This is how I do it, I run this scrip (with all the correct names and users), on a cron task every other week or on demand:
I don't know why GIT hub didn't want me to insert this as code, so here it is in raw as a comment:

########Updates the spam mailbox#########

########Creating tempmail################
openssl rand -hex 3 > temp-mail
DOMAIN="DOMAIN.com"
Database="somename"
##MailBox1
SPAM_USERNAME_1=$((echo "USER-Spam"))
USER_ID_1=420
SPAM_ID_1=421
MAIL_BOX_1="USER"
##############
##MailBox2
SPAM_USERNAME_2=$((echo "USER2-Spam"))
USER_ID_2=69
SPAM_ID_2=691
MAIL_BOX_2="USER2"
############## Not commented
mysql -u user -D $Database -e "UPDATE mailbox SET username='$MAIL_BOX_1-$(cat temp-mail)@$DOMAIN' WHERE name ='$SPAM_USERNAME_1'";
mysql -u user -D $Database -e "UPDATE forwardings SET address='$MAIL_BOX_1-$(cat temp-mail)@$DOMAIN' WHERE id ='$USER_ID_1'";
mysql -u user -D $Database -e "UPDATE forwardings SET forwarding='$MAIL_BOX_1-$(cat temp-mail)@$DOMAIN' WHERE id ='$SPAM_ID_1'";
mysql -u user -D $Database -e "UPDATE forwardings SET address='$MAIL_BOX_1-$(cat temp-mail)@$DOMAIN' WHERE id ='$SPAM_ID_1'";

##Updates Mailbox
mysql -u root -D $Database -e "UPDATE mailbox SET username='$MAIL_BOX_2-$(cat temp-mail)@$DOMAIN' WHERE name ='$SPAM_USERNAME_1'";
#Edits Forwarding to Main MailBox
mysql -u root -D $Database -e "UPDATE forwardings SET address='$MAIL_BOX_2-$(cat temp-mail)@$DOMAIN' WHERE id ='$USER_ID_2'";
#Edits forwarding to new Temp mail
mysql -u root -D $Database -e "UPDATE forwardings SET forwarding='$MAIL_BOX_2-$(cat temp-mail)@$DOMAIN' WHERE id ='$SPAM_ID_2'";
#Edits Temp Mail to Temp mail forwarding
mysql -u root -D $Database -e "UPDATE forwardings SET address='$MAIL_BOX_2-$(cat temp-mail)@$DOMAIN' WHERE id ='$SPAM_ID_2'"
rm temp-mail

This ensures that all the temp mail get forwarded to your main email with the correct temp mail address as the origin, but also since you can run this every hour, day, week you can keep track of what services used what mail, and thus if you ever need to reused the old temp mail you can simply edit the sql entry to match your "old" temp mail:

mysql -u root -D $Database -e "UPDATE forwardings SET address='$MAIL_BOX_2-[OLD SPAM ID]@$DOMAIN' WHERE id ='$SPAM_ID_2'"

I hope this helps somebody get their own temp mail server running, I've personally used that solution for about three years, and I've stopped using temp mailservices since I could never reuse those old temp mail addresses. meanwhile if I'm hosting I can simply re activete them while avoiding all the old spam :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment