如何创建邮件观察者
每当在我的应用程序上发送电子邮件时,我想运行一些代码。
由于 ActionMailer 不支持 after_filter,我想使用观察者。
Rails 文档顺便提到了这一点,但没有详细说明。
谢谢!
I'd like to run some code whenever an email is sent on my app.
As ActionMailer doesn't support after_filter, I would like to use an observer.
The Rails docs mention this in passing, however does not elaborate.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很惊讶 Rails 的文档中对此的描述如此之少。
基本上,Rails 3 中的 ActionMailer 引入了拦截器(在消息发送之前调用)和观察者(在消息发送之后调用)的使用。
要设置观察者,请将以下内容添加到初始值设定项:
现在,每次应用发送电子邮件时都会运行
delivered_email
方法。但是,您只能访问实际的Mail
消息。要注册拦截器,请执行与上面相同的操作,将
register_observer
替换为register_interceptor
,并将该方法从self.delivered_email
重命名为self.delivering_email
。此 Railscast 是我能找到的有关此信息的最佳来源(他们只谈论拦截器,但对于观察者来说概念是相同的)。
I'm surprised how little there is in Rails' documentation about this.
Basically, ActionMailer in Rails 3 introduces the use of Interceptors (called before the message is sent) and Observers (after the message is sent).
To set up an Observer, add the following to an initializer:
Now, the
delivered_email
method will run every time your app sends an e-mail. However, you will only have access to the actualMail
message.To register an Interceptor instead, do the same as above, replacing
register_observer
withregister_interceptor
, and renaming the method fromself.delivered_email
toself.delivering_email
.This Railscast was the best source I could find for info on this (they only talk about interceptors, but the concept is the same for observers).