Sending email from Drupal more reliably
Sending email from Drupal more reliably
Posted on October 02, 2014 by Bob Kruse
A common issue I see is difficulty sending email from within Drupal. Most of the time, the problem has nothing to do with Drupal at all. Server and DNS misconfigurations are usually to blame.
Here are some basic things to look into when you want to send email reliably.
Setup website hosting properly when you have a 3rd party email server
Most of my medium/larger clients host email separately from their website hosting server. This is preferred because if your website hosting goes down then your email can continue running. Keeping web hosting and email bundled together is an old school train of thought from when websites and email first started getting popular in the mid-1990’s.
Letting your web design company be responsible for your email is also an old school train of thought. Email is better handled by an IT person or company that maintains the client’s computers, networking, and individual email clients. This shouldn’t be in the job scope of a web designer. Sorry for the digression.
So getting back to hosting, when a client has a 3rd party email server, a lot of times the web server is not configured correctly to know about it. A web server natively expects to find an email address of a same domain name as the website domain name it is hosting… unless you tell it to look somewhere else for email.
When configuring a web host for a client that has a separate email server, you want to make sure that the web host’s DNS records contain properly set MX records for their email server. That’s the secret most people miss and why they are scratching their heads when an email looks like it sent, but never gets received.
If you use Cpanel, under Edit MX Entry, you can also select Remote Mail Exchanger, which will look back to your Domain Registrar’s DNS (if you are using hosted DNS) in order to grab the proper MX information.
Use SMTP instead of PHP mail()
The good old fashioned PHP mail() command is used by default in Drupal. Mail() sends "down and dirty" emails without a care for who is sending it and who the email says is sending it. It’s commonly used for sending/spoofing spam emails. When you’re on a shared host, a server’s mail queue can easily get clogged up with unsent messages if too many are being sent at one time. This results in your email never leaving the server or leaving after a long delay.
If PHP mail does get sent out, you will most commonly find it in the recipients spam folder because it usually fails sender authentication tests, like reverse DNS lookups, SPF records, DKIM, etc.
The work around? Use SMTP. Similar to how Outlook connects to a POP account to send and receive emails, using SMTP allows your website to authenticate itself to your mail server and send the email through your mail server. This results in a real email being sent from the actual legitimate mail server representing your domain name. It’s less likely to go to someone’s spam and more likely to get sent quickly and successfully.
In Drupal you can get SMTP setup in two basic ways:
- The SMTP module (https://www.drupal.org/project/smtp)
- The PHPMailer module (https://www.drupal.org/project/phpmailer)
Both modules rely on the same inner workings, a special PHP package called PHP Mailer (http://phpmailer.worxware.com/index.php?pg=phpmailer) which is a library of code that make PHP to SMTP connections possible.
The SMTP module has more installs and is easier to install. PHP Mailer requires you to download the phpmailer library files as an install step, while SMTP includes it with the module. The features are fairly similar.
Simply install either module, fill in the SMTP connection setting page, and off you go.
SMTP Configuration screen
PHP Mailer Configuration screen
As an additional step you should also include the Mailsystem module (https://www.drupal.org/project/mailsystem), just to ensure that ALL of Drupal’s emails use SMTP rather than the default PHP mail(). I have seen some emails from Drupal’s registration and password resets fall through the cracks, so adding Mailsystem bullet-proofs it.
Within Mail System, simply set the Site-wide default to use the chosen SMTP module and you’re good to go.
What if you don’t have access to an SMTP server?
If you don’t have access to a client’s SMTP server, there are numerous services that offer 3rd party SMTP you can subscribe to. If you will be sending less than 12,000 emails per month then you can signup for a free Mandrill account (https://mandrillapp.com). Mandrill is a service provided by the folks at MailChimp. It offers tracking, stats, the works. I use it for a lot of my client’s websites and it’s great… and free. Even a client that sends out weekly email blast to 500 users with Simple News never comes close to hitting the 12K limit.
Now that you’re empowered with more email smarts. Get SMTP, get Mandrill, and bullet-proof your Drupal emails. Happy sending.