ruby-on-rails - 控制不同环境中电子邮件的日志记录和发送

标签 ruby-on-rails email smtp actionmailer

在 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.rbstaging.rb文件应该有一行控制日志级别。类似于以下内容:
config.log_level = :debug

这与您已经找到的邮件程序配置完全分开,后者控制是否发送电子邮件。
config.action_mailer.perform_deliveries = true

关于ruby-on-rails - 控制不同环境中电子邮件的日志记录和发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4599959/

相关文章:

ruby-on-rails - 无法从 Rails Controller 呈现 JavaScript

html - 2007/10 年与其他所有内容相比,电子邮件图像未对齐

php - 我无法连接到 SMTP 主机

ios - MailCore 2 - 发送电子邮件 Excenge 服务器

ruby-on-rails - 如何在rails中迁移dbase数据库

ruby-on-rails - 跨三个模型的 ActiveRecord 查询?

ruby-on-rails - 使用 Mongoid 进行不区分大小写的排序

php - 使用网络电子邮件客户端进行项目管理

php - 如何在 php 中将 HTML 动态表作为邮件发送?

amazon-web-services - Amazon SES SMTP 连接超时