tags:

views:

197

answers:

5

Working on a little side project web app...

I'd like to have it set up so that, when users send email to a certain account, I can kick off a php script that reads the email, pulls out some key info, and writes it to a database.

What's the best way to do this? A cron job that checks for new email?

The app is running on a "Dedicated-Virtual" Server at MediaTemple, so I guess I have a reasonable level of control, which should give me a range of options.

I'm very slowly learning the ropes (php, mysql, configuring/managing the server), so your advice and insight is much appreciated.

Cheers, Matt Stuehler

A: 

The Cronjob is the common solution to such a task. Checking for new Mails with PHP is no Problem. If you run a qmail-server (maybe other servers can do this too?) you can fire a script on every "received mail", which triggers your php script.

crono
A: 

if you have control of a mail transfer agent that is configurable to allow .forwards or similar configurable delivery options (qmail, postfix, and sendmail all are), i'd just set the script up in your .forward, .procmailrc, or other similar programmable delivery mechanism. when doing this, you should do some serious input validation on the mail (make sure the sender is who you expect, the received lines match up, the data is sane) if you don't want others who stumble onto the address to be able to muck with your system.

you'll also want to use whatever input sanitizer php uses to avoid things like sql injections from malicious data! we can all reflect upon the lesson of little bobby tables:

xkcd.com/327/

A: 

I would guess a .forward file

Pat
+4  A: 

Procmail is how I do it. Here's an example where I actually process the text inside the email to archive it back to a MySQL database.

:0:  
* ^(From).*[email protected]
{   
  :0 c
  | php /var/www/app/process_email.php

}
hoyhoy
A: 

I recently worked on a project that had this need. I had great success using a .forward file in the mail accounts home directory. For example, let's say you're trying to do this for the address [email protected], and the server you are working with is the mail server for bar.com. You would first need to create a .forward file for this account. On the server I worked on, this would be:

/home/email/[email protected]/.forward

The contents of that file were as follows:

"|/path/to/script.php"

Also, the .forward file's owner was [email protected], and it was chmod'd to 600 (read/write to owner only.)

Next, you need to setup the script you're piping the mail to (/path/to/script.php above.) Firstly, that script needs to be executable (+x). The rest simply reads STDIN and handles it however you wish. Here's a sample script that reads the entire message and stores it in a variable $email.

#!/usr/locacl/bin/php
<?php
$fd = fopen("php://stdin","r");
$email = '';
while($feof($fd)){
    $email .= fread($fd, 1024);
}
fclose($fd);
?>

Hopefully that was of some help to you.

Warren Krewenki