如何在开发中禁用ActionMailer?

ali*_*lik 26 ruby-on-rails actionmailer environment-variables ruby-on-rails-3.1

有时当我开发时,我没有互联网连接.这会导致我的应用程序发送电子邮件时出错:

getaddrinfo: nodename nor servname provided, or not known
Run Code Online (Sandbox Code Playgroud)

有没有一种简单快捷的方法,我可以更改配置值,使ActionMailer不尝试实际发送电子邮件,而不是抛出错误?也许是开发环境的一些东西.或者其他一些方法,我可以避免错误被抛出,我的代码传递到我称之为actionmailer的地方?

我正在使用Rails 3.1

Wiz*_*Ogz 57

通常的做法就是让Rails忽略邮件错误.在您的config/environments/development.rb文件中添加,取消注释或修改:

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
Run Code Online (Sandbox Code Playgroud)

你也可以这样设置:

config.action_mailer.perform_deliveries = false
Run Code Online (Sandbox Code Playgroud)

请参阅此处的文档http://edgeguides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration

您还可以将交付方式设置为:test,但我还没有尝试过

config.action_mailer.delivery_method = :test
Run Code Online (Sandbox Code Playgroud)

  • 对于那些对 `.delivery_method = :test` 感到好奇的人:ActionMailer 不会发送任何电子邮件,而是将它们存储在一个可用于 `ActionMailer::Base.deliveries` 的数组中。这可能是大多数人的首选选项,因为它使您能够例如在代码中放置断点并确保 ActionMailer 收到您的发送请求(并且看起来应该如此),或者当然可以在测试中使用它。(来源:https://api.rubyonrails.org/classes/ActionMailer/Base.html) (3认同)

dhu*_*han 14

如果要在初始化rails应用程序后(在创建示例数据时,在迁移期间等)禁用邮件传递:

ActionMailer::Base.perform_deliveries = false
Run Code Online (Sandbox Code Playgroud)