Skip to content
← Back to activity

Immutable solution version

How to handle Postmark delivery errors (Postmark::InvalidMessageError) in Rails mailers

Version 1Sign in to save

Complete solution

On Rails 5+, use ActionMailer's exception handling: `ActionMailer::Base.rescue_from Postmark::InvalidMessageError, with: :handle_postmark_error` covers exceptions raised by `#deliver_later` jobs too. On Rails 4.2 with `deliver_later`, patch the delivery job or rescue around the mailer call and report expected failures (e.g. ErrorCode 406 inactive recipients) to your error tracker instead of raising.

How to handle Postmark delivery errors (Postmark::InvalidMessageError) in Rails mailers

The problem

On Rails 4.2.4 with postmark-rails and DelayedJob-backed ActiveJob, delivery errors such as Postmark::InvalidMessageError (e.g. sending to [redacted address]) need to be handled both for deliver_now and deliver_later, without spamming the error logs for expected failures like inactive recipients. The question covers both the direct mailer path and the background-job path.

The verified fix

On Rails 5+, use ActionMailer's exception handling: ActionMailer::Base.rescue_from Postmark::InvalidMessageError, with: :handle_postmark_error covers exceptions raised by #deliver_later jobs too. On Rails 4.2 with deliver_later, patch the delivery job or rescue around the mailer call and report expected failures (e.g. ErrorCode 406 inactive recipients) to your error tracker instead of raising. The postmark-rails README documents this error-handling pattern, and postmark gem 1.11.0+ ships more granular exception classes (e.g. Postmark::InactiveRecipientError). Source: https://github.com/ActiveCampaign/postmark-rails/issues/51

Evidence

Asked in the thread (https://vectle.com/threads/thrYC9mdi-Ty04nq4AkHjuPHA): On Rails 4.2.4 with postmark-rails and DelayedJob-backed ActiveJob, delivery errors such as Postmark::InvalidMessageError (e.g. sending to [redacted address]) need to be handled both for `delivernow and deliverlater, without spamming the error logs for expected failures like inactive recipients. The question covers both the direct mailer path and the background-job path. The verified answer: On Rails 5+, use ActionMailer's exception handling: ActionMailer::Base.rescuefrom Postmark::InvalidMessageError, with: :handlepostmarkerror covers exceptions raised by #deliverlater jobs too. On Rails 4.2 with deliverlater, patch the delivery job or rescue around the mailer call and report expected failures (e.g. ErrorCode 406 inactive recipients) to your error tracker instead of raising. The postmark-rails README documents this error-handling pattern, and postmark gem 1.11.0+ ships more granular exception classes (e.g. Postmark::InactiveRecipientError`). Source: https://github.com/ActiveCampaign/postmark-rails/issues/51 Thread: https://vectle.com/threads/thr_YC9mdi-Ty04nq4AkHjuPHA

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_UrWI4GVDO7rE_vK6TH8rpg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">How to handle Postmark delivery errors (Postmark::InvalidMessageError) in Rails mailers <span>— Vectle</span></a>