PHP mail() with Ubuntu Desktop and Gmail

Recently I was adapting a newsletter plugin for WordPress and needed the PHP mail() function for testing. However an Ubuntu desktop install is missing Sendmail – the MTA that PHP expects to find on a Linux PC.

I use a local Apache/MySQL server on a laptop to do a lot of my development – I don’t need a full mail server just to send mail.

Also SMTP servers on dynamically assigned IP addresses are so untrusted these days that you can be pretty much guaranteed that a decent spam filter will reject your email based on a RBL lookup. By using Google Mail’s authenticated SMTP service you bypass this restriction.

The lightweight solution is ssmtp.

1: Install ssmtp

[ccN lang=”bash”]sudo apt-get install ssmtp[/cc]

Check where the binary and the ssmtp.conf file ended up. You’ll need to know the locations for the following steps:

[cc lang=”bash”]francis@francis-laptop:~$ whereis ssmtp
ssmtp: /usr/sbin/ssmtp /etc/ssmtp /usr/share/man/man8/ssmtp.8.gz[/cc]

2. Configure ssmtp

Edit /etc/ssmtp/ssmtp.conf

[ccN lang=”bash”]#
# Config file for sSMTP sendmail
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named

# Where will the mail seem to come from?

# The full hostname

# 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


3. Setup your users

Edit /etc/ssmtp/revaliases

You’ll probably want to set up your local user and root for sedning mail:

[cc lang=”bash”][/cc]

4. Configure PHP

Edit /etc/php5/apache2/php.ini

(This is correct for Ubuntu 10.04. You can always check which php.ini file Apache is using with the PHP function call phpinfo( INFO_GENERAL ).)

Find the sendmail configuration line:

[ccN lang=”bash”]sudo grep -ni sendmail_path /etc/php5/apache2/php.ini

1047:;sendmail_path =[/cc]

and change to

[ccN lang=”bash”]sendmail_path = /usr/sbin/ssmtp -t[/cc]

Restart Apache

[cc lang=”bash”]sudo service apache2 restart[/cc]

(Check for other distributions. If you’re completely lost, a reboot will do it!)

5. Send a mail

[ccN lang=”php”]< ?php $res = mail ( "", "Test from de lappie..." , "Hello World!" ); var_export( $res ); ?>[/cc]

and you’re done!

References (thanks guys!):

  • PHP mail() and ssmtp on Debian Linux:
  • Send Mail with Gmail and sSMTP:
Tagged with: , ,
Posted in Development, Tech Blog
20 comments on “PHP mail() with Ubuntu Desktop and Gmail
  1. phpuser says:

    I use Swift mailer library to send mail. I think it is a better option for new applications because it is portable.

  2. Francis says:

    I agree – if I was developing a mail function in a PHP app I would use something like Swift mailer – it is a very nice piece of kit.

    In this case though I was working with someone else’s code and needed the mail() function.

    Thanks for the comment.

  3. Dennis says:


    I think people should know that Google has very low limits about the number of emails you can send via your GMail account – on general, you won’t be able to send more than 500 emails a day. So while this is perfect option for low volume mailing, if you have a large number of registered users and want to send a newsletter to them, this won’t work.

  4. Vivek says:

    First i thanks to you for this help.

    Now my mail function is working fine and sending the mail. But one problem is occur, in From : address my email is sending not that one which is i mentioned in mail(). Even i had done the setting in ssmtp.cnf file.

    Can u please help me to solve this issue.

    Thanks once again and waiting ur reply.

  5. Francis says:

    Google are the culprits here – they will not let you use an arbitrary “From” address (for very good reasons).
    However as Dennis points out this solution is not for production use.
    To solve your problem either:
    – verify the “from” address in your GMail account
    – use an alternative SMTP server.

  6. Vivek says:

    Thanks Francis.

  7. is anyone still using mail()?


    zend mail rules ….. probably there are other libs but zf does all i need including php-level smtp delivery so local smtp is not even needed 😛

    how cool is that?!


  8. Angelo says:

    Thanks, really great post!

    Angelo from Italy.

  9. ZendManiac says:


  10. Ed says:

    I followed that exactly and when I send mail, it is sent from www-data(mygmail addy)…what did I do wrong?

  11. Francis says:

    Hi Ed,

    You set the ‘from’ address when you send the email. For example PHP’s mail() function allows you to pass the ‘from’ address.

    You should also check ‘/etc/ssmtp/ssmtp.conf’:

    # 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

  12. Jon says:


    I followed you instructions but cannot get the mail function working.
    – Cannot open

    I am doing it behind a proxy. I am not sure if this causes the problem.
    Is there any configuration for proxy?


  13. Francis says:

    Hi Jon,

    You could try to telnet to the SMTP server:

    From a command prompt (in Windows or Linux):
    telnet 587

    If you see something like
    220 ESMTP fo2sm32590wbb.48
    You are connected.

    If you get a failure message it is likely that a firewall is blocking you.

    (Since Windows Vista telnet is not installed by default. See:
    for more information.)

  14. junkiest says:

    Nice tutorial…. thanks !!!

  15. Fran says:

    I just want to say THANK YOU. I’ve used this microtutorial four times til today. 🙂

  16. Mahesh.D says:


    Thanks for the awesome document.It is very useful.After searching long in net,I found this document,at last I fixed the problem with help of this document

  17. Visitha B says:


    That is very simple and useful tutorial.. It works perfectly for me. Before I used sendmail and postfix. From both of them it returned true from mail() function. But there was no any mail in my inbox or spam. Then I tried with ssmtp as you explained and it worked well. Thank you very much for your post.

    Can you guess why I may failed with sendmail and postfix?

  18. Francis says:

    I could only really guess, but once mail() returns TRUE it is up to the server (Sendmail in your case) to deliver the email. All I can suggest is that you check the Sendmail and Postfix logs.

  19. ted says:

    thanks a lot.
    it’s work!!

  20. Josh says:

    Worked great! I just had to make sure to comment out the line:


Leave a Reply

Your email address will not be published.