Setup email piping with PHP

software development



I am trying to setup email piping. My idea is to make a simple piping script where emails are sent and read by a PHP script. I have created a new user, a new subdomain and added a catch-all address. After that I have written the following PHP script:

//piping script
#!/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);


/* Saves the data into a file /
$fdw = fopen(“mails.txt”, “w+”);
fwrite($fdw, $email);
/* Script End

However, when I run this script it throws:

[Mon May 28 16:24:20 2012] [error] [client] Premature end of script headers: piper.php

Can you please guide me on how to setup a simple email piping script on dreamhost?

Also some of the mail just bounce back. Strange! Also I tried setting a .forward file to the PHP script. It didnt work. And ‘new’ in ‘Maildir’ is empty. Please help.


Email is in a different server than your webserver, if you haven’t already you must visit message filters in the panel and create a filter that will forward the email to your webserver.

That said, I didn’t review the code you submitted.


Well, the web server software, Apache, doesn’t handle email messages. It handles HTTP requests and responses. The error message is letting you know the script output is not proper for generating an HTTP response.

Doesn’t sound like a good idea to me. The machines are for hosting web sites, and if you process the mail through PHP you’re going to bog them down depending on the volume of mail you receive.


I have already created a filter that forwards mails to a shell account. The forwarding part is working fine. I have also set a .forward directive that pipes the email to the PHP script.

Also the mail and the webserver are not on different servers. Its a subdomain of my main domain and I am running the PHP script under the same shell user as well.

In reply to Atropos7, it won’t have a huge number of users. Its actually for my project management system. I have around 4-5 clients at an instant of time so at max it will be 20-30 emails a day which, I believe, is negligible. I confirmed this with DH support and they are fine with it.

Can somebody please guide me toward the right direction?


Nope, they are on different servers. There is one set of machines that handle mail, and another set of machines that handle web - that is how DreamHost has things setup.

I’m glad they are fine with it - the issue is with volume of mail (not users), and if you are using a catch-all, you’re bound to get spam at all sorts of made-up addresses.

Have you seen the Wiki yet?


I didn’t go with piping from a catch all address since according to most in the forum its not a very good idea. I just created one email address and wrote a script that can fetch unread mails via IMAP since the whole piping concept was really confusing me. Its working fine now. I had to write a lot of extra code and change my workflow to implement all the features that I require but in the end it was worth it. Thanks to all for helping me out.