Send mail with Drupal 7: Deliver email reliably & avoid the spam folder
Sending emails with Drupal 7 is meant to work right out of the box, but that's not always the case. Depending on your environment (sendmail, anyone?) you could encounter some significant obstacles. And even if you're all setup to send off emails without any additional configuration, ensuring those emails are reliably delivered can be a different story.
Over the past couple of years I've built plenty of sites that struggle with their emails being delivered into recipients' spam folders -- an issue that can be very frustrating for clients looking to register new accounts, send notifications, or assist users in resetting their passwords. I'm often surprised at how quickly a couple of missed notifications turn into frustrated messages in my inbox tagged with [Website Feedback].
Guaranteeing your Drupal emails stay out of the spam folder can be a complex subject, but there are two quick steps that will get you most of the way there in fifteen minutes. Here's the big two: ensure your emails are TLS encrypted and send your emails with a trusted external service.
In this post, I'll walk through configuring your Drupal site to send TLS encrypted mail with SendGrid -- helping to keep those emails out of your users' spam folders.
Highly deliverable email with SendGrid
I've run into two major problems with Drupal mail in the past. First, unencrypted emails (sent using the Drupal default PHP mail library) can easily find themselves in a spam folder or marked as suspicious simply for a lack of encryption. Second, configuring mail to be sent via SMTP by your hosting provider or registrar can have unexpected results. In some cases (especially shared servers) the IPs you end up sending mail from are blacklisted with specific email clients due to past infractions. In my experience, this problem manifests most frequently with emails addressed to Hotmail, MSN or Yahoo clients being dropped altogether -- before they even have a chance to reach the end users' spam folder.
Both of these issues can be addressed by sending TLS encrypted mails through SendGrid.
1. Open a SendGrid account
SendGrid is an easy to use email service that makes delivering encrypted emails a snap. There are probably plenty of other reliable services out there, but I find SendGrid's user experience to be pretty intuitive and their products are very affordable. It's FREE to about ~12,000 emails monthly, and brackets beyond the free tier start at just $1 / month.
Head over to SendGrid and find their Try for Free option. You'll be prompted for a username, email address and password. Once you've supplied your basic credentials, you'll be asked for some additional information like your name, phone number, organization and a physical address. SendGrid uses this information to assist in identifying you as a legitimate email sender -- not a spammer. After entering all of your information, you'll need to wait a couple of minutes while your account is provisioned. You'll receive an email shortly letting you know this processs is complete.
Once you've received the email notifying you that your SendGrid account has been provisioned, you're ready to setup credentials. It's possible you'll need to log out of SendGrid and log back in before the interface recognizes that you've been provisioned (I had to). Once you've logged back in, navigate to Settings, then Credentials, then click on Add New Credential. Adding multiple credentials allows you to track and manage emails for multiple web properties.
Your property specific credentials will need to be different than your account username & password -- these credentials are used to identify individual senders (websites) rather than your account at large. When creating credentials, make sure you check the UI / API and Mail options, then go ahead and Create Credential. And you're set! We're done with SendGrid and on to Drupal configuration.
Trouble getting setup? Check out SendGrid's Credentials Documentation for the latest guides & details.
2. Setup Drupal for SendGrid
First you need to download, install and enable the Drupal SMTP Module. Once that's done, head over to /admin/config/system/smtp to configure the SMTP module. You can refer to SendGrid's Integration Documentation for the latest on configuration, but I've also outlined a quickstart setup below.
Here's the basic settings you'll need to get started.
- Turn this module on or off: On
- SMTP Server: smtp.sendgrid.net
- SMTP Backup Server: (can be left blank)
- SMTP Port: 587
- Use encrypted protocol: Use TLS
- Username: [username credential from SendGrid]
- Password: [password credential from SendGrid]
- E-Mail from address: your site's email address as configured at /admin/config/system/site-information
- E-Mail from name: [preferred name of sender]
At this point you should be all set. Once you click Save Configuration you'll be sending encrypted mail via a reliable service. Want to see it working? Enter a test email address under E-Mail address to send test e-mail to, then click Save Configuration a second time to test the service. When you receive your test email, you'll notice a few things...
First, it's marked as sent via SendGrid. Second, it's got all of your correct Name and Reply-to information. And finally, it's marked as TLS encrypted. Sweet.
NOTE: One thing that I've noticed with the SMTP module is that if you don't turn the module On in the config screen (first setting in above list), the other settings don't seem to take. If you're working with a production site, make sure to keep the implications in mind before switching everything on.
That's about it. With end-to-end TLS encryption and a reliable external mailing service, you can count on your system emails getting delivered to your users' inbox -- not their spam folder. Have some other ideas for simplifying email notifications in Drupal 7 or avoiding spam filters? Let me in on your tips and tricks in the comments.