内部 Controller :
def update
@user.update({approved: true})
UserMailer.send_approved_mail(@user)
redirect_to(root_url)
end
在 user_mailer.rb 里面
class UserMailer < Devise::Mailer
def send_approved_mail(user)
@resource = user
email_body = render "user_activated_mail"
if @resource.valid?
client = Postmark::ApiClient.new(ENV["POSTMARK_API_KEY"])
client.deliver(from: ENV["POSTMARK_SIGNATURE"],
to: @resource.email, subject: "User Activation information.",
tag: 'account-activated', :content_type => "text/html",
html_body: email_body)
end
end
end
在 rails 4.1.0 中, Controller 中的上述方法被调用并发送电子邮件,但在 rails 4.2 中, Controller 中的邮件程序方法未被调用,但是当从 rails 控制台调用时,它可以工作。我已经为 postmark api 和配置文件做了所有必要的配置。唯一的事情是在 Controller 内部的 rails 4.1.0 mailer 中被调用,但在 rails 4.2 中它不会,但是当从 rails 控制台调用时它可以工作。具体是什么原因实在想不通。
最佳答案
您在此处注意到的行为是 Rails 4.2 中引入的新默认值:
With the introduction of Active Job and #deliver_later ... the invocation of the instance methods (on a mailer) is deferred until either deliver_now or deliver_later is called.
查看 Rails 4.2 upgrade guide更多细节。
不过不用担心。您仍然可以将 Postmark 与 ActionMailer 一起使用。 official Postmark Rails gem为 Postmark 提供与 ActionMailer 的直接集成。通过使用它,您应该能够像编写常规 Rails 邮件程序一样编写邮件程序,而无需在代码中手动管理 Postmark 连接。
附言我在 Wildbit(邮戳的创建者)工作。请随时直接与我们联系。
关于ruby-on-rails-4.2 - 未调用邮件程序方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29386753/