Install ssmtp
sudo apt-get install ssmtp
Configure ssmtp
sudo nano /etc/ssmtp/ssmtp.conf
-----------------------------------------------------------------------------
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=your-full-gmail-address
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587
# Where will the mail seem to come from?
#rewriteDomain=
# The full hostname
hostname=your-full-gmail-address
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
UseSTARTTLS=YES
AuthUser=your-gmail-username-here
AuthPass=your-gmail-password-here
---------------------------------------------------
-----------------------------------------------------------------------------
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=your-full-gmail-address
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587
# Where will the mail seem to come from?
#rewriteDomain=
# The full hostname
hostname=your-full-gmail-address
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
UseSTARTTLS=YES
AuthUser=your-gmail-username-here
AuthPass=your-gmail-password-here
---------------------------------------------------
Setup your users
sudo nano /etc/ssmtp/revaliases
root:username@gmail.com:smtp.gmail.com:587
localusername:username@gmail.com:smtp.gmail.com:587
localusername:username@gmail.com:smtp.gmail.com:587
Configure PHP
sudo nano /etc/php5/apache2/php.ini
------- sendmail_path = /usr/sbin/ssmtp -t
sudo service apache2 restart
Send a mail
<?php
$to = "********@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
mail($to,$subject,$message,"From: $from");
echo "The email has been sent.";
?>