Rails Mailer:在delivery_method:file时更改文件

时间:2013-04-22 19:12:08

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

我希望文件名为Timestamp + normal_mail_name +“。eml”..

我查看了rails源代码,mail-gem源代码和字母opener-gem ..你能给我一个提示如何(猴子补丁)rails mailer支持我可以指定类似的东西:

config.action_mailer.file_settings = {:location => Rails.root.join('tmp','mail'),: file_name => Time.now.to_i.to_s +“mail.eml”}

谢谢!

更新 使用我的本地相关电子邮件程序与launchy一起自动打开这些邮件也很好,就像开信宝石一样......我会自己做,但我不懂源代码..

2 个答案:

答案 0 :(得分:1)

我认为你有很多邮件,你想调试邮件正文,文本等?我对吗? 如果我是对的,我不会发送带有delivery_method:file的邮件,我只会创建一个真实的电子邮件(例如gmail)帐户并通过测试帐户发送邮件。

例如在config / environments / development.rb中:

email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/mail.yml"))[Rails.env] rescue nil

if email_settings.nil?
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.perform_deliveries = false
  config.action_mailer.delivery_method = :file
else
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address              => "#{email_settings["address"]}",
    :port                 => email_settings["port"],
    :authentication       => "#{email_settings["authentication"]}",
    :user_name            => "#{email_settings["user_name"]}",
    :password             => "#{email_settings["password"]}",
    :enable_starttls_auto => email_settings["enable_starttls_auto"]
  }
end

你的mail.yml文件:

development:
  address: smtp.gmail.com
  port: 587
  authentication: login
  user_name: test@your-domain.com
  password: yourpassword
  enable_starttls_auto: true

对于你的问题,这不是一个直接的答案,但也许这个工作对你来说是个不错的选择。您也可以根据需要以相同的方式配置其他环境。

答案 1 :(得分:0)

如果您只是想通过真实的邮件服务器跳过电子邮件的传输来在本地查看您的电子邮件,我使用的两个好的解决方案是:

非自由的OSX特定解决方案是使用http://mocksmtpapp.com/

如果你想拥有原始电子邮件的副本(标题和全部),我会这样做的方法是编写一个电子邮件拦截器并将邮件对象的内容写入磁盘。

http://railscasts.com/episodes/206-action-mailer-in-rails-3

lib/development_mail_interceptor

这样的内容
class DevelopmentMailInterceptor
  def self.delivering_email(message)
    message.perform_deliveries = false
    File.open("#{Time.now.to_i}-email.eml", "w") { |f| f.write(message.to_s) }
  end
end

和config / initializers / setup_mail.rb

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