July 13, 2008
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 firstname.lastname@example.org’).