【问题标题】:ActionMailer::Base.mail method is 'Not available' when called from initializer从初始化程序调用 ActionMailer::Base.mail 方法时“不可用”
【发布时间】:2021-11-24 18:27:46
【问题描述】:

我想创建一个处理程序来拯救电子邮件(主要是因为超时错误),所以我可以稍后尝试重新发送它们。 其他人建议使用其中定义的 ActionMailer::DeliveryJob.rescue_from 的初始化程序(我们使用 Deliver_later,无法挽救它)。此外,我需要渲染电子邮件,因为我们的模板引用了 activerecord 模型,因此如果我在再次发送电子邮件之前渲染电子邮件,可能会发生变化。 我的问题是 ActionMailer::Base.mail 似乎在这里不可用:

ActionMailer::DeliveryJob.rescue_from(StandardError) do |exception|
    attempt = EmailAttempt.new
    attempt.error_class    = exception.class.to_s
    attempt.mailer         = self.arguments[0]
    attempt.email_template = self.arguments[1]
    attempt.params         = self.arguments[3]
    attempt.enqueued_at    = self.enqueued_at
    attempt.timezone       = self.timezone

    attempt.email_body = ActionMailer::Base.mail(**self.arguments[3][:email]) do |format|
      format.html do
          render locals: self.arguments[3][:body]
      end
    end

    attempt.save!
end

在调用邮件方法之前停止代码,只需将ActionMailer::Base.mail 放入调试控制台会产生“不可用”消息(该类似乎可用,ActionMailer::Base.methods 会打印很多东西,@ 987654323@不在其中)。

请忽略方法的参数化,这是一个非常正在进行的代码,但是一旦我弄清楚如何调用它,我就可以整理(我认为)给它提供哪些参数。

我绝对不会深入研究 Ruby 或 Rails,所以这可能是新手错误,但我们将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby actionmailer


    【解决方案1】:

    调试器似乎有点问题,如果我使用 byebug 代替,方法就在那里。

    之前,我使用内置的 VS Code 调试器(带有 ruby​​-debug-ide 和 debase gem),但它的工作方式似乎与 byebug 略有不同。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2014-07-29
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多