Using SendGrid To Deliver Email

Detailed information on using SendGrid to deliver email through your WordPress and Drupal site.

Contributors: Erik Mathy, Brian Thompson, Brent Connor, Sarah German


Email is a necessity when running a website, whether it's used with a simple contact form or to manage subscription based services, odds are you’re going to need it. Users may want to receive notices of content updates, have sales receipts sent to them, update their password or membership information, and more. Email is the most effective way of communicating with a site's user base, but it does no good if these messages are filtered and marked as spam.

One of the most common reasons that email gets blocked is because it originates from a website hosted by a third party service, like Pantheon. In order to ensure this doesn't happen to you, we at Pantheon highly encourage using your own email server or a service provider such as SendGrid.

Why SendGrid?

SendGrid has a lot of things going for it, all of which do the hard work for you when it comes to how mail is managed on your site. It can handle massive email campaigns while providing highly detailed and flexible reporting, along with the ability to scale and increase deliverability.

Create a SendGrid Account

  1. Get started by signing up for an account and selecting a plan that meets your business needs. After receiving your confirmation email, sign in to your SendGrid account.

  2. From within the Settings menu, click API Keys:

    SendGrid Multiple User setup

  3. Click Create API Key to define the name and permissions for the API key your site will use. Click Create & View to complete the process.

  4. The API key will only be displayed once. Copy the key and save it somewhere secure until you can apply it to the site.

Integrating Sendgrid With Drupal and Wordpress

Follow the procedure outlined below for your CMS:

Download the WordPress SendGrid Plugin

The official SendGrid Plugin replaces the wp_mail function with API integration, making it easy to configure and get started.

Install and activate the latest release through the WordPress dashboard or place it in the code/wp-content/plugins directory and activate via the dashboard.

Add Your SendGrid Account Details

  1. After you have installed and activated the plugin, go to your site's Dashboard and select Settings, then click the SendGrid menu item.
  2. Provide the API Key from the previous section.
  3. Enter the email address to send from and provide a reply email address if you prefer replies to go to another address (optional).
  4. SendGrid supports categories so you can track email analytics and organize message types. Include any categories you would like to use, separated by commas.

WP Settings example

When you're done, click Update Setings. Your WordPress application is now set up to send email through SendGrid! Complete the fields in the SendGrid Test section of the Settings page and watch the magic work its way to your inbox. For guidance on checking deliverability in SendGrid, see Checking Deliverability in SendGrid.

Choosing an Integration Method

Two methods can be used to integrate SendGrid with your Drupal 8 site: API or SMTP.

Considerations

  • SMTP requests are associated with dynamic outgoing IPs, which can have a negative impact on deliverability.
  • API integration using the SendGrid Integration module is recommended; however, installation of this module is slightly more complicated, as it requires the use of Composer.

SendGrid API Integration

Warning

The SendGrid API Integration Module for Drupal 8 requires a Composer managed workflow as described in our Build Tools or Drupal 8 and Composer on Pantheon Without Continuous Integration guides. We cannot support non-Composer workflows using this module. For details, see the module readme file.

  1. Install the SendGrid Integration module using Composer:

    composer require drupal/sendgrid_integration
    
  2. Commit, push, and enable the new module.

  3. Visit /admin/config/services/sendgrid once you've logged into your Drupal site as an administrator. Paste your API Key and click Save Configuration.

Your Drupal application on Pantheon is now set up to send email through SendGrid's API. Test your configuration from /admin/config/services/sendgrid/test.

SendGrid SMTP Integration

A stable release for Drupal 8 is not yet available for the SMTP Authentication Support module. However, some users have reported success with the beta version.

  1. Install the SMTP Authentication Support module using the Drupal interface or with Terminus:

    terminus drush <site>.<env> -- en smtp -y
    
  2. Visit /admin/config/system/smtp once you've logged in as an administrator.
  3. From within Install Options, select On.
  4. Use the following within SMTP Server Settings:

    SMTP server: smtp.sendgrid.net

    SMTP port: 2525

    Use encrypted protocol: We strongly recommend selecting TLS

  5. Provide your site-specific SendGrid credentials and click Save configuration.

Your Drupal application on Pantheon is now set up to send email through SendGrid. Provide an address within the Send Test E-mail configuration field and click Save configuration to test.

Choosing an Integration Method

Two methods can be used to integrate SendGrid with your Drupal 7 site: API or SMTP.

