How to enable PHP mail function on Ubuntu

I personally use very simple and lightweight package ssmtp. It will use any smtp server to send outbound emails. Install it by running:

sudo apt-get install ssmtp

Then edit /etc/ssmtp/ssmtp.conf file, comment out existing mailhub line and add the following lines (this example is for gmail smtp server):

(Provide your gmail username & password. Of course you can use any other SMTP server).

Now make sure that your php.ini has correct sendmail_path . It should read as:

sendmail_path = /usr/sbin/sendmail -t

(You do not have to specify ssmtp here because the installation should have created a symlink for /usr/sbin/sendmail , which correctly points to ssmtp . Otherwise you have to specify sendmail_path = /usr/sbin/ssmtp -t )

Also make sure your server has port 25 UDP open (default port for SMTP. might need to contact your hosting for this)

Reload apache and your php should be able to send outgoing emails now.

If you are getting error " Could not execute: /usr/sbin/sendmail "
means you don’t have sendmail installed on the server. On a debian based Linux distribution use

sudo apt-get install sendmail 

with CentOS or Redhat use

sudo yum install sendmail

If that still doesn’t work then check the permissions of /usr/sbin/sendmail.