First, install ssmtp package:

sudo apt-get install ssmtp

Remove any old sendmail packages floating around that you won’t need:

sudo apt-get autoremove

Edit the ssmtp config.

sudo nano /etc/ssmtp/ssmtp.conf

Set the following variables in the file:

root=webmaster@domain

mailhub=mail.server:465

hostname=web.server

FromLineOverride=YES

AuthUser=youremail@mail.server
AuthPass=yourpassword
UseSTARTTLS=yes
UseTLS=yes

 

Note: You may wish to make UseSTARTTLS commented out if you have issues.

Modify your php.ini file.

sudo nano /etc/php5/apache2/php.ini

Set the variable in the file as below:

sendmail_path = /usr/sbin/ssmtp -t

Restart the server.

sudo reboot

Create a php test page.

<?php
$to      = 'youremail@address.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers))
{
   echo("<p>Email successfully sent!</p>");
  } else {
   echo("<p>Email delivery failed.</p>");
  }
 ?>

Sources: