在 Rails 应用程序中,我在其 environments/
中使用以下参数设置了一个新的登台环境。文件:
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
但是,当系统生成电子邮件时,它会打印到
staging.log
文件而不是发送。我的 SMTP 设置在其他环境中工作正常。我缺少什么配置才能实际发送电子邮件?编辑:是的,临时框设置了有效配置,用于它有权访问的 SMTP 服务器。问题似乎不在于 SMTP 设置(如果是,我不会在日志中收到错误吗?),而在于 Rails 配置。该应用程序仍在将电子邮件重定向到日志文件(说“已发送邮件:...”),而不是实际通过 SMTP。
编辑 #2:看起来电子邮件实际上已正确发送,它们也恰好打印到日志中。我正在尝试使用
sanitize_email
gem 将邮件重定向到另一个地址,这似乎不起作用,这就是为什么我认为电子邮件不会发出。所以我认为这解决了我的问题,尽管我仍然很好奇 ActionMailer 的设置中控制是发送电子邮件、记录到日志文件还是两者兼而有之。编辑 #3:
sanitize_email
的问题归结为我需要将新的暂存环境添加到 ActionMailer::Base.local_environments
.我将保持这个问题的开放性,看看是否有人可以回答我的最后一个问题(是什么决定了 ActionMailer 的电子邮件是发送出去、记录到日志文件还是两者兼而有之?)
最佳答案
关于您的第三次编辑,日志记录是您为应用程序本身设置的日志级别的函数,而不是 ActionMailer
中的任何特定设置.
在 Rails 2.3 中,ActionMailer::Base
只需将电子邮件发送到已配置的任何记录器(如果有)。收件人发送到 info
日志并将电子邮件正文发送到 debug
日志。 (评论是我的。其余的直接来自源代码。)
def deliver!(mail = @mail)
raise "no mail object available for delivery!" unless mail
#
# Logging happens first (or not)
#
unless logger.nil?
logger.info "Sent mail to #{Array(recipients).join(', ')}"
logger.debug "\n#{mail.encoded}"
end
#
# And then we decide if an email should really get sent
#
begin
__send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
rescue Exception => e # Net::SMTP errors or sendmail pipe errors
raise e if raise_delivery_errors
end
return mail
end
您的
environment.rb
或 staging.rb
文件应该有一行控制日志级别。类似于以下内容:config.log_level = :debug
这与您已经找到的邮件程序配置完全分开,后者控制是否发送电子邮件。
config.action_mailer.perform_deliveries = true
关于ruby-on-rails - 控制不同环境中电子邮件的日志记录和发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4599959/