0

我已将 ActionMailer 配置为在开发模式下通过 gmail 发送电子邮件。

配置/开发.rb

config.action_mailer.default_url_options = { host: ENV["MY_DOMAIN"] }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: ENV["MY_DOMAIN"],
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["MY_USERNAME"],
  password: ENV["MY_PASSWORD"]
}

我有这个设置,所以我的设计师可以触发测试 html 电子邮件并将其发送到指定地址,以便在各种浏览器/设备中进行测试。

但是,在开发模式下,我想阻止所有未发送到该指定电子邮件地址的外发电子邮件。

我正在寻找类似的东西:

config.action_mailer.perform_deliveries = target_is_designated_email_address?

...但我需要以某种方式检查 Mail 实例,以确保将其发送到正确的地址。

有任何想法吗?

谢谢!

4

1 回答 1

2

查看 ActionMailer 使用的邮件 gem 中的邮件拦截器。您可以在 ActionMailer 上的这个 railscast中找到更多关于它们的信息。

直接来自railscast的相关部分:

创建一个类来重定向邮件。

class DevelopmentMailInterceptor
  def self.delivering_email(message)
    message.subject = "[#{message.to}] #{message.subject}"
    message.to = "eifion@asciicasts.com"
  end
end

仅在开发模式下注册拦截器:

Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?

然后它只会将主题行替换为“[youremail@blahbalh.com] Some subject”,并将消息重定向到您想要的任何人。

于 2012-09-26T16:14:53.433 回答