Archive for the ‘php’ Category

using php mail() with postfix

Monday, April 16th, 2007

First, let me give a short history… I had to make a script at work that would get fired off by our hylafax server when ever an incoming fax was recieved. This script needed to check the caller id info and see what number was dialed, then based on that either move the file into a dir for the department it was intended for, or if it was ment for a person then to convert the tiff file into a pdf then email them the fax. Sounds simple enough… So for what ever reason my boss is hung up on using php for everything so I start working on this thing in php. We are using asterisk at work with IAXmodem to make asterisk and hylafax work happy like.. We have given all 90+ users we have their own DID as well as their own FaxDID. What I do with this script is lookup the number that was passed with callerid info in a mysql database to see whos umber it is, I get their username and their email as well as some other info, then mail the user the fax. I decided to use Pear for this because I like to reuse code when ever possible. I made a simple script to get the job done and damn to hell if it wouldnt work. I would get an email with the headers all screwed up. I knew they were not getting messed up in the php because I could print the email (headers and all) to the command line and it was perfect. I tried this same script on another box (that happened to have sendmail) and it woked perfect. I spent all damn day trying to figure out why my emails were not being sent correctly today. Then along came my good buddy Caleb. The first words from his mouth were ” I bet the working servers are sendmail and the nonworking are postfix” Sure enough that was the case. For what ever reason postfix is adding 2 new lines to my stuff. The fix was really extremely trivial.

$body = $mime->get();
$body = str_replace(“\r”,”,$body);
$headers = $mime->headers($headers);
$headers = str_replace(“\r”,”,$headers);

The full script can be found here. This script is not the best work ever, and it could be improved for sure. But this is what I am using. I hope someone can find it useful someday.