A problem with the PHP mail function… and alternatives

I hate the php mail() function. I’ve had so much trouble with reliability that I’ve pretty much given up on it as a means of sending mail. I now use the SMTP class SwiftMailer almost exclusively with everything I do… and I never thought or bothered to investigated the range of issues that were problematic using php’s mail() function since Swift is such a far more powerful option anyhow.

A friend was forced to investigate the issue (since his own server doesn’t support SMTP mail), and he discovered that PHP uses the ini directive sendmail_from to set the from email address in the SMTP protocol. If this is not correctly set, or if it does not match the from header in the email headers, the email is held from delivery.

The solution seems to be rectified by setting the directive during execution:

ini_set("sendmail_from", $email_from);
$headers = "From: $email_from";
mail($to, $subject, $message, $headers);

This may or may not work for your own configuration since each server is configured in a cabbage-patch doll style.

In any case, there are a whole range of SMTP options that give you far more control over your mail than is possible or permissible when using the native PHP function. Although I use and recommend Swift Mailer, there are a number of options available if you look for them.

If you’re a WordPress user, there are a number of options that will reconfigure the wp_mail() function to use SMTP instead of mail(), and the various plugins will create options that allows you to specify how mail should be sent. If you’re a Gmail user, you can use Google’s SMTP server to send your mail. I use the WP Mail SMTP plugin and can highly recommend it!

We’ll be posting some examples using Swift soon.

If you liked this article, you may also like:

  1. Short WP New Post Email Notification List
  2. WordPress plugin Evil
  3. PHP Function to Force Links to Open in a New Window
  4. PHP function to generate random password string
  5. PHP function to truncate text (into a preview or excerpt) with trailing dots…
About Marty

is a passionate web developer from Sydney, Australia. He owns about 400 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. You can follow Marty on Twitter or Google+.

Comments

  1. Calum says:

    I’ve had a blog for about a few months and had problems with my mail function but the plugin you linked to works great. Thanks for your help Marty.

Speak Your Mind

*