pop2smtp: POP3 ("catchall") to SMTP with multiple destinations

Translation of the original in Catalan. Read it in: CatalàCastellano

Some ISPs let us have an account that collects all the mail sent to a domain; this, for example, can be used as a mail-backup of our domain. Well, through fetchmail it is very easy to re-inject the mail stored in a POP3 account into an SMTP server against a user. The most complicated thing is when the mail that is in the POP3 account can go to more than one destination, like the one I mentioned a moment ago in POP3 accounts of the 'catchall' type or cul-de-sac as some call them.

So, for this reason I have made a small script in python that, starting from a BSMTP file, modifies the RCPT TO command according to the information of the Envelope to tag, which contains a list of the destination email accounts to which the mail is addressed. So, basically the task consists of looking for which email accounts appear in the aforementioned tag and then deleting the RCPT TO command and creating as many commands of that type as needed in its place to send copies of the mail we want to send to their destinations. This new modified BSMTP file we inject into our MTA. This MTA will be in charge of processing those mails and delivering them to the destination accounts.

In my case I have decided to make a small shell script that is run from the crontab every 10min. This script calls fetchmail to get the BSMTP file with the mail that was in the POP3 account, then it calls the python script to modify the incoming BSMTP file as I have mentioned and creates a new outgoing BSMTP file. Finally this file is injected via SMTP by calling a small script that, thanks to NetCat, sends the content of the outgoing BSMTP file over SMTP.

The script with the directory structure packaged in a file: pop2smtp_r1.tar.gz.

I am sure there are more elegant ways of solving the problem, but the truth is I haven't been able to find them.