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
# 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 mail.domain.com
# 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
You’ll probably want to set up your local user and root for sedning mail:
4. Configure PHP
(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
and change to
[ccN lang=”bash”]sendmail_path = /usr/sbin/ssmtp -t[/cc]
[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 ( "email@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/