Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to we catch and handle Postmark::InvalidMessageError #19

Closed
noctivityinc opened this issue Oct 3, 2012 · 16 comments
Closed

How to we catch and handle Postmark::InvalidMessageError #19

noctivityinc opened this issue Oct 3, 2012 · 16 comments

Comments

@noctivityinc
Copy link

I noticed in my error log today a number of these:

Postmark::InvalidMessageError
You tried to send to a recipient that has been marked as inactive. Found inactive addresses:

Since we don't do anything special except use Rails 2.3's normal ActiveMailer for sending mail, how do we capture and act on these errors that the Gem is returning?

@temochka
Copy link
Contributor

temochka commented Oct 3, 2012

Hey,

Address becomes inactive when the recipient address has generated a hard bounce (e.g. address does not exists) or when the recipient declares your email as a spam. Address can be reactivated if it is due to bounce (if it is due to spam complaint, you can only reactivate by contacting our support - this is to protect people from getting spam messages).

You can reactivate those inactive emails by using the API. Check out the docs at http://developer.postmarkapp.com/developer-bounces.html#activate-a-bounce.

Otherwise you have to mark those recipients as inactive in your database.

Thanks,
Artem Chistyakov,
Postmark team

@temochka temochka closed this as completed Oct 3, 2012
@noctivityinc
Copy link
Author

Sorry but that doesn't answer the question. How do we handle the exception in rails?

Sent from my iPhone

On Oct 3, 2012, at 12:31 PM, Artem Chistyakov notifications@github.com wrote:

Hey,

Address becomes inactive when the recipient address has generated a hard bounce (e.g. address does not exists) or when the recipient declares your email as a spam. Address can be reactivated if it is due to bounce (if it is due to spam complaint, you can only reactivate by contacting our support - this is to protect people from getting spam messages).

You can reactivate those inactive emails by using the API. Check out the docs at http://developer.postmarkapp.com/developer-bounces.html#activate-a-bounce.

Otherwise you have to mark those recipients as inactive in your database.

Thanks,
Artem Chistyakov,
Postmark team


Reply to this email directly or view it on GitHub.

@temochka
Copy link
Contributor

temochka commented Oct 3, 2012

I'm not sure what your question is about. You can handle these errors the same way you would handle any other exception in Ruby. Wrap your #deliver call with begin/rescue and then log them somewhere, reactivate automatically or do whatever you need. You can introduce a new BaseMailer class and override ActionMailer::Base#deliver method there. Postmark::InvalidMessageError just means that message wasn't delivered to recipient, so it should be processed as any other delivery error. You can also configure ActionMailer not to raise any delivery errors if that's what you need:

config.action_mailer.raise_delivery_errors = false

Let me know if I can provide any other help on this.

@davidpelaez
Copy link

Maybe what you are asking for is something like this:

            puts "Trying to send notification"
            begin
                XMailer.deliver_notifications_for(self)
            rescue Postmark::InvalidMessageError
                puts "An exception was raised while trying to notify. However this was rescued."
                return true
            end

@jonathansimmons
Copy link

@davidpelaez @temochka Is there a way to capture these errors in the application_controller.rb rather than putting a begin-recue at every e-mail call? That just feels clunky to me.

I tried

 rescue_from Postmark::InvalidMessageError do |exception|
    NotificationMailer.postmark_issue(false, exception).deliver
    render nothing: true, status: 200
 end

but this doesn't seem to work. I am still getting 500 errors in my newrelic logs.

@jonathansimmons
Copy link

Anyone? @davidpelaez @temochka

@dgilperez
Copy link

Has anyone got this working along with DelayedJob? I'd like to mark invitations sent to failing emails as so and we are only sending emails through background processes ... I think that DelayedJob exception handling must be patched, but I cannot find any suitable solution out there.

Thanks!

@noctivityinc
Copy link
Author

@dgilperez did you ever get this working with DJ? We tried but were getting all sorts of odd errors, such as the method we created that we called with DJ didnt exist. Thanks.

@temochka
Copy link
Contributor

@noctivityinc could you please mail us at support@postmarkapp.com with some additional details about your setup? (i.e. Ruby version, Rails version, DJ version, postmark&postmark-rails gem version). We’re not aware of any problems when using the gem with Delayed Job.

@dgilperez
Copy link

@noctivityinc sorry, I tried for a while and then stalled the task ... I'd be interested in any solution you can find!

@ssurfr2819
Copy link

We would also be interested in handling these within a delayedjob configuration.

@noctivityinc
Copy link
Author

Yeah that would be great. That largest problem with the recommended solution is that DJ reports (via Airbrake or Exceptional or whatever) the DJ caught error, NOT the actual error that occurred. This makes it extremely difficult to debug.

@zykadelic
Copy link

@temochka’s link seems to be old, this seems to be the new one: http://developer.postmarkapp.com/developer-api-bounce.html#activate-bounce

@bithavoc
Copy link

bithavoc commented Oct 3, 2017

Found a way to catch these:

begin
  client.deliver(...)
catch => e
  if e.full_response['ErrorCode'] == 406
    // email address deactivated, ignore
  else 
    // important error happened
    raise
end

this is what #full_response usually contains:

{"ErrorCode"=>406, "Message"=>"You tried to send to a recipient that has been marked as inactive.\nFound inactive addresses: {redacted}.\nInactive recipients are ones that have generated a hard bounce or a spam complaint. "}

@chrispa
Copy link

chrispa commented Oct 4, 2017

This works fine for us....Rails 4.2 sending with a Postmark Template through Delayed Job. You can just match the specific class of Postmark error you want to rescue.

begin
      postmark_client.deliver_with_template(
      ..........
      )
rescue Postmark::InvalidMessageError => error
     // mark as bounced or take other action...
end

@ndbroadbent
Copy link

ndbroadbent commented Sep 19, 2020

Sorry to comment on such an old issue, but in my case some Postmark::InactiveRecipientError exceptions started blowing up my Sentry error reports while I was testing Rebound with an intentional hard bounce. I send my emails in a background job with Sidekiq, so it would keep retrying the job for the next few days if I didn't delete it manually.

To prevent this from happening in the future, I ignored the exception in my Raven configuration:

# config/initializers/_sentry.rb
Raven.configure do |config|
  config.excluded_exceptions << 'Postmark::InactiveRecipientError'
end

UPDATE: Actually this just prevents the error from making it to Sentry, but the Sidekiq jobs will continue being retried until they get stale.

This rescue_from fix looks like the right way to go.

LAST UPDATE: The wiki has a section about error handling. I should have read that first!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants