Tuesday, April 28, 2009

email piping with PHP and cPanel

I had a lot of trouble piping emails to a PHP script today. There was a real scarcity out there in terms of information (or I just didn't know how to search for the right terms). So, I thought I'd post about my fun in case someone else got stuck. (By the way, I use cPanel 11 and started with this script.)

What I wanted to do was set up an email address that would pipe to a PHP script. That is, my PHP script would get a copy of the emails. Then the PHP script could parse the file and do whatever; make a database entry or something similar.

I ran into two problems.

First, I would get a return email that said "Local delivery failed." It would have this at the top of the email:
pipe to |/home/accountname/public_html/emailin.php
generated by msg@accountname.com
local delivery failed

I found some website somewhere that suggested changing the file permissions for the PHP script (emailin.php in this case). So that is what I did, and it seemed to help. I used the cPanel File Manager to change the permissions to 754.

That fixed this problem.

The second problem was that despite receiving the email at the script and doing the appropriate things with it, I would still get an error email back. So, even though it all worked, my email would bounce back saying:
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

pipe to |/home/accountname/public_html/emailin.php
generated by msg@accountname.com

The following text was generated during the delivery attempt:

------ pipe to |/home/accountname/public_html/emailin.php
generated by msg@accountname.com ------

Again, after a few hours of searching I found someone that suggested changing the hash bang at the top of the script file to this: #!/usr/bin/php -q

That dash q made all the difference.

This was my adventure.


Shatra said...

Thanks SO much, I was having exactly the same problem.

David said...

Thanks for being detailed and to the point.

For anyone's future reference, the '-q' flag suppress http header output.

Eric said...

Glad it helps! Thanks for the info, David.

Amanda Blain said...

dead old post.. but omg THANK YOU .. I had both these exact problems in my piping script today... thank you for updating it so 3 years later you would save me countless hours of work. <3

Eric said...

Old, new, who cares!? If it helps, it helps. :)