【问题标题】:How to setup a mail interceptor in rails 3.0.3?如何在 rails 3.0.3 中设置邮件拦截器?
【发布时间】:2011-09-06 00:47:57
【问题描述】:

我正在使用 rails 3.0.3、ruby 1.9.2-p180、mail (2.2.13)。我正在尝试设置邮件拦截器,但出现以下错误

 /home/abhimanyu/Aptana_Studio_3_Workspace/delivery_health_dashboard_03/config/initializers/mailer_config.rb:16:in `<top (required)>': uninitialized constant DevelopmentMailInterceptor (NameError)

我该如何解决?

我使用的代码如下所示:

config/initializer/mailer_config.rb

ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.default_content_type = "text/html"
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "secure.emailsrvr.com",
:port => '25',
:domain => "domain",
:user_name => "user_name",
:password => "password",
:authentication => :plain

}

ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if  Rails.env.development?

lib/development_mail_interceptor.rb

class DevelopmentMailInterceptor

  def self.delivering_email(message)
    message.to = "email"
  end

end

提前致谢。

【问题讨论】:

    标签: ruby ruby-on-rails-3 actionmailer


    【解决方案1】:

    我发现安装mailcatcher gem 更容易。然后在 development.rb 中:

      config.action_mailer.delivery_method = :smtp
      config.action_mailer.smtp_settings = {
        :address              => "`localhost`",
        :port                 => 1025
      }
    

    然后只需运行“mailcatcher”并在浏览器中点击http://localhost:1080/。它在后台运行,但可以直接从浏览器中退出。为您提供文本+html 视图、源代码和分形分析,如果您采用这种方式。超级干净。

    【讨论】:

    • 很高兴在 Rails 拦截器上尝试这个解决方案。
    【解决方案2】:
    require 'development_mail_interceptor' #add this line
    ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if  Rails.env.development?
    

    【讨论】:

    • 给出错误:`require': cannot load such file -- development_mail_interceptor (LoadError)
    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    相关资源
    最近更新 更多