Considerations

  • SMTP requests are associated with dynamic outgoing IPs, which can have a negative impact on deliverability.
  • API integration using the SendGrid Integration module is recommended; however, installation of this module is slightly more complicated, as it requires the use of Composer. For Drupal 7 we can utilize the dev release of this module and use Composer in a non-invasive manner that is compatible with upstream updates.

SendGrid API Integration

  1. Install Composer, then install and authenticate Terminus.
  2. If you haven't done so already, clone your Pantheon site repository and navigate to the project's root directory. Replace <site_name> with your site's name (e.g., your-awesome-site):

    SITE=<site_name>
    `terminus connection:info $SITE.dev --fields='Git Command' --format=string`
    cd $SITE
    
  3. Set the connection mode to Git:

    terminus connection:set $SITE.dev git
    
  4. Install the dev release of the SendGrid Integration module (and it's dependencies) and the Composer Vendor module using Drush:

    drush dl sendgrid_integration-7.x-1.x-dev mailsystem composer_vendor
    

  5. Initialize composer to create a composer.json file with the Drupal 7 package repository:

    composer init --repository=https://packages.drupal.org/7 --no-interaction
  6. Edit the composer.json to add extra configuration that specifies installation paths for Drupal modules, libraries, and themes.

    {
      "repositories": [
        {
          "type": "composer",
          "url": "https://packages.drupal.org/7"
        }
      ],
      "require": {},
      "extra": {
        "installer-paths": {
          "sites/all/modules/{$name}/": ["type:drupal-module"],
          "sites/all/themes/{$name}/": ["type:drupal-theme"],
          "sites/all/libraries/{$name}/": ["type:drupal-library"]
        }
      },
      "scripts": {
        "remove-git-submodules": "find . -mindepth 2 -type d -name .git | xargs rm -rf",
        "post-install-cmd": [
          "@remove-git-submodules"
        ],
        "post-update-cmd": [
          "@remove-git-submodules"
        ]
      },
      "config": {
        "vendor-dir": "sites/all/vendor"
      }
    }
  7. Require the SendGrid API Library via Composer:

    composer require fastglass/sendgrid:~1.0
    
  8. Add, commit, and push all of the new files:

    git add .
    git commit -am "Add Sendgrid API Integration"
    git push origin master
    
  9. Enable Composer Vendor, followed by SendGrid Integration. Order is important here, SendGrid Integration will refuse to activate if the library file is not autoloaded:

    terminus drush $SITE.<env> -- en composer_vendor -y
    terminus drush $SITE.<env> -- en sendgrid_integration -y
    
  10. From within your SendGrid account, navigate to Settings > API Keys and create a site-specific API Key. Click the key to copy it to your keyboard.

  11. Visit /admin/config/services/sendgrid once you've logged into your Drupal site as administrator. Paste your API Key and click Save Settings.

Your Drupal application on Pantheon is now set up to send email through SendGrid's API. Test your configuration from /admin/config/services/sendgrid/test.

SendGrid SMTP Integration

  1. Install the SMTP Authentication Support module using the Drupal interface or with Terminus:

    terminus drush <site>.<env> -- en smtp -y
    
  2. Visit /admin/config/system/smtp once you've logged in as administrator.
  3. From within Install Options, select On.
  4. Use the following within SMTP Server Settings:

    SMTP server: smtp.sendgrid.net

    SMTP port: 2525

    Use encrypted protocol: We strongly recommend selecting TLS

  5. Provide your site-specific SendGrid credentials and click Save configuration.

Your Drupal application on Pantheon is now set up to send email through SendGrid. Provide an address within the Send Test E-mail configuration field and click Save configuration to test.

Checking Deliverability in SendGrid

For testing purposes, your first few deliveries should be to email addresses that you control directly. You can track and measure unique aspects of mail behaviors from within your site's SendGrid account, which should be monitored regularly.

First, log into SendGrid and select Activity. You will be taken to a page with a form to search by email. Enter the email address, and press Enter. SendGrid will search through your mail queue for any messages sent to that address. For additional search parameters, select the filters near the top right corner.

SendGrid email search options

You can explore the Statistics and Email Reports from within your site's account to gain insight into email activity, statistics on email clients, and much more.

Congratulations!

You have now successfully integrated an industrial strength, simple to use, email delivery service into your website. If you have any questions, contact SendGrid's support team or check out SendGrid’s Email Infrastructure Guide for advanced tips on how to create and publish DNS records for increased deliverability.

Troubleshooting

In some cases, other WordPress plugins can conflict with the Sendgrid plugin and cause it to use PHPMailer instead of SendGrid. One such plugin is called iLightBox.

See Also