mail() without sendmail on linux – a hack

Some time ago, my friend Amy Hoy was working an environment where a higher power disallowed them from installing a mail server on their machine – yet they were using 3rd party software that required the mail() function be available – unless much hacking was to be done.

So, I came up with a solution, thanks to Sara Golemon this will work nicely.

First, PHP should not compile with a mail() function if sendmail is not detected, however if you have a sendmail (or compatible) binary on your system, as most *nix’s do, this will invariably fail. The first step is to disable it during compile, otherwise we cannot override it:

  1. Run ./configure as normal
  2. Edit main/php_config.h
  3. find the line that reads ‘#define HAVE_SENDMAIL 1‘ and comment it out using /* and */
  4. save and exit
  5. continue compilation as normal (i.e. make && make install)

Next, we need to define our own mail() function that uses an external mail server, I would suggest using the PEAR::Mail package or some such, but I’m leaving this as a user excercise because its 7:13am – save this in a file accessible from all hosts

Finally, use the php.ini setting auto_prepend_file to include this file in all PHP scripts, and they will transparently be able to use you mail() function :-)

Hope this helps someone.

– Davey