<p>Working on a little side project web app...</p>

<p>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.</p>

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

<p>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.</p>

<p>I'm very slowly learning the ropes (PHP, MySQL, configuring/managing the server), so your advice and insight are much appreciated.</p>

<p>Cheers,
Matt Stuehler</p>

[{'answer_id': 33472, 'author': 'crono', 'author_id': 1462, 'author_profile': 'https://Stackoverflow.com/users/1462', 'pm_score': 0, 'selected': False, 'text': '<p>The Cronjob is the common solution to such a task. <a href="http://php.net/manual/en/function.imap-getmailboxes.php" rel="nofollow noreferrer">Checking for new Mails with PHP</a> is no Problem. If you run a qmail-server (maybe other servers can do this too?) you can <a href="http://www.faqts.com/knowledge_base/view.phtml/aid/34099/fid/139" rel="nofollow noreferrer">fire a script</a> on every "received mail", which triggers your php script.</p>\n'}, {'answer_id': 33479, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n\n<p>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:</p>\n\n<p>xkcd.com/327/</p>\n"}, {'answer_id': 33488, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 0, 'selected': False, 'text': '<p>You can use a <a href="http://activecampaign.com/support/tt/index.php?action=kb&amp;article=145" rel="nofollow noreferrer">.forward</a> file.</p>\n\n<p>Just place the full path of your PHP script into the file, after a pipe sign:</p>\n\n<pre><code>|/full/path/to/script.php\n</code></pre>\n'}, {'answer_id': 33921, 'author': 'hoyhoy', 'author_id': 3499, 'author_profile': 'https://Stackoverflow.com/users/3499', 'pm_score': 2, 'selected': False, 'text': "<p>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. </p>\n\n<pre><code>:0:  \n* ^(From).*test@example.com\n{   \n  :0 c\n  | php /var/www/app/process_email.php\n\n}\n</code></pre>\n"}, {'answer_id': 804920, 'author': 'Warren Krewenki', 'author_id': 98028, 'author_profile': 'https://Stackoverflow.com/users/98028', 'pm_score': 1, 'selected': False, 'text': '<p>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 foo@bar.com, 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:</p>\n\n<p>/home/email/foo@bar.com/.forward</p>\n\n<p>The contents of that file were as follows:</p>\n\n<p>"|/path/to/script.php"</p>\n\n<p>Also, the .forward file\'s owner was foo@bar.com, and it was chmod\'d to 600 (read/write to owner only.)</p>\n\n<p>Next, you need to setup the script you\'re piping the mail to (/path/to/script.php above.)\nFirstly, 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.</p>\n\n<pre><code>#!/usr/local/bin/php\n&lt;?php\n$fd = fopen("php://stdin","r");\n$email = \'\';\nwhile($feof($fd)){\n    $email .= fread($fd, 1024);\n}\nfclose($fd);\n?&gt;\n</code></pre>\n\n<p>Hopefully that was of some help to you.</p>\n'}]
['https://Stackoverflow.com/questions/33465', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']