简体   繁体   English

如何配置ActionMailer以在开发模式下有条件地发送电子邮件?

[英]How can I configure ActionMailer to conditionally send emails in development mode?

I have ActionMailer configured to send emails through gmail in development mode. 我将ActionMailer配置为在开发模式下通过gmail发送电子邮件。

config/development.rb 配置/ development.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"]
}

I have this set up so my designer can trigger and send test html emails to a designated address for testing in a variety of browsers / devices. 我有这个设置,所以我的设计师可以触发并将测试HTML电子邮件发送到指定的地址,以便在各种浏览器/设备中进行测试。

However, in development mode, I'd like to block all outgoing emails that are not being sent to that one designated email address. 但是,在开发模式下,我想阻止所有未发送到该指定电子邮件地址的外发电子邮件。

I'm looking for something like: 我正在寻找类似的东西:

config.action_mailer.perform_deliveries = target_is_designated_email_address?

... but I need someway to inspect the Mail instance, to ensure that it's being sent to the correct address. ...但我需要检查Mail实例,以确保它被发送到正确的地址。

any ideas? 有任何想法吗?

thanks! 谢谢!

Check out the Mail interceptors in the mail gem that ActionMailer uses. 查看ActionMailer使用的邮件gem中的Mail拦截器。 You can find out more about them in this railscast on ActionMailer . 您可以在ActionMailer上的这个railscast中找到更多关于它们的信息。

The relevant part straight from the railscast: 相关部分直接来自railscast:

Create a class to redirect mail. 创建一个类来重定向邮件。

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

Register the interceptor in development mode only: 仅在开发模式下注册拦截器:

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

Then it will just replace the subject line with "[youremail@blahbalh.com] Some subject" and redirect the message to whomever you want. 然后它将用“[youremail@blahbalh.com]某些主题”替换主题行,并将消息重定向到您想要的任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM