views:

147

answers:

4

Hi,

I'm building a photo sharing website (just testing PHP stuff) and I want users to be able to submit photographs using email.

For instance, the website needs to run a script when the users sends an email with an image attachment and text in the body. The image would be uploaded to the server, and a new "photo post" would be created with the text in the email body being the description.

My question is, how do I tell my server to run a script automatically when email is RECEIVED?

Any/all help is greatly appreciated. If you would like more info, just comment!

Thanks! -Giles

A: 

You want to use cron. It's the standard Unix way of running scripts on a regular basis without any user intervention. Create your script, make sure it can be run by the server user, then schedule it (the full command, e.g., php myscript.php in cron). It won't run when an email is received, but you can run the script often enough that the difference won't be noticeable.

Tom
I'd like a little info on the downvote-- yes, cron isn't going to push email directly to a script when the email happens, but it will allow you to manage the flow of traffic. If he's receiving large attachments and a high volume of email, I think it would be better to have a job that runs every given time period, checks to see if the last run is still going, etc.
Tom
A: 

Alternatively, you may be able to "pipe" the email directly to your PHP script. The process to do so will slightly differ depending on your email suite and/or server control panel software. You may be able to get "inspiration" from the Kayako manual at http://www.kayako.com/manuals/Kayako_SupportSuite_User_Manual_PDF.pdf (page 61 onwards) which shows how to setup email piping to the Kayako support helpdesk. You'll have to write the PHP file which reads in the file from STDIN yourself though.

Richy C.
+1  A: 

If you are running the website on a UNIX server you have access to you can do this with procmail, sieve or similar mailtransport helpers. You would have to create a useraccount for the alias recepient as procmail is only invoked to process "real" users mail. Your .procmailrc would look something like this:

:0
*
| /usr/bin/php /path/to/your/script.php

And remember that procmail will pass it's info as arguments (and in the env variables).

The above scenario might not at all be possible for you, but if it is then I recommend taking a closer look at http://www.procmail.org/

kb
+1  A: 

If you use cpanel you can pipe the email to a script which then processes the email accordingly. You can find that option under the email forwarders.

John Conde