Avoiding SPAM with qmail
SPAM. Everybody hates it, nobody can (seriously) avoid it. To keep the inbox reasonably tidy, though, there's a rather convenient solution: temporary email addresses! temp-mail.org, tempmail.com, tempmailaddress.com, bytom.de... the list is long. Unfortunately, what has always bothered me about this solution is that I first have to go to the provider's site, generate the address, wait for the mail to arrive (which can sometimes take a while), and last but not least the provider has access to all emails, which is a bit creepy for security reasons. Temporarily signing up for Facebook, Twitter or similar is certainly not a good recommendation this way. And anyone who wants to register on various forums just to ask a quick question or view an answer can get pretty annoyed having to constantly generate new temp mail addresses or - even worse - waiting for the confirmation mail that never arrives because the forum operator has blocked the temp mail provider. So what to do? The solution: a combination of a .qmail file, a BASH script and a cron job. The result should be that every day at 24:00 we create a new email address in the format dd.mm.YYYY@domain.com that forwards incoming mail to our primary email address, and have the old email address deleted.
The qmail file
Let's start with the first step: qmail. Qmail is a mail server for Unix systems. It was developed in 2007 because sendmail had a relatively large number of security-critical bugs and no other serious alternative was available. The advantage of qmail in our case is that we can conveniently set up email forwarding via qmail files. If you're not hosting at uberspace.de like me, you first need to install and set up qmail as a mail server, as described on the qmail website. Unfortunately I can't write much about that, since I didn't have to do this step. We can now create .qmail files in our home directory (usually found under the path "~"), which are read automatically by the mail server. To test, you can create a .qmail file (let's simply call it .qmail-hello) in your home directory and feed it your (correct) email address. Note that for your mail to be forwarded you have to put an "&" in front of your email address, so for example &address@example.com
If you now send an email to hello@yourdomain.com, it will, provided you did everything right, be forwarded automatically to address@example.com. Of course we now want to automate this so we don't have to create the file again every day. For that I wrote a small bash script that looks like this:
#!/bin/sh
DATE=`date +%d.%m.%Y`
YESTERDAY=`TZ=GMT+24 date +%d.%m.%Y`
rm ~/.qmail-$YESTERDAY
cat <<EOF > ~/.qmail-$DATE
&meineechtemailadresse@devdave.de
EOF
What does it do?
#!/bin/sh
That should be clear - the shebang tells the console that we are now starting a bash script.
DATE=`date +%d.%m.%Y`
Stores the current date in the variable "DATE" in the format day.month.year. We need it to name the file correctly later.
YESTERDAY=`TZ=GMT+24 date +%d.%m.%Y`
The variable "YESTERDAY" contains yesterday's date, also in the format day.month.year
rm ~/.qmail-$YESTERDAY
The rm command deletes a file, in our case yesterday's .qmail file, from our home directory. Your cron job might not accept relative paths or might not resolve the tilde correctly - in that case just specify the absolute path, e.g. /home/username/.
cat <<EOF > ~/.qmail-$DATE
&mail@example.com
EOF
CAT writes the content into a file, namely today's .qmail file. Here you of course have to replace mail@example.com with your primary email address. As a result, every time the script runs we create a new .qmail file in the format day.month.year and delete yesterday's old .qmail file. Run the script once (give it execute permission with chmod +x if necessary) and a .qmail file should be created. You can ignore the error message; it just tells us that no file from yesterday could be deleted.
Cron job
As a last step we automate the whole thing via our crontab.
crontab -e
enter and add the following line:
@daily PFAD_ZUM_SHELL_SKRIPT
(where PATH_TO_SHELL_SCRIPT has to be replaced with your - who would have thought - path to the shell script). Save the cron job, which in the best case is acknowledged with
crontab: installing new crontab
What happens now? Every day at 24:00 the cron job will start your shell script, which deletes your old .qmail file (the one with yesterday's date) and creates a new .qmail file. Emails sent to the address with yesterday's date won't be forwarded, and emails you have sent to the address with the current date will be relaxedly forwarded to your primary mailbox. So before anyone gets the idea of selling your email address, it's already deleted again :-)
TL;DR
- Install qmail, if not already installed
- Create the bash script from above, adjust it and make it executable with chmod +x if necessary
- crontab -e
- @daily PATH_TO_SCRIPT
PS: Since I didn't have to install qmail myself, I unfortunately can't say anything about it. I'd be interested to know whether the installation was easy for you or whether there's something else to consider, though. Feel free to leave your opinions in the comments.