本文介绍了执行动作邮件程序时,不会读取sendgrid用户名和密码环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预期:

telnet smtp.sendgrid.net 587EHLOAUTH LOGINEnter username in Base64Enter password in Base64

telnet smtp.sendgrid.net 587EHLOAUTH LOGINEnter username in Base64Enter password in Base64

235 Authentication successful

实际:

通过本地主机

config/environments/development.rb

config/environments/development.rb

config.action_mailer.raise_delivery_errors = true

config/environment.rb

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

sendgrid.env

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

现在我要做

source ./sendgrid.env

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

I check the ENV, it's shows the username/password.

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

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

I see the email gets logged, but I get

Net::SMTPFatalError: 550 Unauthenticated senders not allowed

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

If I hardcode the env variables, the authentication does work. So I don't understand why the ENV variables are not getting into the process.

请告知

推荐答案

我使用dontenv-rails gem解决了此问题: https://github.com/bkeepers/dotenv

I've used the dontenv-rails gem to solve this issue: https://github.com/bkeepers/dotenv

这篇关于执行动作邮件程序时,不会读取sendgrid用户名和密码环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:20