【问题标题】:sendgrid username and password environment variables are not read when action mailer is executed执行 action mailer 时不读取 sendgrid 用户名和密码环境变量
【发布时间】:2018-06-16 18:28:21
【问题描述】:

预期:

telnet smtp.sendgrid.net 587 EHLO AUTH LOGIN Enter username in Base64 Enter password in Base64

235 Authentication successful

实际:

通过本地主机

config/environments/development.rb

config.action_mailer.raise_delivery_errors = true

config/environment.rb

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

控制器

NotificationMailer.notification_email(@admin_email, @item).deliver

sendgrid.env

export SENDGRID_USERNAME='apikey'
export SENDGRID_PASSWORD='the api key that was provided during the smtp setup'

现在可以了

source ./sendgrid.env

我检查了 ENV,它显示了用户名/密码。

rails c
NotificationMailer.notification_email(@admin_email, @item).deliver

我看到电子邮件被记录了,但我收到了

Net::SMTPFatalError: 550 Unauthenticated senders not allowed

如果我对 env 变量进行硬编码,则身份验证确实有效。所以我不明白为什么 ENV 变量没有进入这个过程。

请指教

【问题讨论】:

    标签: ruby-on-rails smtp environment-variables sendgrid


    【解决方案1】:

    我使用 dontenv-rails gem 来解决这个问题:https://github.com/bkeepers/dotenv

    【讨论】:

    • 如果问题是关于在生产环境中运行,那您是正确的,但这不适用于所描述的 rails c 控制台命令,它应该继承环境变量。
    • 今天将研究 dotenv-rails 并相应更新答案。
    • 改进了我的答案@inopinatus
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2019-02-25
    • 2021-08-01
    相关资源
    最近更新 更多