When PHP’s mail function doesn’t work

mail-07[1]A lot of VPS’s do not have mail functionality right out of  the box, so if you install a stack like LAMP or maybe a WordPress install script (like you find on Linode or Digital Ocean) , and then try to send mail via your website, you will most likely get an error.

For example if your using Contact Form 7 in WordPress you will get “Failed to send your message”, not a very meaningful error.  So the first step is to troubleshoot the problem, login via SSH and type:

netstat -tulpn

This should show your servers open ports, there should be a mail program in there with a mail port open to 25 and/or port 465 (or 587) for SSL. If not then you will not be able to send any mail via PHP, unless you have configured a different port or remote service, all of which can be verified.

You can further test PHP’s mail by sending a test email using  PHP’s command line or putting the following into a PHP file.

mail ( 'your@email.com', "Test mail", "Test mail from your server name" );

Now test your server emails  on the command line by entering  (replace the email with your own):

 echo "This is a test." | mail -s Testing someone@example.com 

After doing both of the above you can verify if the problem lies within your servers mail config or with PHP’s config. Meaning if the server test worked but PHP test did not, you know the problem most likely resides within php.ini or your Framework / CMS.

Options if you get no mail for both tests:

Install a mail program on your OS.  Most Linux distributions use “SendMail” but this is a large and somewhat complex piece of software (though it is very good), in most cases I just want a site to send mail and not receive any. For that reason I install Exim4 . For Ubuntu:

sudo apt-get install exim4-daemon-light mailutils
sudo dpkg-reconfigure exim4-config

You will then have GUI to go through, for a better guide on how to choose these options:  https://library.linode.com/email/exim/send-only-mta-ubuntu-12.04-precise-pangolin

Options if you get mail via the server but not for PHP:

  • Look in your php.ini file and make sure the sendmail_path and configuration to sendmail is correct.
  • Check if the default sendmail user (usually nobody@example.com) is enabled.
  • Install http://swiftmailer.org/ to debug email response code and configure options (better than creating it from scratch)
  • Configure PHP mail to use a 3rd party SMTP server

smtp

WordPress specific links:

http://wordpress.org/plugins/check-email/
http://wordpress.org/plugins/search.php?q=smtp

Advertisements

One thought on “When PHP’s mail function doesn’t work

  1. Thanks for introducing the Check Email plugin, very helpful.I used to create a custom PHP file to test everything, but that requires upload file to host but that’s time consuming.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s