vennedey.net

Temporary trashmail addresses with postfix

On Mon, 09 May 2016 21:32:36 +0200 by Falco Nordmann

Warning: The information given in this article is outdated. See the project's website for up-to-date information.

If you are in need of temporary mail addresses (trashmail addresses) in postfix to avoid spam in your inbox you might be interested in the solution I found for this problem.

The idea is to generate an e-mail address from a date and a secret. Everyone who knows the secret (you and your mail server) will be able to generate the e-mail address for a given date. This e-mail address can then be used to sign up for a service or generated dynamically and published on your website and will only be valid for the current day.

Here is an example PHP code

$secret = "<Your secret>";
echo substr(md5(date("Ymd").$secret),0,8).'@example.com';

This will print a different e-mail address like 35de1bff@example.com for every day.

One problem with this technique is that if someone starts writing an e-mail at 23:59, he likely won't be finished before the address he uses expires. To solve this, two temporary addresses will always be valid: The one generated from the current date, and the one generated from yesterday's date.

To make this work with postfix, we need a way to map the temporary addresses to a permanent address. Looking at the Postfix Lookup Table Overview I decided to write a small script utilizing socat (since the traditional netcat package has its flaws and the OpenBSD version of netcat did not perform very well) to implement a simple TCP/IP lookup table for postfix. This is probably not a solution for high frequented mail servers, but sufficient for personal use.

It's just about 40 lines of code to do the job, and can be found on GitHub. On Debian 8 you will need the socat package.

root@mailhost:~# apt-get install socat git
root@mailhost:~# git clone https://github.com/68b32/postfix-trashmail.git

You have to open the actual script postfix-trashmail to configure some parameters

postfix-trashmail
SECRET="<Your secret>"
LENGTH=8
DOMAIN="example.com"
MAILDROP="permanent-address@example.com"

INTERFACE="127.0.0.1"
PORT="10000"

SECRET sets the secret as described above and will be used to generate the temporary e-mail addresses consisting out of LENGTH hexadecimal characters followed by @ and DOMAIN. So the above configuration will generate e-mail addresses like 35de1bff@example.com.

MAILDROP sets the address that is mapped to the temporary addresses. This is where mail sent to temporary addresses should be delivered to.

INTERFACE and PORT set the IP address and the port for the TCP lookup table. If this script is run on the same machine as the postfix daemon, a local interface should be used.

To test the configuration, run the script without arguments first. This will print the currently valid temporary addresses.

root@mailhost:~/postfix-trashmail# ./postfix-trashmail
35de1bff@example.com
97615254@example.com

Now start the script in listen mode with -l.

root@mailhost:~/postfix-trashmail# ./postfix-trashmail -l

and from another terminal use the postmap command to query for the real address.

root@mailhost:~# postmap -q 35de1bff@example.com tcp:127.0.0.1:10000
permanent-address@example.com

If you query for a currently valid temporary address, the permanent address as specified in MAILDROP should be returned. Querying for any other address should return an empty result.

To install the script and start it automatically on boot before postfix, copy the script to some commonly used path and install the systemd configuration. On Debian 8 you can do this with

root@mailhost:~# cp postfix-trashmail/postfix-trashmail /usr/local/bin
root@mailhost:~# chown postfix:postfix /usr/local/bin/postfix-trashmail
root@mailhost:~# chmod 700 /usr/local/bin/postfix-trashmail
root@mailhost:~# cp postfix-trashmail/postfix-trashmail.service /etc/systemd/system
root@mailhost:~# systemctl daemon-reload
root@mailhost:~# systemctl start postfix-trashmail
root@mailhost:~# systemctl enable postfix-trashmail
Created symlink from /etc/systemd/system/postfix.service.wants/postfix-trashmail.service to /etc/systemd/system/postfix-trashmail.service.

Then edit your /etc/postfix/main.cf to add the lookup table to the virtual_alias_maps.

/etc/postfix/main.cf
virtual_alias_maps = ... tcp:127.0.0.1:10000

Reload postfix and test if you receive mails to the addresses printed by the postfix-trashmail command.

root@mailhost:~# postfix reload && postfix-trashmail
35de1bff@example.com
97615254@example.com

Comments

Write a comment
* optional