July 13, 2008

Linux, a second-class citizen in the PHP world

by orzel
Categories: Admin, Gentoo
Tags: ,
Comments: 5 Comments

PHP has the ability to send mails. Great. To do so, it can either connect to an external smtp server, or use a local MTA. Even greater!

But only on windows. As astounding as it sounds, the feature of using a smtp server is only available on windows : on linux, you need to install a full-blown MTA just to be able to send mails from PHP.

Consider the following quite common setup : you have a main vserver, hosting the MTA (postfix in my case), and a dedicated vserver for all those unsecured php-based web stuff. The natural thing to do would be to ask apache/php to send mail through the MTA on the main vserver. This is just not possible, even with the more recent php version.

You can try to use SSMTP or similar “relay only” MTA, but this wont work very well. They don’t relay headers well enough and for example the “From:” header wont go through.

The most surprising thing about it, is that the feature is here, but for some unknown reason, it is not made available under linux. PHP authors decided that linux would be a second-class citizen in the PHP world, and I don’ understand why it is so.

The solution I’ve finally used is to install postfix as a relay only, and NOT starting smptd (you need to comment out the line related to “smtpd” in the file master.cf). It would not be possible anyway, as the smtp port (25) is already opened on the main vserver (vservers share inet ports). The important steps for the configuration (main.cf) are :

  • relayhost = 192.168.0.1 # IP of your main postfix server
  • inet_interfaces = all #wont be used, but needed anyway
  • mydestination = # nothing -> everything is relayed

Once postfix has started, you can check that no port is opened (“netstat -anp | grep postfix”) and that mails actually go through (‘echo foobar | sendmail mymail@myhost.com’).


5 Comments »

  1. Gilles says:

    Hi,

    I see that you found a way to leverage this issue 🙂 ! So a very good subject.

    Anyway, Thank you for this patch.

    Cheers.

    –Gilles

  2. I…. must be confused. I’ve been using PHP with Postfix and sending emails with them since PHP 4.0 under Gentoo Linux.

    Maybe I’m just misunderstanding your issue, but I find it quite easy to send emails out with PHP under Linux using Postfix as the MTA.

    ???

  3. You need a local postfix (through the command line “sendmail”). Under linux you can not use a remote smtp server, while you can with php/windows.

  4. Zeth says:

    I am not a PHP person these days, but a nice simple SMTP for people who hate configuring horrible email things is no brainer SMTP, available in all good Linux distributions as nbsmtp.

  5. As I said, ssmtp could not be used for some strange reason. I’ve tried another one (could not remember the name), but I’ve never heard of nbsmtp, I will definitely try it when I have time. Thanks for the tip!

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA image
*