ruby-on-rails - 使用环境变量配置 Redmine 电子邮件

标签 ruby-on-rails email environment-variables redmine

我已经根据redmine wiki中的说明在redmine上配置了电子邮件,一切正常。以下是我的 config/configuration.yml 文件的内容:

production:
delivery_method: :smtp
 smtp_settings:
    address: "smtp.sendgrid.net"
    port: 587
    authentication: :plain
    domain: "heroku.com"
    user_name: my_email@gmail.com
    password: my_password 

但是我正在尝试使用环境变量代替 my_email@gmail.com 和 my_password,如下所示:

production:
  delivery_method: :smtp
  smtp_settings:
    address: "smtp.sendgrid.net"
    port: 587
    authentication: :plain
    domain: "heroku.com"
    user_name: <%= ENV['SENDGRID_USERNAME'] %>
    password: <%= ENV['SENDGRID_PASSWORD'] %>

当我尝试使用配置文件中的环境变量发送测试电子邮件时,redmine 给出此错误:

'An error occurred while sending mail (535 Authentication failed: Bad username / password )'.

所以我猜 erb 片段没有被评估(我已经仔细检查了环境变量的值)。

我已经搜索了一个解决方案,但一无所获,有人对我应该如何在 redmine 中配置电子邮件有任何建议,这样我就不会在配置文件中公开我的 sendgrid 凭据吗?

或者,如果有人可以告诉我在没有环境变量的情况下直接使用凭据没有安全风险,那也可以解决我的问题。

最佳答案

我2周前回答过这个问题,马上就被别人删了。 所以我不确定是否有人会再次删除这个答案以防止其他人知道如何解决这个问题。祝你好运!

我遇到了同样的问题和这篇文章 Set up mailing in redmine on heroku解决了我的问题。

想法是将设置从 config/configuration.yml 移至 config/environments/production.rb 并使用 Ruby 代码进行设置。由于 ENV['key'] 是由 erb 处理的,我猜 configuration.yml 不是这样处理的。可能存在一些安全问题?

因此在您的情况下,您应该将这些代码添加到 config/environments/production.rb 中,如下所示,

ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp

然后从 config/configuration.yml 中删除代码并使其看起来像这样,

default:
  # Outgoing emails configuration (see examples above)
  email_delivery:
    delivery_method: :smtp

关于ruby-on-rails - 使用环境变量配置 Redmine 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15045369/

相关文章:

从 Gmail 回复时,电子邮件签名成为附件

php - 通过 PHPMailer 发送电子邮件时出现身份验证错误

vue.js - 如何设置 Vue 环境变量而不将它们包含在 'build' 之后的 JS 文件中?

c++ - 有没有办法弄清楚可执行文件需要/使用哪些环境变量?

ruby-on-rails - 如何在查询中添加uniq

jquery - Bootstrap 图片库,作者:blueimp : How to disable the wheel handler and keyboard handler

ruby-on-rails - 检查关系是否存在 has_one mongoid

ruby-on-rails - 当页面加载时间过长时,如何停止页面加载并关闭 Ruby Watir 中的浏览器?

javascript - 使用 PHP 通过电子邮件发送表单数据

environment-variables - 导出 AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY 的替代方案