Skip to content

Instantly share code, notes, and snippets.

@gowtham1337
Last active March 1, 2024 12:53
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • 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.
@nurulafsar03
Copy link

Hello sir,
I want to create my own temporary mail server / Disposable Email Server, can you please support me or you will do for me? I will try to fill up your requirements.
Please inform me to my email: afsar.nurul@gmail.com

Thanks and regards
Mohammed Nurul Afsar

@justpassinthrough
Copy link

justpassinthrough commented Dec 30, 2020

hi
i used a temporary email about a year ago and i used it to make some social media accounts at the time i didn't think i would use them for long but i kept using one of them about a week a ago i noticed that i had the disposable email on my account and i don't have access to it so i changed it but forgot to confirm my account and now for some reason I'm locked out of my account and cant get it back i tried everything
there is still more to the story but anyways i was wondering if i can make a temporary mail with a custom domain? the domain is [dmail1.net] i even tried some temporary mail apps to create a custom email but none of them have this domain. plus i don't even remember where i got the email from i was using a lot of different temp mail apps and websites at the time.
if you can help me with any info that would be great.
i would leave my email here but i rarely check them
instagram:rb.09
whatsapp: +971 50 8727 441

@Avi895
Copy link

Avi895 commented Feb 7, 2021

He'llo sir imm on a project and looking for someone to guide me on selfhosting my own disposable email server as first step of my project.i wish to get some guidance from you

@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