PHP+Ubuntu Send email using gmail form localhost

Mangesh Sathe picture Mangesh Sathe · Nov 28, 2015 · Viewed 11.2k times · Source

I have searched several posts on this but no luck. Everyone is using postfix. But when I gone through the text on https://help.ubuntu.com/community/Postfix

What is a Mail Transfer Agent In other words, it's a mail server not a mail client like Thunderbird, Evolution, Outlook, Eudora, or a web-based email service like Yahoo, GMail, Hotmail, Earthlink, Comcast, SBCGlobal.net, ATT.net etc.... If you worked for a company named Acme and owned acme.com, you could provide your employees with email addresses @acme.com. Employees could send and receive email through your computer, but not without your computer running all the time. If all your email addresses are at a domain (@gmail.com, @yahoo.com) you do not own (you don't own Google) or do not host (acme.com) then you do not need this at all.

As the last line says You cannot us it for gmail or yahoo to make it work from localhost..!

Can anyone tell me how can I configure mail server on localhost using gmail SMTP? I am using Ubuntu 14.

Links I have tried before NONE of them worked for me. No error or warnings during testing of below listed links

https://askubuntu.com/questions/314664/sending-php-mail-from-localhost https://askubuntu.com/questions/228938/how-can-i-configure-postfix-to-send-all-email-through-my-gmail-account https://easyengine.io/tutorials/linux/ubuntu-postfix-gmail-smtp/ https://easyengine.io/tutorials/mail/postfix-debugging/

Answer

Vikas Dwivedi picture Vikas Dwivedi · Jul 16, 2017

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 ca-certificates

~$ sudo apt-get install msmtp ca-certificates

It will take few seconds to install msmtp package.

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)

defaults
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default
host smtp.gmail.com
port 587
auth on
user [email protected]
password MY_GMAIL_PASSSWORD
from [email protected]
logfile /var/log/msmtp.log

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.

Now create msmtp.log as

~$ 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 [email protected]

Please check your Gmail inbox.


Now if you want to send email with php from localhost please follow below instructions:-

Open and edit php.ini file

~$ sudo gedit /etc/php/7.0/apache2/php.ini

You have to set sendmail_path in your php.ini file.

Check your SMTP path with

~$ which msmtp 

and you will get /usr/bin/msmtp like that.

Search sendmail_path in php.ini and edit as below

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = /usr/bin/msmtp -t

Please check 3rd line carefully there is no semicolon before sendmail_path.

Now save and exit from gedit. Now it's time to restart your apache

~$ sudo /etc/init.d/apache2 restart

Now create one php file with mail function from http://in2.php.net/manual/en/function.mail.php.

Do test and enjoy !!