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
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.
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.
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/
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.
Related
I have a PHP script on my server, this PHP script gets specified emails from my mySQL database, it then pulls a whole bunch of data from the database which it uses to compose an email which it sends to each email address it pulled from the database.
In order to run this script once a day I have set up a cron job on my server which does so.
When I receive an email sent from this script, in the source of the email, it has the following line: X-PHP-Script: (exact address to PHP script)
My concern is that anyone, at anytime can go to that address and run this script over and over again, not only will it make everyone on the email list very irritated, but it will also clog up my server.
I would like to know if there is anything I can do, to disable someone from being able to run this script themselves, or if there is a better way I should be doing this which would also solve my problem.
Thank you in advance :)
You can place the PHP script outside the public website (for example, in Plesk, the public url starts in httpdocs, so you can place directly in a folder before this one).
Is there any tutorials out on the web that would show how to setup a program that would use piping, PHP and MySQL to setup email where replies would be sent into the database, or something similar to this?
Thank you in advance for any direction in finding out more about this.
PHP and MySQL are both mutually exclusive, and even more so from an email server.
To use PHP to receive email and insert it to a MySQL Database, you would have to write PHP code that is going to connect to a POP3/IMAP box and receive email directly and read new emails, subsequently inserting them into the database for handling them as you wish.
Luckily for you there are a whole host of mail related extensions for PHP. Note that you will specifically want to look at the set of imap_* methods (despite the name, they also work for POP3, etc).
I know, you could write your own SMTP server in PHP but who on earth wants to do that?
You can write own script which will check (via IMAP or POP3) for new e-mails in e-mail box and will store some e-mails directly into database.
Writing PHP CLI scripts is easy but there are some differencies between operation systems. See http://www.php.net/manual/en/features.commandline.introduction.php
The script can be periodically triggered via CRON or any other similar mechanism.
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
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.
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.
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/
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.
Is there a way to configure a server or a script to execute a php script when an email is received?
In theory this could be extended to other protocols, such as XMPP or SMS, etc.
What I have in mind is a user could send a message to checking-in#example.org and this would trigger a script which would then do whatever needed to be done, either irrelevant to the message (an automated message that gets sent whenever some other even occurs, like a server having issues) or related to the message (like it could store the subject in a database that other users could view as an RSS feed).
I know that most list-serve software have a means of sending commands (like unsubscribe), but I'm not sure how complicated the process is and if it is feasible to have something like this on a server-script level.
Would this need to occur at the IMAP/SMTP level, or could it be done closer to the script or HTTP server?
Just to give some context, one of the things I'm considering is a message-based clock in server for one of my work sites. Users could IM/email/text that they are at their scheduled location and this could trigger a script that would update a DB, rather then send the managers a direct message they need to log. This would just be one option (a web-based method is already in the works), but we want to make it as platform/protocol independent as possible. But if we can achieve that, we can look at implementing it for many other day-to-day needs.
Another way of asking might be: is there a way to have "discovery" of users from a server-script app or does something need to be doing a constant check to relevant sources to see such changes?
If you control your own machine you can configure it to pipe the e-mail through a filter. Simplest would be to setup a new account and setup a .forward or alias
The following examples are for Sendmail, but (all?) Unix e-mail programs offer a similar service.
Setting up an alias (option 1)
Look in the directory /etc on your server for your alias file. Add the line:
script: "|/path/toyourscript/pipe.php"
Using a .forward file (option 2)
Create a .forward file in your main home directory.
"|/path/toyourscript/pipe.php"
or:
myemail#example.com,"|/path/toyourscript/pipe.php"
If you are on shared hosting then most hosting providers also provide the possiblity to "pipe" e-mails received to a particular account through a script instead of storing them in a mailbox. Check the CPanel setup.
Use a cron job to check for emails through a pop3 interface. If an "interesting" mail is found, run your php script.
Note that the cron script can be in PHP too.
As jldupont said, it is easy to do ith php itself, simple reading the smtp continuosly with a cronjob.
Otherwise, you could use a daemon, in python for example.
Both ways allow you to do an important thing: check if the sender of the email is a your user (you have the users in a db, right?), or a spambot (nomatter what kind of anti-spam you use, soon or later, checking-in#example.org will be full of spam)
We've built a web service that needs to check constantly for email messages. Basically, an user sends us an email, and the server should make actions based on that email. We could use a crontab PHP script that checks for new messages every minute, with POP. But that's kind of offensive to the popserver and not very efficient (1min is too long).
But, I've read about PUSH email using IMAP around mobile devices. In my case is not a mobile device but a webserver.
Can I push an email to my webserver and have it execute a PHP script? We're using GMail as POP/SMTP/IMAP server.
EDIT 1 from the answers, we figured out:
there must be a 24/7 running process (daemon) on my webserver checking for emails
this daemon may communicate with Gmail using: i) POP with NOOP coomand or ii) IMAP with IDLE command
What's the best? POP or IMAP? Google seems to invite more the use of IMAP.
I don't want to overuse gmail (what's their 'fair use' for checking email? every 10secs?
If you want to use postfix as your MTA you can alias an email address to any executable like this. It really isn't that difficult to setup on a *nix box...
I've no experience with IMAP, but had to do the same thing. If I were you, I'd install Postfix and use the pipe command (basically lets you run an arbitrary script) to invoke a small http client that parses the email and sends it to your server.
You can replace postfix with whatever MTA you'd like, of course. The point is: don't implement your own email server. Use an existing one and use its hooks to send the mail off to where ever it is you want, however it is you want.
email_scheduler.c
int main()
{
while(1){
sleep(5); // every 5 secs the email_parser.php would get executed.
system("/usr/bin/php -q /var/www/html/email_parser.php");
}
}
email_parser.php :
/* Use any email message parser to
parse the email messages like MIME
message parser based on your purpose
: Refer :
http://www.phpclasses.org/browse/package/3169.html
*/
Also check relevant answers here for more info
There's an old (but great) article on Evolt about piping emails to a php script, which therefore gets the full content of the email and can act on it.
http://www.evolt.org/article/Incoming_Mail_and_PHP/18/27914/index.html