Web Hosting Talk







View Full Version : Executing a file when receive email


Omair Haroon
11-15-2001, 02:35 PM
Hello Guys,

I have a client who wants to execute a file whenever he receives an email to his address.

For example, he has created a user bot@hisdomain.com and wants a file /home/sites/www.hisdomain.com/web/mail_action.php to be executed when ever he receives an e-mail.

Can anyone tell me how I can do this??



-Omair

skylab
11-15-2001, 03:39 PM
what's the action he wants to happen?

is it in anyway like an auto-reply?

Omair Haroon
11-15-2001, 04:31 PM
Well, don't know much about it. The only thing he needs is the script to execute whenever an e-mail is received.

I think he is programming a Ticket Support system.



-Omair

nudetravel
11-15-2001, 04:43 PM
Omair:

I don't think sendmail will do that. He will probably have to have the php script check his mailbox for messages and then take action accordingly. You might be able to modify one of the scripts at http://www.hotscripts.com/Perl/Scripts_and_Programs/Email_Systems/Email_Utilities/ to check the mailbox and wakeup the php script.

technoart
11-18-2001, 09:19 AM
Originally posted by Omair Haroon
Hello Guys,

I have a client who wants to execute a file whenever he receives an email to his address.

For example, he has created a user bot@hisdomain.com and wants a file /home/sites/www.hisdomain.com/web/mail_action.php to be executed when ever he receives an e-mail.

-Omair

In theory at least, it should be possible to to execute a program from the user's (mail) .forward file (hopefully in a somewhat secure manner, e.g., via smrsh (restricted shell, etc.)).

Getting this method to actually work however has proved to be somewhat of a challenge (at least for me) on a RAQ... :( If someone has figured this process on the RAQ, I'd love to hear more 'bout it! :>

thewitt
11-18-2001, 12:50 PM
procmail is the standard *nix solution for this.

-t

jaschwa
11-18-2001, 04:03 PM
I had the same problem. See my thread on the Technical forum: http://www.webhostingtalk.com/showthread.php?threadid=26423

jucebro
11-21-2001, 03:58 PM
Try using the .forward method in jaschwa's post. You would have to add a link to the cgi / shell script you want to be allowed to forward to to the "/usr/adm/sm.bin/" directory.

If he wants to execute a .php script, try calling the .php through wget. for example:

in the .forward file put: "| execme_when_emailed"


in the /usr/adm/sm.bin/ directory, make a link named execme_when_emailed that links to the file you want executed.

e.x. execme_when_emailed -> /home/sites/www.your-domain.com/web/cgi-bin/script-to-run.cgi

then, in the script-to-run.cgi or .sh make a call like this:

`wget http://www.your-domain.com/phpfile-to-exec.php`


This way, you would send an e-mail to bot@your-domain.com, the .forward file redirects it to /usr/adm/sm.bin/execme_when_emailed, which is a link to /home/sites/www.your-domain.com/web/cgi-bin/script-to-run.cgi. The script-to-run.cgi makes a call to `wget http://www.your-domain.com/phpfile-to-exec.php` which will then execut the phpfile-to-exec.php.



This is tricky. and I'm not sure I explained it too good. But I have seen it work with a .cgi, not 100% sure a shell script will work though.

technoart
11-23-2001, 07:12 AM
Ok.... this works on a RaQ3. Here's is a sketch of what's required.

(1) PHP

Compile PHP4 as a standalone binary. *Read* docs @ PHP.net for how-to-compile info + possible security issues:

http://www.php.net/manual/en/security.cgi-bin.php

(2) The PHP script

If you've compiled PHP standalone, PHP parser outside of the web tree, you will then need to add the following line as the very first line of your PHP script.

#!/usr/local/bin/php -q

You will also need to make the (PHP script) file executable, e.g., chmod 755.

(3) The .forward file

Login (SSH) as the user (that mail will forwarded to PHP script).

Create .forward file, e.g., if you want to forward mail to a script called "mail_handler.php" for a user named "mailtoPHP", then .forward file will look like:

mailtoPHP: "|mail_handler"

(4) smrsh

Login as SU.

cd /usr/adm/sm.bin/

Create a sym-link to the script, e.g, ln -s /home/sites/home/web/scripts/mail_handler.php mail_handler

(5) Test it
Send e-mail to mailtoPHP@YOUR-DOMAIN-HERE.com

If all goes well w/your PHP script, the desired result should be achieved.

Omair Haroon
11-23-2001, 09:54 AM
I will check what technoart posted and will post the results by tonight!



-Omair

technoart
11-23-2001, 09:18 PM
I have working example online of the e-mail to .forward to PHP script process I described in my earlier message. In this demo, e-mail from the Cobalt Users mailing list is sent to a user's mailbox on my RaQ3. When the e-mail is received, the .forward file directs the mail to a PHP script. The PHP script takes the content of the mail, amd inserts it as a message in a (MySQL) threaded message forum.

You can view this email-to-forum example at: http://www.wvwebsites.com/forum/

NewonNet
11-25-2001, 05:46 AM
Originally posted by technoart


In theory at least, it should be possible to to execute a program from the user's (mail) .forward file (hopefully in a somewhat secure manner, e.g., via smrsh (restricted shell, etc.)).

Getting this method to actually work however has proved to be somewhat of a challenge (at least for me) on a RAQ... :( If someone has figured this process on the RAQ, I'd love to hear more 'bout it! :>

We have an autoresponder system that is installed and working on Raq4i

The setup is rather simple actually.
No .forward file needed, just .procmailrc

Omair Haroon
11-25-2001, 06:16 AM
Tom,

Care to explain how you set it up through .procmailrc ?



-Omair

NewonNet
11-25-2001, 06:45 AM
Here's a sample included code we have.

#demo1@mydomain.com
:0
* ^TO_demo1@mydomain.com
{
FROM=`formail -rtzxTo:`
USER=`formail -tzxTo:`
SUBJECT=`formail -rtzxSubject:`

:0i
| /home/sites/www.mydomain.com/web/cgi-bin/process.pl
}
#demo1@mydomain.com


Process.pl would run everytime an email is received at
demo1@mydomain.com