Please do following steps to send mail from localhost on Ubuntu/Linux through gmail :-
For that you need to install
msmtp on Linux/Ubuntu server.
Gmail uses https:// (it's hyper text secure) so you need install
~$ sudo apt-get install msmtp ca-certificates
It will take few seconds to install
Now you have to create configuration file(
msmtprc) using , gedit editor.
~$ sudo gedit /etc/msmtprc
Now you have to copy and paste following code in gedit (file you created with above command)
Don't forget to replace MY_GMAIL_ID with your "gmail id" and MY_GMAIL_PASSSWORD with your "gmail password" in above lines of code.
~$ sudo touch /var/log/msmtp.log
You have to make this file readable by anyone with
~$ sudo chmod 0644 /etc/msmtprc
Now Enable sendmail log file as writable with
~$ sudo chmod 0777 /var/log/msmtp.log
Your configuration for gmail's SMTP is now ready. Now send one test email as
~$ echo -e "Subject: Test Mail\r\n\r\nThis is my first test email." |msmtp --debug --from=default -t MY_GMAIL_ID@gmail.com
Please check your Gmail inbox.
For sending mail with php please check https://stackoverflow.com/questions/33969783/phpubuntu-send-email-using-gmail-form-localhost