如何在 Rails 中捕获电子邮件的输出并将其放入变量中

发布于 2024-11-04 09:23:29 字数 116 浏览 2 评论 0原文

从 ActionMailer 输出发送到日志的电子邮件的输出,我想知道是否可以将该输出放入变量中,以便将其存储在文件中。

Ps. 我忘了提及这是在 Rails 2 上

The output from an email sent from ActionMailer output to the log, I wanted to know if I could get that output into a variable so I can store it in a file.

Ps. I forgot to mention that this is on Rails 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风尘浪孓 2024-11-11 09:23:29

正如 McStretch 所指出的,观察者是处理邮件发送者传递的每条消息的最佳方式。但是,如果您只想捕获 1 或 2 个特殊情况,可以执行以下操作:

假设您有一个名为 MyMailer 的 ActionMailer 子类,以及一个名为 foobar 的电子邮件,

# Rails 2.x
mail = MyMailer.create_foobar(...) # instead of MyMailer.deliver_foobar(...)
File.open('filename.txt', 'wb') {|f| f.write(mail.body) }
MyMailer.deliver(mail)

# Rails 3.x
mail = MyMailer.foobar(...) # instead of MyMailer.foobar(...).deliver
File.open('filename.txt', 'wb') {|f| f.write(mail.body) }
mail.deliver

As McStretch has pointed out, observer is the best way to handle every message that is delivered by a mailer. However, if you'd like to just capture 1 or 2 special cases, you can do the following:

Assuming you have an ActionMailer subclass called MyMailer, and an email called foobar,

# Rails 2.x
mail = MyMailer.create_foobar(...) # instead of MyMailer.deliver_foobar(...)
File.open('filename.txt', 'wb') {|f| f.write(mail.body) }
MyMailer.deliver(mail)

# Rails 3.x
mail = MyMailer.foobar(...) # instead of MyMailer.foobar(...).deliver
File.open('filename.txt', 'wb') {|f| f.write(mail.body) }
mail.deliver
仲春光 2024-11-11 09:23:29

您可以使用 register_interceptorregister_observer 方法分别在发送邮件之前或之后执行某些操作。 ActionMailer 文档指出:

Action Mailer 提供了挂钩
邮件观察者和拦截器方法。
这些允许您注册对象
邮件期间被调用的
交付生命周期。

观察者对象必须实现
:delivered_email(message) 方法
每封电子邮件都会被调用一次
电子邮件发送后发送。

拦截器对象必须实现
:delivering_email(message) 方法
它将在电子邮件之前被调用
已发送,允许您制作
修改之前的电子邮件
打击送货代理。你的对象
应该进行和需要的修改
直接传入传入的
Mail::Message 实例。

这些方法中的每一个都提供 Mail::Message 作为参数,因此您应该能够从该对象获取所需的数据并将其保存在某处:

class  MyInterceptor
  def self.delivering_email(mail)
    # do something before sending the email
  end
end

class MyObserver
  def self.delivered_email(mail)
     # do something after sending the email
  end
end

You can use the register_interceptor or register_observer methods on ActionMailer to do something before or after sending the mail, respectively. The ActionMailer docs state:

Action Mailer provides hooks into the
Mail observer and interceptor methods.
These allow you to register objects
that are called during the mail
delivery life cycle.

An observer object must implement the
:delivered_email(message) method which
will be called once for every email
sent after the email has been sent.

An interceptor object must implement
the :delivering_email(message) method
which will be called before the email
is sent, allowing you to make
modifications to the email before it
hits the delivery agents. Your object
should make and needed modifications
directly to the passed in
Mail::Message instance.

Each of these methods provide a Mail::Message as an argument, so you should be able to get the desired data from that object and save it somewhere:

class  MyInterceptor
  def self.delivering_email(mail)
    # do something before sending the email
  end
end

class MyObserver
  def self.delivered_email(mail)
     # do something after sending the email
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文