applebee.io logo
Back to articles
DrupalSendGridTwilioDevOps

Integrating SendGrid and Twilio with Drupal

Jim Applebee

Integrating SendGrid and Twilio with Drupal

Adding an email or SMS provider to Drupal is easy. Building a dependable notification workflow is not. The application has to authenticate safely, avoid slowing down a web request, record what happened, and respond sensibly when a provider accepts a message but the carrier or mailbox provider does not deliver it.

Keep provider credentials out of Drupal configuration exports

Use restricted API keys and load secrets from the hosting platform’s secret store or environment. Do not commit them to the repository or export them with Drupal configuration. Authenticate the sending domain in SendGrid so receiving systems can evaluate mail from that domain. For Twilio webhooks, validate the request signature before accepting a status update.

Drupal configuration can safely hold non-secret choices such as the sender name, a template identifier, or the queue to use. The secret itself belongs outside the database and codebase.

Queue the work

A form submission or content update should normally create a small notification record and place work on a queue. A worker can then call SendGrid or Twilio outside the user’s web request.

That separation matters because a slow provider does not make the Drupal page slow, transient failures can be retried without asking the user to repeat the original action, and the application has a durable record of why the message was requested.

Make the queue item idempotent. A retry should not send a duplicate message merely because a worker lost its connection after the provider accepted the first request. Store the provider’s message identifier and use an application-level idempotency key where the API and workflow permit it.

Treat acceptance and delivery as different events

An API success means the provider accepted the request. It does not guarantee that an email reached an inbox or an SMS reached a handset.

SendGrid’s Event Webhook reports events such as processed, delivered, deferred, bounced, and dropped. Twilio sends asynchronous message status callbacks such as queued, sent, delivered, failed, and undelivered. Persist those events against the notification record. Do not assume callbacks will arrive in the order you expect; handlers should tolerate duplicates and out-of-order updates.

Use the failure category to decide what happens next. A temporary network error may deserve a bounded retry with backoff. An invalid number, hard bounce, opt-out, or permanent provider rejection usually should not. Repeated blind retries can create duplicate messages, damage sender reputation, or violate a recipient’s preference.

Transactional and marketing communication are not interchangeable. Record the channel, purpose, source, and time of consent, and process opt-outs promptly. Before creating an SMS job, confirm that the use case, recipient, sender type, and jurisdiction permit it. Email workflows should also maintain suppression and unsubscribe state rather than treating it as a reporting detail.

Avoid putting sensitive data into webhook metadata or logging full message bodies. Store only what operations and support actually need, and set a retention period.

A useful Drupal boundary

A clean implementation usually has four parts:

  1. Drupal decides that a notification is needed and records the business event.
  2. A queue worker renders the approved template and submits it with a stable correlation identifier.
  3. A secured webhook endpoint records delivery or failure events.
  4. Operations can inspect failures, retry eligible messages, and suppress ineligible recipients.

The provider handles transport. Drupal still owns the business rule, consent, message history, and recovery behavior. That boundary makes it possible to change providers later without rewriting the workflow that decided why a message should exist.

Sources