2

从生产环境发送邮件时出现错误,特别是:

Net::SMTPAuthenticationError (530-5.5.1 Authentication Required)

在我的本地机器和开发环境中,我使用ENV[GMAIL_USERNAME]and声明密码,ENV[GMAIL_PASSWORD]并且它工作正常,声明的 ENV 存储在我的 .bash_profile 中

  # Development Environment for Action Mailer config
  config.action_mailer.default_url_options = { host: '0.0.0.0:3000' }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.default charset: 'utf-8'
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    port: 587,
    domain: 'mydomain.com',
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: ENV["GMAIL_USERNAME"],
    password: ENV["GMAIL_PASSWORD"]
  }

  # Production Environment for Action Mailer config
  config.action_mailer.default_url_options = { host: 'mydomain.com' }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default charset: 'utf-8'
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: "mydomain.com",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: ENV["GMAIL_USERNAME"],
    password: ENV["GMAIL_PASSWORD"]
  }

不幸的是,在生产中发送邮件时出现错误。但是,如果我对其中的用户名和密码进行硬编码,environments/production.rb它就可以工作。和ENV["GMAIL_USERNAME"]`ENV["GMAIL_PASSWORD"] 在 .bash_profile 中设置:

export GMAIL_USERNAME="hello@mydomain.com"
export GMAIL_PASSWORD="mypassword"

我以为是应用程序无法调用ENV["GMAIL_USERNAME"]ENV["GMAIL_PASSWORD"]但是当我跳入生产 Rails 控制台并puts ENV["GMAIL_USERNAME"]输出正确的凭据时。

我已经重新启动了 Apache 并多次重新启动了我的应用程序,但我很困惑下一步该做什么。

任何帮助深表感谢。

提前谢谢你。

4

1 回答 1

3

谁的 bash 简介?你的用户?Apache 不会在启动时读取这些配置文件,因为它不会运行 bash 来启动服务器。当您登录到服务器时,您的配置文件就会被获取。这就是为什么它在您测试时起作用的原因。

您需要确保在脚本或 apache 运行的环境中定义了环境变量。根据您的服务器,这可能位于 /etc/apache2/envvars 中或添加到 apache 的启动脚本中。

于 2012-10-15T18:44:06.590 回答