Google SMTP is useful if the IP address of your vps server is blacklisted and your mails go to spam.
You can check status of your VPS IP Address from here.
Step 1 : Install msmtp package
apt-get update
apt-get install msmtp-mta ca-certificates
update-ca-certificates
Step 2 : Fill the config file of msmtp
Edit /etc/msmtprc file :
nano /etc/msmtprc
Copy and paste the following lines into it :
account default
Hit Ctrl+X to exit nano and save it.
host smtp.gmail.com
port 587
timeout 30
auth on
user ACCOUNT@YOURDOMAIN.COM
password PASSWORD
auto_from on
maildomain YOURDOMAIN.COM
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
Step 3 : Alter the php.ini file to use msmtp
Edit php.ini file. php.ini file is placed in /etc/php5/apache2.ini or /etc/php4/apache2/php.ini.
You can find the place of php.ini from phpinfo(); of your server.
nano /etc/php5/apache2/php.ini
Find ( using Ctrl+W) around line 560 :
sendmail_path = "/usr/sbin/sendmail -t -i "
and change it to :
sendmail_path = "/usr/bin/msmtp -t -i "
Save and close the file with Ctrl+X
Step 4 : Restart Webserver
/etc/init.d/apache2 restart
Test it
You can see if it is working or not with this php script :
<?php
$to = "myemail@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "admin@mydomain.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>