我从生产环境发送邮件时遇到错误,具体来说:
Net::SMTPAuthenticationError (530-5.5.1 Authentication Required)
在我的本地计算机和开发环境中,我使用 ENV[GMAIL_USERNAME]
和 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"]
}
不幸的是,在生产中我在发送邮件时遇到错误。但是,如果我在环境/生产.rb 中对用户名和密码进行硬编码,它就可以工作。 ENV["GMAIL_USERNAME"]
和 `ENV["GMAIL_PASSWORD"] 在 .bash_profile 中设置:
export GMAIL_USERNAME="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f098959c9c9fb09d89949f9d91999ede939f9d" rel="noreferrer noopener nofollow">[email protected]</a>"
export GMAIL_PASSWORD="mypassword"
我以为应用程序无法调用 ENV["GMAIL_USERNAME"]
和 ENV["GMAIL_PASSWORD"]
但当我跳入生产 Rails 控制台时并 put ENV["GMAIL_USERNAME"]
它会输出正确的凭据。
我已重新启动 Apache 并多次重新启动我的应用程序,但我不知道下一步该怎么做。
非常感谢任何帮助。
提前谢谢您。
最佳答案
谁的 bash 个人资料?你的用户? Apache 不会在启动时读取这些配置文件,因为它不会运行 bash 来启动服务器。当您登录到服务器时,您的配置文件就会被获取。这就是为什么它在测试时有效。
您需要确保脚本或apache运行的环境中定义了环境变量。根据您的服务器,这可能位于/etc/apache2/envvars 中或添加到 apache 的启动脚本中。
关于ruby-on-rails - 使用 ENV[USERNAME] 和 ENV[PASSWORD] 与 Gmail 一起使用 Rails 邮件程序 Net::SMTPAuthenticationError(需要 530-5.5.1 身份验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12901904/