[EN] Email flow modifier

We have developed a technology that we have called “Email flow modifier“. Some people have interest on this subject, we will expose a technical text about how we have implemented it.

The rest of this article has technical connotations.

(This article is also availiable in spanish).

Why email flow modifier ?

This system allows us to develop rate of reply and response time. It is important to us to recollect statistical information about email contact between owners and users, because this is the critical section in our business.

It also allows us to prevent “spammer behavior“. You may have a web form in whitch you want to allow contact between a user ([email protected]) and a service ([email protected]). Then, from yourdomain.com you are sending an email to [email protected] from [email protected]. Implementing the email flow modifier, all emails are sent as [email protected]. So you are not having any more the “spammer behavior”.

General system view


Phase 0: User is writing at service-[code]@yourdomain.com on first time, using sender [email protected] (Email address service-[code]@yourdomain.com and context can be generated using a web form).

Between phase 0 to 1, on the email server:
A code for this user referring to that service is generated (user-[code]@yourdomain.com).

Phase 1: The service gets a new email from user-[code]@yourdomain.com.

Phase 2: The service replies to the user using the user-[code]@yourdomain.com email.

Between phase 2 to 3, on the email server
The email adreess has user-[code]@yourdomain.com format. From this code, we can get the real address [email protected] and the recipient address service-[code]@yourdomain.com.

Phase 3: The original From and To are changed, and this new email is sent. The users receives a new email from service-[code]@yourdomain.com.

Phase 4: Is is basically identical to Phase 0, but now we already know the user code, so we do not need to create a new one.

Email server requisites

It is likely that the system we implemented can be done on another email server. Basic requirements:


The hardest part on this project is to get the theoretical part working. The script will be as complex as additional functionalities are being implemented. The programming language we choose is Perl, but you can do the same in several others.

First, we need to create wildcards email accounts. We are using qmail, so the way to do it is creating two files, .qmail-user-default and .qmail-service-default. The content on these files will be the same:


So, when a new email is sent the script will take care of it.

Then, in our script, we read the entire email:

while ($mail=<STDIN>) {
$completeemail .= $mail;

Then we split the headers from the body, and we modify the headers so each header will be on one line.

@tempmail = split(/\n\n/, $completeemail, 2);
$header = @tempmail[0];
$header =~ s/\n\s+/ /g;

Then we create an associative array with email headers (name/value)

%hdrs   =  (UNIX_FROM => split /^(\S*?):\s*/m, $header);

Now we can check all information we need from To, CC and From headers. We replace whatever we need (we can also do it in the body, if necessary) and then we get our transformed email.

We save all the information in a file, and invoke qmail-inject on order to send the email properly.

$command = "/var/qmail/bin/qmail-inject < /tmp/newmail.tmp";
$res = `$command`;

Qmail-inject will interpret the email and sent it to the recipients.

At last, we return the code “99” to tell qmail not to further process the original mail.

Things that you may have to take care

  • Be very careful with email loops.
  • You need to send additional emails to remitents when an error ocurred. (user is using wrong codes).
  • If the final user or service email is wrong (quota error, or emails does not exists any more) the error email notification will be returned to the sender.
  • If an email is sent using BCC address, the system will not work. An error email must be triggered.
  • Automatic responses from email clients (email has been received, or holidays notice) will not work. This is because most of the emails clients send this using BCC.


  • If spammers discover this system, and start to send emails using brute force to email wildcards, impact can be minimized using the validrcpto patch.


Escapadas San Valentín


[ES] Implementación del nuevo sistema de e-mails

1 Comment

  1. [ES] Implementación del nuevo sistema de e-mails

    En Rentalia.com hace unos meses hemos implementado un sistema llamado “Email flow modifier”. Dado a que existe interés por este tema, vamos a comentar cómo lo hemos implementado técnicamente, y las consideraciones adicionales que hemos discutido.El res…

Comments are closed.

Funciona con WordPress & Tema de Anders Norén