Actionmailer SMTP 服务器响应

发布于 2024-11-15 22:03:27 字数 214 浏览 4 评论 0原文

当通过actionmailer发送邮件时,actionmailer会从SMTP服务器获取响应,无论何时正常,或者何时错误。有没有办法在发送邮件后检索此回复? 另外,当 SMTP 服务器没有抛出错误时?

我们的 qmail 邮件服务器抛出一个处理程序 ID,我们想用它来跟踪电子邮件。

例如,服务器响应如下:

250 ok 1308235825 qp​​ 17832

When sending mail through actionmailer, the actionmailer gets a response from the SMTP server, when its ok, or when its wrong. Is there a way to retrieve this response after sending a mail?
Also when no errors are thrown by the SMTP server?

Our qmail mail server throws a handler id which we want to use for tracing e-mails.

As an example, the server response is this :

250 ok 1308235825 qp 17832

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

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

发布评论

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

评论(2

可是我不能没有你 2024-11-22 22:03:27

在 smtp 设置中设置 return_response: true 并调用 message.deliver! 而不是 deliver。这将返回 SMTP 服务器响应,即 Net::SMTP::Response,其中包含您要查找的服务器响应。

如果您需要与服务器连接的所有响应的日志,而不仅仅是最终结果,则需要深入研究 Net::SMTP。

Set return_response: true in the smtp settings and call message.deliver! instead of deliver. This returns the SMTP server response, a Net::SMTP::Response, which contains the server response you're looking for.

If you need a log of all responses from the connection with the server, not just the final result, you'll need to dig into Net::SMTP.

你怎么这么可爱啊 2024-11-22 22:03:27

查看源代码,您可以定义一个观察者:

base.rb

  # Register an Observer which will be notified when mail is delivered.
  # Either a class or a string can be passed in as the Observer. If a string is passed in
  # it will be <tt>constantize</tt>d.
  def register_observer(observer)
    delivery_observer = (observer.is_a?(String) ? observer.constantize : observer)
    Mail.register_observer(delivery_observer)
  end

因此,您可以在初始化文件中使用类似这样的代码:

class MailObserver
  def self.delivered_email(message)
    logger_info "Sent Message: #{message}"
  end
end

ActionMailer::Base.register_observer(MailObserver)

这将记录发送的邮件,您可以查看是否可以从发送的邮件对象获取标头或响应。

Looking at the the source you can define an observer:

in base.rb

  # Register an Observer which will be notified when mail is delivered.
  # Either a class or a string can be passed in as the Observer. If a string is passed in
  # it will be <tt>constantize</tt>d.
  def register_observer(observer)
    delivery_observer = (observer.is_a?(String) ? observer.constantize : observer)
    Mail.register_observer(delivery_observer)
  end

So you could use some code like this in an initialization file:

class MailObserver
  def self.delivered_email(message)
    logger_info "Sent Message: #{message}"
  end
end

ActionMailer::Base.register_observer(MailObserver)

That will log sent mail and you can see if you can get the headers or response from the sent mail object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文