【问题标题】:Redirect all outgoing emails to single address for testing将所有外发电子邮件重定向到单个地址以进行测试
【发布时间】:2011-05-22 13:09:54
【问题描述】:

在 Ruby on Rails 中,我希望我的应用程序(在特定的测试环境中)拦截应用程序生成的所有外发电子邮件,并将它们发送到不同的测试地址(也许将正文修改为“最初发送到:...”)。

我看到 ActionMailer 有一些钩子可以观察或拦截邮件,但如果有更简单的方法,我不想旋转我自己的解决方案。有什么建议吗?

【问题讨论】:

    标签: ruby-on-rails email testing environment actionmailer


    【解决方案1】:

    通常,您在测试中所做的是检查 ActionMailer::Base.deliveries,它是通过您的应用程序发送的邮件的 TMail 对象数组。在测试环境中,默认设置是不发送任何内容,只需放入该数组即可。

    我还会考虑在您的测试中使用 email_spec。比滚动您自己的测试功能要方便得多。在使用 email_spec、capybara 的辅助函数和 web 步骤以及 factory_girl 之间,这几乎占了我的大多数应用程序中用于测试的应用程序表面积的 80%。

    【讨论】:

      【解决方案2】:

      我们使用sanitize_email gem 取得了很大的成功。它将所有电子邮件发送到您指定的地址,并在主题前加上原始收件人。听起来它完全符合您的要求,并且让 QA-ing 电子邮件对我们来说变得轻而易举。

      【讨论】:

      • 这看起来正是我正在寻找的。谢谢!
      【解决方案3】:

      老问题,但首先在 Google...

      我最终通过(ab)使用delivery_method = :sendmail 以不同的方式解决了这个问题,这有效地将电子邮件发送到可执行文件;假定为sendmail,但实际上它可以是任何东西。

      在您的config/environments/development.rb 中,您可以执行以下操作:

      YourApp::Application.configure do
          # [...]
          config.action_mailer.delivery_method = :sendmail
          config.action_mailer.sendmail_settings = {
              location: "#{Rails.root}/script/fake-sendmail",
              arguments: 'martin+rails@arp242.net',
          }
      end
      

      然后制作script/fake-sendmail:

      #!/bin/sh
      sendmail -if fake_sendmail@example.com "$1" < /dev/stdin
      

      (别忘了让这个可执行文件!)

      一个相关的解决方案(我更喜欢)是将其附加到 mbox 文件中;这需要很少的设置。

      config/environments/development.rb 看起来很相似:

      YourApp::Application.configure do
          # [...]
          config.action_mailer.delivery_method = :sendmail
          config.action_mailer.sendmail_settings = {
              location: "#{Rails.root}/script/fake-sendmail",
              arguments: "'#{Rails.root}/tmp/mail.mbox'",
          }
      end
      

      script/fake-sendmail 现在看起来像:

      #!/bin/sh
      echo "From FAKE-SENDMAIL $(date)" >> "$1"
      cat /dev/stdin >> "$1"
      echo >> "$1"
      

      使用$any电子邮件客户端打开mbox文件...

      这是一个非常简单的方法,似乎效果很好。更多细节can be found here(我是这个页面的作者)。

      【讨论】:

        【解决方案4】:

        Rails 3.0.9+

        现在使用Action Mailer interceptors 非常简单。

        拦截器可以轻松更改外发邮件的接收地址、主题和其他详细信息。只需定义您的拦截器,然后将其注册到 init 文件中。

        lib/sandbox_mail_interceptor.rb

        # Catches outgoing mail and redirects it to a safe email address.
        module SandboxMailInterceptor
        
          def self.delivering_email( message )
            message.subject = "Initially sent to #{message.to}: #{message.subject}"
            message.to = [ ENV[ 'SANDBOX_EMAIL' ] ]
          end
        end
        

        config/init/mailers.rb

        require Rails.root.join( 'lib', 'sandbox_mail_interceptor' )
        
        if [ 'development', 'staging' ].include?( Rails.env )
          ActionMailer::Base.register_interceptor( SandboxMailInterceptor )
        end
        

        如果您需要在不受拦截器干扰的情况下测试原始电子邮件,您也可以随时取消注册拦截器。

        Mail.unregister_interceptor( SandboxMailInterceptor )
        
        # Test the original, unmodified email.
        
        ActionMailer::Base.register_interceptor( SandboxMailInterceptor )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-04
          • 2016-07-16
          • 1970-01-01
          • 1970-01-01
          • 2014-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多