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.
root=your-full-gmail-address
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587
# Where will the mail seem to come from?
#rewriteDomain=
# The full hostname
hostname=your-full-gmail-address
# 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
FromLineOverride=YES
UseSTARTTLS=YES
AuthUser=your-gmail-username-here
AuthPass=your-gmail-password-here[/cc]
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”]root:username@gmail.com:smtp.gmail.com:587
localusername:username@gmail.com:smtp.gmail.com:587[/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 ( "fran@example.com", "Test from de lappie..." , "Hello World!" ); var_export( $res ); ?>[/cc]
and you’re done!
References (thanks guys!):
- PHP mail() and ssmtp on Debian Linux: http://www.davidhurst.co.uk/2007/06/19/php-mail-and-ssmtp-on-debian-linux/
- Send Mail with Gmail and sSMTP: http://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/
I use Swift mailer library to send mail. I think it is a better option for new applications because it is portable.
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.
Hello,
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.
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.
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
or
– use an alternative SMTP server.
Thanks Francis.
is anyone still using mail()?
hehe
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?!
art
Thanks, really great post!
Angelo from Italy.
Thanks.
I followed that exactly and when I send mail, it is sent from www-data(mygmail addy)…what did I do wrong?
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’:
Hi,
I followed you instructions but cannot get the mail function working.
– Cannot open smtp.gmail.com:587
I am doing it behind a proxy. I am not sure if this causes the problem.
Is there any configuration for proxy?
Thanks,
Hi Jon,
You could try to telnet to the SMTP server:
From a command prompt (in Windows or Linux):
telnet smtp.gmail.com 587
If you see something like
220 mx.google.com 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:
http://windows.microsoft.com/en-US/windows-vista/Telnet-frequently-asked-questions
for more information.)
Nice tutorial…. thanks !!!
I just want to say THANK YOU. I’ve used this microtutorial four times til today. 🙂
Hi,
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
Hi,
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?
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.
thanks a lot.
it’s work!!
Worked great! I just had to make sure to comment out the line:
rewriteDomain=