Sendmail alternative – postfix on ubuntu (when PHP doesn’t send emails) in a few easy steps
Imagine you have your new shiny Ubuntu server. You installed your php, apache & mysql and everything seems fine until… you realised your system doesn’t send emails.
If there is a problem – there must be a solution! In our case it’s either install good (… not so in fact) sendmail or postfix.
We decided to install postifix:
> sudo apt-get install postfix
Pick the correct option in a nice interface (for the web site it’s normally Internet)
Now it’s all “almost” working ;)You also have to go and change php.ini to pick your new mail sending program.
* Please note: since I am using Apache MPM_Worker on my local server – my PHP is working as a FastCGI module. So, my php.ini is located here: /etc/php5/cgi/php.ini
on a default installation of Apache it’s most likely here: /etc/php5/apache2/php.ini
Check where is your new “sendmail” is located:
> which sendmail
/usr/sbin/sendmail
Good! Now change php.ini – find following string in it:
"; http://php.net/sendmail-path"
and add / edit following line below (don’t forget to replace email from field – just in case):
sendmail_path = /usr/sbin/sendmail -t -i -f name_from@your_server_name.com
Restart Apache:
> sudo service apache2 restart
PS: more info can be found here: https://help.ubuntu.com/12.04/serverguide/postfix.html
One useful hint – To configure postfix, run the following command:
sudo dpkg-reconfigure postfix
To test if it’s working ok:
> apt-get install mailutils
echo 'this is a test mesage body' | mail -s test_email_header username@gmail.com
And test in PHP document on the server:
<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
if (mail('caffeinated@example.com', 'My Subject', $message)){
echo "ALL GOOD";
}else{
echo "Sending Failed";
}
?>
THE END
In: Fighting the system, Linux, Ubuntu · Tagged with: install, linux, php, postfix, ubuntu
on December 12, 2013 at 18:58
Permalink
Hi There! I really appreciate your help but I have a ask: What to put on postfix config?
Thanks!
on December 12, 2013 at 21:09
Permalink
I don’t normally change anything. Works like this – no problems.