is it possible to issue commands to a PHP script through e-mail?

bitt3n

Senior member
Dec 27, 2004
202
0
76
I want to code a PHP script that will recognize when I have sent an email to a particular e-mail address to which the script has access, then parse the email to identify a command, and act on the command.

For example, I want to be able to send an email from my phone to new_users@mywebsite.com, and have a PHP script hosted at mywebsite.com receive the email and reply with the number of new users registering on my site in the last 24 hours, and other info (nothing that needs to be secure).

If anyone has suggestions on how to do this, or can point me to a resource that will help, I would appreciate it. (I should mention that I am no expert -- just tinkering around with PHP.) Thanks for your help.
 

apinomus

Senior member
Dec 14, 2005
394
0
0
Why not write a small script that polls your database with accounts created within the last 24 hours? You are timestamping their creation date right? :)
 

bitt3n

Senior member
Dec 27, 2004
202
0
76
true enough. however, I want to do a things that are a bit fancier than that, too. for example, another thing I want to be able to do is send an e-mail message to my site, have the script post the contents of the e-mail onto a web page, and then respond to my e-mail address confirming that the command was completed successfully.
 

apinomus

Senior member
Dec 14, 2005
394
0
0
The e-mail on completion thing is fairly easy, it's just finding a way to read the e-mail. I haven't done this before, but by the looks of it, you can create an e-mail address say "newspost@mywebsite.com" then create a script using the imap_open() command to login to the account and read the mail, then possibly dump it to a .txt file on the server and you just call a if(fopen($yourfile)) include $yourfile .

My concern is, having to limit the newspost@mywebsite.com to only accept mail from a certain address/IP or going a less-secure route and just making the e-mail address up of somewhat random characters, but store it in your address book to e-mail to.

Hope that helps.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
The easiest way to do this is to set the PHP script to run once every minute checking for new mail, and if it finds any, then it does whatever it is that you want it to do. The unix mbox mail format is not that complicated to read.

 

bitt3n

Senior member
Dec 27, 2004
202
0
76
Originally posted by: apinomus
you can create an e-mail address say "newspost@mywebsite.com" then create a script using the imap_open() command to login to the account and read the mail, then possibly dump it to a .txt file on the server and you just call a if(fopen($yourfile)) include $yourfile .

My concern is, having to limit the newspost@mywebsite.com to only accept mail from a certain address/IP or going a less-secure route and just making the e-mail address up of somewhat random characters, but store it in your address book to e-mail to.

Hope that helps.
at your link I found a link to a script that seems to describe how to parse email into MySQL: http://www.sellchain.com/phPOP3/phPOP3.txt

I'll be playing around with that to see what I can do. My initial understanding is that I can automatically run this script every couple minutes to check for new mail, copy the mail into MySQL (and delete it off the server), then perform operations using normal MySQL queries. Does that sound reasonable?

I'm not concerned about security because the info I intend to send and receive is public anyway, and I may want to allow registered users of my site to send commands to the site via e-mail. However I admit I haven't thought this through carefully.

Originally posted by: jfall
You can "pipe" a forward of the email to your php script.
I googled "PHP pipe" and it appears that pipes are what allow a site to (among other things) receive e-mail and send an automated response. E-mail gets forwarded to a pipe script, which then forwards it to an e-mail parser that performs the appropriate action. That sounds like exactly what I want to do. However, I haven't yet learned how the pipe method differs from what apinomus suggested above. Is pipe a special way to do what he describes? If you can point me to a resource that describes how to set up a simple pipe, which, for example, takes an e-mail and parses it into MySQL, that would be great. Thanks for your help.

Originally posted by: notfred
The easiest way to do this is to set the PHP script to run once every minute checking for new mail, and if it finds any, then it does whatever it is that you want it to do. The unix mbox mail format is not that complicated to read.
interesting, I'll do some research on this. Do you know where I could fine an example script that shows it working? Thanks for the suggestion.
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
I have done this before. One quick'n'dirty solution is to put a "wget http://mysite.com/myscript.php" into the crontab. I think with -q to run it quiet. In the crontab you can set how often you want the script to be loaded.
If you have php5, you can also run the script directly from the command line.
Works like a charm.
 

Hersh

Senior member
Oct 14, 1999
331
0
0
Originally posted by: jfall
You can "pipe" a forward of the email to your php script.
I googled "PHP pipe" and it appears that pipes are what allow a site to (among other things) receive e-mail and send an automated response. E-mail gets forwarded to a pipe script, which then forwards it to an e-mail parser that performs the appropriate action. That sounds like exactly what I want to do. However, I haven't yet learned how the pipe method differs from what apinomus suggested above. Is pipe a special way to do what he describes? If you can point me to a resource that describes how to set up a simple pipe, which, for example, takes an e-mail and parses it into MySQL, that would be great. Thanks for your help.

How to pipe depends on the mail server, but it'll basically be like setting up a 'filter' to trigger a re-direct data to a command (in this case, a php script). The php script can then read in the data from STDIN and do whatever with it.

Some resources I found real quick:
http://www.computing.net/webdevel/wwwboard/forum/1072.html
http://www.evolt.org/article/Incoming_Mail_and_PHP/18/27914/ (this one looks good)

Good luck
 

bitt3n

Senior member
Dec 27, 2004
202
0
76
great, that evolt article looks promising (full article at: http://www.devarticles.com/c/a/PHP/Incoming-Mail-and-PHP/1/ )

so now I have a few newb questions to annoy you all with..

The article says: "The first thing we have to do, is tell our mail system to send all mail addressed to script@example.com to our script. How to accomplish this, depends on which mail system you use."

The article gives a few examples, Sendmail, Exim, and Qmail. (I read elsewhere about these, and apparently they are called Mail Transfer Agents, and sit between the Mail User Agent and the Mail Delivery Agent.) For each MTA the forwarding options are basically either creating a .forward file or an alias, if root access is available.

I figured the first thing to do was to try to make a .forward file and try forwarding mail somewhere, so I stuck a file called ?.forward? and containing an e-mail address into the mail directory for an email account on my site, but apparently it was the wrong place, because it didn?t work, and other locations I tried also failed.

I then read about Sendmail aliases, which are apparently kept in the file /etc/aliases, but I cannot find this in my directory anywhere. I am running my site off a shared server, so maybe I don?t have access to the directory -- I have no idea. (I also can?t locate obvious way to determine what MTA my hosting providers (modwest.com, xr4free.com) use.)

Once I set up the forwarding, it appears that I can forward the incoming email to a PHP script for parsing, but I need some advice on properly setting up the .forward and/or alias file.

Second of all, it looks like this is one of two ways to make PHP accept e-mails, the other of which is using crontab to check periodically for email to parse, as RSMemphis and others suggested above. Unless I am mistaken, the latter method seems to have the advantage that parsing is not dependent upon any specific "to:" address that triggers forwarding to the script. This would mean that if I wanted to make each user's e-mail address a command (for example, mail sent to any address such as username@mysite.com alters the relevant user's account in some way), I could do that without adding this address to a .forward or alias file manually for each user. Perhaps you can tell me if I am understanding this correctly. If I understand this correctly, then perhaps a pipe is not the best way.

Thanks again and sorry if these questions sound kind of stupid ? I have very little experience with this stuff. I will keep searching around so that I can avoid asking questions whose answers are available online somewhere.