1

I've been asked to provide a way of loading incoming e-mails, manipulate some part of the text and then forward it to the web server for the client to read. Basically, intercept the mail, put it through another process and once this process is finished - this is important - throw it at the web server, exactly as it was going to happen before my process intercepted.

Besides the actual e-mail file I also need to read information about the account from a MySQL db.

The mail server is Postfix/Dovecot and the Web server is Apache - running on CentOs. They could be running on the same server or not. I do not want to do the server's work and scan for viruses or do any authentication - all this should have already happened.

I have looked into some caching ideas, or even using NginX as an IMAP proxy but I think it's like trying to kill a fly with a shotgun. To make things worse I'm not actually a web person so I don't know all the Apache intricacies.

Does anyone have any ideas as to when is the right time to intercept this e-mail before it gets to the browser in a seamless way ?

Any help would be much appreciated.

Many Thanks

guestTM
  • 11
  • 1
  • Your explanation is a bit unclear. Is MTA deliver messages by itself or dovecot-lda invoked instead? When you say that message is forwarded to the webserver - do you mean some webmail-client? Do you have a control over the all mail system components? – Kondybas Oct 02 '13 at 15:25
  • Sorry, should have been clearer. Postfix is moving the files. Yes the client is a browser. I 'think' I have access to all the components. – guestTM Oct 02 '13 at 15:29
  • Just to clarify, Postfix listens to incoming emails, and when he receives one, executes an action which is usually writing it somewhere on the disk (where the client can read it). The email isn't really *forwarded* to the client, unless this client is another mail server... but certainly not a web server. To be read with a webmail software (what you probably refer to as *web server for the client to read*), there must be a request from the client and this is totally asynchronous (you probably don't want to play with WebSockets). – Calimo Oct 02 '13 at 16:05
  • Thanks. I understand. So I should be able to read the incoming mails from the disk after Postfix has created each one. Something like scanning each directory sequentially, loading the mail, passing it to another process and moving to the next dir, in a continuous loop. – guestTM Oct 02 '13 at 16:14

2 Answers2

2

The easiest way is to perform LDA not directly from postfix, but rather by invocation of the dovecot_lda wrapped into the some script.

Let modify postfix config:

mailbox_command = /some/path/to/the/script.sh "$SENDER" "$RECIPIENT"

Let script.sh contains such code:

#!/bin/sh

MSG=$(cat < &0)
ARG1=$1
ARG2=$2

# let's deliver message as usual
echo "$MSG" | /usr/lib/dovecot/dovecot-lda -f $ARG1 -a $ARG2

# from here we can do anything we want
echo "$MSG" > /some/path/to/the/fresh/$$.copy
echo $MSG | while read line
do 
   if [ $line != '' ]
   then
      echo $line >> /some/other/file.txt
   else 
      echo '---------------------' >> /some/other/file.txt
      break
   fi 
done
#####

Then your software that generate webpages needs only to read everything from the /some/path/to/the/fresh/* and include them into html.

Kondybas
  • 6,964
  • 2
  • 20
  • 24
0

You probably want to have a look at Amavis. It is mostly advertised as an antivirus, but you can disable this and run whatever perl script you want. You will find example scripts included, some of which contains call to a database. It integrates very well with Postfix/Dovecot.

Calimo
  • 410
  • 2
  • 6
  • 15