2

我想要名为 Timestamp + normal_mail_name + ".eml" 的文件。

我查看了 rails 源代码、mail-gem 源代码和 letter opener-gem .. 你能否给我一个提示如何(猴子补丁)rails 邮件程序以支持我可以指定如下内容:

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

谢谢!

更新: 用我的本地关联电子邮件程序和launchy自动打开这些邮件也很好,就像开信刀gem一样。我会自己做,但我不明白源代码..

4

2 回答 2

2

我认为您有很多邮件内容,并且您想要调试邮件正文、文本等?我对吗?如果我是对的,我不会使用 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

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

于 2013-04-25T16:46:08.907 回答
1

If you just want skip the transmission of the emails through a real mail server to view your emails locally, two good solutions I've used are:

A non-free, OSX-specific solution is to use http://mocksmtpapp.com/

If you want to have a copy of the raw email (headers and all), one way I would do it would be write an email interceptor and write the contents of the mail object to disk.

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

Something like this for 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

and in config/initializers/setup_mail.rb

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

于 2013-04-29T06:25:51.537 回答