PDA

View Full Version : Forwarding Email to a Script


pmarg212
01-07-2007, 10:26 PM
Greetings,

I am new to hostmonster. Before I transfer my existing domain name over to the server, I want to ensure that I can get one of my scripts working. The script is intended to receive an incoming email to a particular email account (say me@mydomain.com), parse it, do a database query, and reply with an email. On my previous host, I used a .forward file to pipe incoming emails to a script.

Since I have not yet transferred my domain, however, I cannot figure out how to send myself an email to test that I can forward incoming emails to a script. I know I can receive email at my-login-name@hostxx.hostmonster.com (my default email address). But I do not know how I can forward this email to a different email account (to test the script) or directly to the script itself.

Does anyone have any experience with something like this to let me know if what I want to do is possible?

Thanks for any insights you can provide!

steinitz
04-14-2007, 10:47 PM
Hello,

Did you ever get a reply to this?

Anybody?

Thanks,

Steve

charlesgan
04-15-2007, 07:58 AM
what you trying to achieve? incoming email initiate the script and process the mail itself??

what you can do is find the script that do the email retrieving and processing. use con job to schedule it to run by fix interval. thus your email will be process every 10 minutes for example.

sjlplat
04-15-2007, 09:44 AM
what you trying to achieve? incoming email initiate the script and process the mail itself??

what you can do is find the script that do the email retrieving and processing. use con job to schedule it to run by fix interval. thus your email will be process every 10 minutes for example.

That is what I would do. A php script can retrieve and parse the email, query a database, and send a response. A Cron job can run the php script at a specified interval.

steinitz
04-16-2007, 06:12 PM
Thanks for the replies. They are a lot more helpful that the conversation I just had with tech support.

I am trying to configure 1MerchantCart's autoresponder. Here is a summary of the instructions:
1.set up a catch-all email address in your hosting panel
2.place a ‚.forward‘ file in the directory of the email you created
under in your web space containing the code provided on the Mail
Forwarding Page in the 1MerchantCart Admin area.
For this to work, your hosting provider must have the feature which
allows incoming emails to be piped through to a script or program. This is
required for the autoresponder to accept subscriptions by email.

As an alternative to catch-all 1MerchantCart's offers this cpanel technique:
If you are using cpanel, you can create an email forwarder and in the
forwarding field you enter the path to the email parser script, which
is the file 1MerchantCart uses to handle incoming email.
For example, if the autoresponder name is course@1merchantcart.com, in
cpanel you would create a forwarder called course@1merchantcart.com
and in the the forwading field, enter:

/path/public_html/cartdir/campaign/campaign.mailforward.php

According to hostmonster tech support neither of these techniques will work.

Regarding your suggestions of a cron job, I not yet able to get it clear in my head. I guess I would have to look in the campaign mail folder and pipe the email to campaign.mailforward.php. I notice that 1MerchantCart's campaign.mailforward.php script includes the code
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
I'd appreciate any clarifications of what I would need to do.

Thanks,

Steve

steinitz
04-17-2007, 07:24 PM
Hello

Cade Ekblad-Frank from hostmonster tech support sent me a helpful email in reply to my ticket about mail forwarding.

He pointed out that the previous info I had been given is wrong and that you can indeed forward to a script. Here is what he said:

use a forward along the lines of:

|php /full/path/to/your/script.php

Without an interpretor (PHP), the server does not know how to execute the file that is piped.
I believe his is referring to a .forward file in the relevant email directory.

Since the system I am setting up is complex I would like a way to test just the mail forwarding component. Can anyone suggest a simple bit of script to put in the .forward file that would verify that the forward is working?

Thanks,

Steve

steinitz
04-19-2007, 09:11 AM
Hello,

Following up on my own post again.

The .forward file seems to be ignored. I have placed a call to a test script in the forward file as follows:

"| php /home/trustmos/public_html/trustmost/1mc/campaign/filecreate.php"

and also:

test-course, "| /usr/bin/php /home/trustmos/public_html/trustmost/1mc/campaign/filecreate.php"

Neither works.

I have verified that the account (test-course@trustmost.com) gets mail and the my test script, filecreate.php, works.

I have asked hostmonster tech support to verify that sendmail is configured to correctly process the .forward file.

Steve


ps here is the test script:

#!/usr/bin/php
<?php
$ourFileName = "/home/trustmos/public_html/testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
?>

steinitz
04-20-2007, 06:29 AM
Hello,

I finally have a technique that works.

Make sure to set the permissions of the target script to 755.

Set up an email forwarder in cpanel. Set the destination to

|/usr/bin/php -q /home/path/to/your/script/yourscript.php
(No quotes.)

As a side note, the following also forwards (bad code):

|/home/path/to/your/script/yourscript.php

but it generates a returned email with the subject

Mail delivery failed: returning message to sender

I think its the -p in the php command that prevents the mail delivery message.

Note that in some cases you will also want to add a second forwarder to actually forward the message. You can do that manually or like this:

fred@yourdomain.com, |/usr/bin/php -q /home/path/to/your/script/yourscript.php

The result being that cpanel creates two forwarders for you.

I hope this saves someone some time and tears.

Best regards to all,

Steve

jonasr
06-01-2007, 10:54 PM
OK, I understand that when you receive email at that address, the PHP script is run. How then do you get the email address of the original sender into your PHP script so that you can respond to them? Will parsing the $argv have the address?

Are you using the PHP script to send a reply, or just do something other than reply when you receive email at the forward address?

InternetRebel
10-19-2008, 10:45 AM
Thanks for this thread Steinitz, it helped me in actually getting the script to function. All that's left now is to actually capture the content of the mail. :-P

@jonasr. I setup a simple script to mail me the output of phpinfo() when the script was hit and found that the sender's address is in $_SERVER["SENDER"] as well as in $_ENV["SENDER"].

Have yet to figure out where the actual mail is though. :-P

Steve Crooks
02-22-2009, 07:07 PM
Hi folks, this is my first post to the forum.

This thread is the closest I have got to an answer to my problem - the only thing I am missing is a way to get any email message sent to my domain piped to my script. I understand the piping part and where to enter the code in cpanel, but I need a way to get a catchall or wildcard address for my domain. ie *@mydomain.com so that any email such as 1@mydomain.com or 2@mydomain.com will be piped to the script, which is a cgi script as opposed to a php script. My autoresponder script creates an address dynamically for each autoresponder created so that subscribers may subscribe via email.

Any ideas anyone?