Absolutely Tech

[HOWTO] Send emails(using mail function) from localhost in PHP through msmtp (using gmail account) on Linux


There’ve been lots of times when I wanted to use the mail() function on my local server. I am sure lots of you would’ve been wanting it too but most of you would’ve settled for PHPMailer or just used a web host to test the code instead. I myself had been doing the same until recently when I finally decided to do some research and get it to work.

Here are the steps in short for the geeks who like to do things on their own:
All I did was used smtp client called msmtp, configured it to work with my gmail account and configured PHP to use msmtp to send emails.

This tutorial is only applicable for linux users. I’ll write another article for windows users soon when I get my hands on a windows box.
All the commands used in the instructions are for Ubuntu, however you may use corresponding commands for your distro (for eg; you can use yum install instead of apt-get install on fedora, redhat, centos.

Step by step instructions:

  1. First of all, follow this tutorial:
    Install msmtp on your linux box and configure it to work with Gmail.

    Msmtp is highly configurable and you can easily configure it to work with any smtp server. If you wish to use any other service with msmtp, you may read its manual and configure it.
    Proceed to next step only if you have successfully configured and are able to send a test message.

  2. Open php.ini in your text editor.
    sudo gedit /etc/php5/apache2/php.ini

    Search for ‘sendmail_path’ and change it to look like

    sendmail_path = '/usr/bin/msmtp -t'

    Save the file and exit the text editor.
    Please note that your msmtp path may vary if you are not using Ubuntu. You may find the path to executable by:

    which msmtp
  3. Restart apache:

    sudo /etc/init.d/apache2 restart

    OR

    sudo /opt/lampp/lampp restart
  4. Everything’s done. Lets test if the mail() function is working now:
    if ( mail ( '[email protected]', 'Test mail from localhost', 'Working Fine.' ) );
    echo 'Mail sent';
    else
    echo 'Error. Please check error log.';

    Replace the [email protected] with your own username, save it into a php file along with php delimeters in your virtual host root folder and execute it through the browser. You should receive a mail.

Give yourself a pat on the back.

Some points to note:

  • If you weren’t able to send yourself a test mail in Step 1 ie. setting up msmtp, you should look at the debug information and search for solutions accordingly on the internet. Here’s the manpage of msmtp to help you with configuration
  • If you didn’t receive a mail in your inbox on the last step, check in your Spam folder. If its not there your should check the php error log for hint about what went wrong and act accordingly.
  • For any other questions, suggestions or appreciation feel free to use the comment box.

Cheers!

Category: How-To, Technology, Ubuntu (Linux), Web development

Tagged: , , , , , , , ,

  • http://www.erasedmail.com/?p=2408 [HOWTO] Send emails from localhost in PHP using msmtp (using gmail … | www.erasedmail.com

    [...] post: [HOWTO] Send emails from localhost in PHP using msmtp (using gmail … from-localhost, php, [...]

  • http://gmail.latnet.biz/?p=7296 [HOWTO] Send emails from localhost in PHP using msmtp (using gmail … | Gmail HOT news

    [...] the rest here: [HOWTO] Send emails from localhost in PHP using msmtp (using gmail … Tags: a-few-hours-, and-deleted, ebusiness, enhanced, from-localhost, internet, linux, php, [...]

  • http://www.gestaopublica.sp.gov.br/ Ramon Leonardi

    I expect anxiously a windows version for this [how to]. I’m not a pro and I’ m having a hard time trying to make my local machine send mail trough google SMTP. :D

  • http://www.gestaopublica.sp.gov.br/ Ramon Leonardi

    I expect anxiously a windows version for this [how to]. I’m not a pro and I’ m having a hard time trying to make my local machine send mail trough google SMTP. :D

  • http://xobb.citylance.biz/2010/11/%d1%88%d0%bb%d0%b5%d0%bc-%d0%bc%d0%b8%d0%bb%d0%be-%d0%b7-%d0%bb%d0%be%d0%ba%d0%b0%d0%bb%d1%85%d0%be%d1%81%d1%82%d0%b0-%d1%87%d0%b5%d1%80%d0%b5%d0%b7-gmail/ Разноє

    Шлем мило з локалхоста через Gmail…

    Прийшла до мене нарешті думка навчити свою робочу конячку відправляти письма мені. Заглушки всякі до sendmail не дуже хочеться писати, а приймати би поштовим клієнтом було би саме кошерніше. Так вот, будемо відправляти всі письма з нашої адреси, яку со…

  • http://www.4future.ir amir

    very very tnx , this work for me (ubuntu 10.10)
    I’ve read a lot blog and forum for this but that don’t work

    only for newbie as me , after change php.ini should restart lampp
    /opt/lampp/lampp restart

    and this link is corrupted “Install msmtp on your linux box and configure it to work with Gmail”
    true link is this: http://goo.gl/nyokj

  • http://www.absolutelytech.com Deepak Mittal

    Thanks for pointing out the wrong link, I’ve fixed it. And you’re welcome :)

  • Romo

    It really works :)

  • http://www.okadadesign.no/blog/web-development/sending-email-from-localhost-using-msmtp-with-gmail/ Sending email from localhost using MSMTP with gmail | Okada Design Blog

    [...] Credit goes to this and this article. [...]

  • G_akyoo

    I have done everything but, I can’t send enail from localhost
    This does not work to me

    
    if ( mail ( '[email protected]', 'Test mail from localhost', 'Working Fine.' ) )
    	echo 'Mail sent';
    	else
    	    echo 'Error. Please check error log.';
    
  • mike

    Thanks for the great how to. Worked smoothly for me on ubuntu 10.10.

  • Thomas Le Feuvre

    is it possible to use a hotmail account instead?

  • http://www.absolutelytech.com Deepak Mittal

    Use this configuration for msmtp:defaultstls ontls_starttls ontls_trust_file /etc/ssl/certs/ca-certificates.crt
    account defaulthost smtp.live.comport 587auth onuser [email protected] hotmail_passwordfrom username .comlogfile /var/log/msmtp.log

    Hope this helps :)

  • Priyamittal06

    hi deeps your posts are knowledgeable I am in loving it..

Ubuntu 11.04

The next version of Ubuntu is coming soon

Follow me

Sponsors