简体   繁体   中英

Email piping with php script

Hi' I want to forward all the emails(which are come to my inbox) to php script and retrieve email content and save it in a file. So do that I was add email forwarder with piping path correctly.

Address to Forward :tickets@ana.stage.centuryware.org

Pipe to a Program : /home/centuryw/public_html/stage/ana/osticket/upload/api/pipe.php

I have used following script as pipe.php

#!/usr/bin/php –q
<?
/* Read the message from STDIN */
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw = fopen("mail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
/* Script End */

But there was no output file and all email are bounced to my inbox again. Can anyone help me please?

确保PHP文件设置了执行位(即chmod +x pipe.php )。

I know this question is 2 years old but hopefully this will help anybody who stumbles across it as I did.

I had exactly the same issue and I spent ages trying to log errors etc. I then noticed that my script (and this one) had PHP short tags (ie <? ) and my PHP config file had these turned off. I changed the tag to <?php and the script worked! Obvious but easy to miss...

try the following 2 options to check:

  • First, your php file must need to have execute permission(from owner group at least), otherwise it won't work.

  • What did you used when you were using forwarder? You need to mention php compiler path at the beginnig also.

I have recently written an article regarding the full detail process of piping email content to php program , you will may like to have a look. Let me know if you have any more question. Thanks.

I encountered the same problem. However I solved it by specifying the output file with full path name. Instead of just 'mail.text', i entered '/home/username/public_html/mail.txt'.

Chanaka -

This doesn't address why there is no output file, but...

Don't you want to use a+ in your fopen() call? The w+ argument will delete any content that already exists in your output file.

PS: Have you tried doing a simple test which writes to the output file using dummy text (not the input from an e-mail) as the contents?

Jeff Cohan

If this is actually an email box, why not use IMAP (PHP) ? There are lots of classes to read the mail with imap @ phpclasses.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM