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

时间:2011-10-04 02:15:38

标签: ruby-on-rails actionmailer postmark

我正在使用Postmark来处理我的Rails 3应用中的所有电子邮件,使用postmark-rails gem。

用户不时会引入错误的电子邮件或不存在的电子邮件,最终会给予硬弹跳。邮戳会引发Postmark::InvalidMessageError个错误来处理此问题,我的用户会收到这个错误作为非描述性500错误。

我想将这些错误处理到我的响应界面中,我想知道什么是最好的策略。我现在有几个邮件已经有几十个,所以我不想在所有这些方法中添加begin-raise块。将这种开始加注添加到控制器似乎也不是最优雅的解决方案。

我一直在阅读有关向rescue_from添加ApplicationController块的内容,但后来我不知道如何在界面中处理此问题(可能是通过调用使用{{1方法?)

我想在管道调教之前听听你的想法。

有什么想法吗?

1 个答案:

答案 0 :(得分:26)

我们不得不在Beanstalk中处理同样的问题。首先,我们关闭了生产中的“raise_delivery_errors”,然后我们为ActionMailer :: Base实现了一个覆盖方法,允许我们动态更改该设置以进行特定交付。像这样:

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

这使我们能够控制何时出现交付异常,并避免在此类错误打破他们不应该出现的问题时出现问题。通常只有一两个地方你想放置那个覆盖。在我们的例子中,它是忘记密码和邀请用户功能,当让用户知道他们的密码重置电子邮件/邀请没有交付是至关重要的。在后台运行的工作中的某个地方有一个交付例外对任何人都没有帮助。

在我们有了这个之后,我们在我们的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链接到使用Postmark API重新激活跳出的控制器。您可以在此处找到有关它的更多详细信息:

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

因此,在您完成所有这些工作之后,您的最终用户可以在处理交付错误方面获得非常好的体验,这在Web应用程序中通常无法解决。在Beanstalk中看起来像这样:

Beanstalk reactive bounce

不仅用户可以看到他的电子邮件被反弹,他还可以自己反应:

Beanstalk reactivate bounce

希望这有帮助。

Ilya Sabanin http://twitter.com/isabanin