Page 1 of 1

How to forward INCOMING email into a php script first?

Posted: Fri Apr 28, 2006 5:56 pm
by Deseree
Any ideas on how to do this....


/etc/valiases/domain.com

i know.... but does anyone know like what it should look like?

*: /home/user/path/to/script/script.php


is that what it should look like or am I way off? I haven't tried it yet, thought i'd ask first.

Posted: Sat Apr 29, 2006 1:36 am
by Chris Corbyn
You're way off. That valiases directory contains aliases for the actaul emails to be passed on to another address (email address, not script).

You need to use a transport protocol that will likely be referred to as "pipe" in your MTA. I don't know what MTA you're using but I know it's trivial in exim once you get your head around it. I've always found MTA's to be damn tricky things to configure (immensely moreso than apache or ftp) but you can do some fun things :)

What Mail Transfer Agent are you running?

Posted: Sat Apr 29, 2006 1:42 am
by Deseree
d11wtq wrote:You're way off. That valiases directory contains aliases for the actaul emails to be passed on to another address (email address, not script).

You need to use a transport protocol that will likely be referred to as "pipe" in your MTA. I don't know what MTA you're using but I know it's trivial in exim once you get your head around it. I've always found MTA's to be damn tricky things to configure (immensely moreso than apache or ftp) but you can do some fun things :)

What Mail Transfer Agent are you running?
Cpanel/WHM, so Exim is the mail processor.....

Posted: Sat Apr 29, 2006 2:27 am
by Chris Corbyn
Deseree wrote:
d11wtq wrote:You're way off. That valiases directory contains aliases for the actaul emails to be passed on to another address (email address, not script).

You need to use a transport protocol that will likely be referred to as "pipe" in your MTA. I don't know what MTA you're using but I know it's trivial in exim once you get your head around it. I've always found MTA's to be damn tricky things to configure (immensely moreso than apache or ftp) but you can do some fun things :)

What Mail Transfer Agent are you running?
Cpanel/WHM, so Exim is the mail processor.....
OK if exim is the mail proccessor I'll show you how I currently use it to pass SPAM mail on my server to a PHP script to log it in a MySQL database.... you'll see the idea.

In exim.conf (usually /etc/exim/exim.conf but may vary):

Code: Select all

#This goes at the top of the config file... the path to system_filer.exim may vary
system_filter = /etc/exim/system_filter.exim
system_filter_pipe_transport = mysql_spamlog

#Down towards the bottom in the transports section
#Change the command to whatever your PHP script is (it needs a shebang in the script)
mysql_spamlog:
    driver = pipe
    command = /usr/local/bin/spam-mail-log
    user = mail
    group = mail
Now in system_filter.exim

Code: Select all

if
   "${if def:header_X-Spam_flag: {there}}" is "there"
then
   pipe "/usr/local/bin/spam-mail-log $sender_address $recipients $h_Subject: $message_body  $h_X-Spam_report: $h_X-Spam_score: $message_id"
endif
If that filter is looking for spam in my case.... if you actually want to pass *all* emails through the pipe (your php script then just put the line that starts with "pipe" by itself, outside of any conditions).

The variables available to you are $h_Header-name, $message_body, $recipients, $sender_address (the full list is in the manual).

Now all that's left to do it to write your PHP script to pipe your mail to:

Code: Select all

#!/usr/local/bin/php
<?php

$conn = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('spamassassin');

if (count($argv) < 8) die();

$copy = array();

foreach ($argv as $v) $copy[] = mysql_real_escape_string($v);

$pre_query = "select * from spamlog where message_id = '$copy[7]'";
$pre_result = mysql_result($pre_query);
if (mysql_num_rows($pre_result) > 0) die();

$query = "
insert into
    spamlog (
        sender,
        recipient,
        subject,
        body,
        report,
        score,
        message_id,
        delivery_time)
    values (
        '$copy[1]',
        '$copy[2]',
        '$copy[3]',
        '$copy[4]',
        '$copy[5]',
        '$copy[6]',
        '$copy[7]',
        now())";
mysql_query($query);

mysql_close();

?>
I've used a CLI-style PHP script, made executable and placed the path to the PHP interpreter in the shebang at the top.... you can probably just change the pipe command in exim to call the PHP interpreter itself though.

If you get stuck just ask (on here) ;)

Posted: Sat Apr 29, 2006 11:58 am
by Deseree
heh, O M G. lol, that's pretty deep man.

I see how it works and all, here's the situation, maybe you can help think of a solution....

We've got 400 domains on the server, and right now we use catchall in valiases "*: one-main-email@gmail.com". I would like to setup some kind of free email forwarding service for PRIVATE users, nothing big. so I would like SELECT email's to go to my email, and others, to be routed else where.....no mysql database, heh, over loads the server quick with the amount of spiders and spam we get.

So after I pipe into the php script, how do I let the mail continue it's journey if it matches those seleect emails, and if it doesn't, it goes else where in the script, obviously an if else statment, and I can write that, so the real question is

what part of the script takes the email from the pipe and releases it for delivery?