简体   繁体   English

使用Postmarkapp在Rails应用程序中处理电子邮件异常的最佳方法是什么?

[英]What is the best way to handle Email exceptions in a Rails app using Postmarkapp?

I am using Postmark to handle all email in my Rails 3 app, using postmark-rails gem. 我正在使用Postmark来处理我的Rails 3应用程序中的所有电子邮件,使用postmark-rails gem。

Now and then an user introduces a wrong email or a non-existing one with ends up giving hardbounces. 偶尔用户会引入错误的电子邮件或不存在的电子邮件,最终会给予硬弹跳。 Postmark raises Postmark::InvalidMessageError errors to handle this issue, that my users receive as a non-descriptive 500 error. Postmark::InvalidMessageError引发Postmark::InvalidMessageError错误来处理此问题,我的用户收到的错误是非描述性500错误。

I'd like to handle those errors into my responsive interface and I was wondering what would be the best strategy. 我想将这些错误处理到我的响应界面中,我想知道什么是最好的策略。 I have now a few mailers already with several dozens amongst all, so I don't want to add begin-raise blocks to all those methods. 我现在有几个邮件已经有几十个,所以我不想在所有这些方法中添加begin-raise块。 Adding this begin-raise to controllers also doesn't seem the most elegant solution. 将这种开始加注添加到控制器似乎也不是最优雅的解决方案。

I've been reading about adding a rescue_from block to my ApplicationController , but then I don't know how to handle this in the interface (maybe by calling a method that uses errors method?) 我一直在阅读有关将rescue_from块添加到我的ApplicationController ,但后来我不知道如何在界面中处理它(可能是通过调用使用errors方法的方法?)

I'd like to listen to your thoughts before plumbering. 我想在开始管道之前听听你的想法。

Any ideas? 有任何想法吗?

We had to deal with the same problem in Beanstalk. 我们不得不在Beanstalk中处理同样的问题。 First of all we turned off "raise_delivery_errors" in production then we implemented an override method for ActionMailer::Base that allowed us to change that setting on the fly for specific deliveries. 首先,我们关闭了生产中的“raise_delivery_errors”,然后我们为ActionMailer :: Base实现了一个覆盖方法,允许我们动态更改该设置以进行特定交付。 Like this: 像这样:

AccountMailer.raise_errors do
  AccountMailer.deliver_welcome_email(@account)
end

That allowed us to control when exactly we want delivery exceptions appearing and avoid problems when such errors were breaking something they shouldn't. 这使我们能够控制何时出现交付异常,并避免在此类错误打破他们不应该出现的问题时出现问题。 Usually there are only one or two places where you'd want to put that override. 通常只有一两个地方你想放置那个覆盖。 In our case it's Forget Password and Invite User functions, when it's crucial to let users know that their password reset email/invitation wasn't delivered. 在我们的例子中,它是忘记密码和邀请用户功能,当让用户知道他们的密码重置电子邮件/邀请没有交付是至关重要的。 Having a delivery exceptions somewhere inside a background-running job doesn't help anyone. 在后台运行的工作中的某个地方有一个交付例外对任何人都没有帮助。

After we had that in place we added a rescue_from to our ApplicationController that would set flash[:alert] and redirect back. 在我们有了这个之后,我们在我们的ApplicationController中添加了一个rescue_from,它将设置flash [:alert]并重定向回来。

def postmark_delivery_error(exception)
  if address = derive_email_from_postmark_exception(exception)
    link = %Q[<a href="#{ reactivate_email_bounce_path(address)  }">reactivating</a>]
    msg = "We could not deliver a recent message to “#{ address }”. The email was disabled due to a hard bounce or a spam complaint. You can try #{ link } it and try again."
  else
    msg = "We could not deliver a recent message. The email was disabled due to a hard bounce or a spam complaint. Please contact support."
  end
  flash[:alert] = msg
  redirect_to :back
end

reactivate_email_bounce_path links to a controller that uses Postmark API to reactivate bounces. reactivate_email_bounce_path链接到使用Postmark API重新激活跳出的控制器。 You can find more details about it here: 你可以在这里找到更多相关的细节:

http://developer.postmarkapp.com/developer-bounces.html http://developer.postmarkapp.com/developer-bounces.html

So after you have all that in place your end user can have a pretty nice experience dealing with delivery errors, something that's not usually addressed in web apps. 因此,在您完成所有这些工作之后,您的最终用户可以在处理交付错误方面获得非常好的体验,这在Web应用程序中通常无法解决。 It looks like this in Beanstalk: 在Beanstalk中看起来像这样:

Beanstalk反应性反弹

And not only a user can see that his email bounced, he can also reactive it himself: 不仅用户可以看到他的电子邮件被反弹,他还可以自己反应:

Beanstalk重新激活反弹

Hope this helps. 希望这可以帮助。

Ilya Sabanin http://twitter.com/isabanin Ilya Sabanin http://twitter.com/isabanin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM