Rails 4 Sendgrid集成给出错误 - 不允许未经身份验证的发件人

时间:2014-07-21 12:52:41

标签: ruby-on-rails-4 sendgrid

我在Rails 4服务器上集成了Sendgrid设置。这些设置适用于开发环境。但这会给生产环境带来错误。

Net::SMTPFatalError (550 Cannot receive from specified address <simmi@mydomain.com>: Unauthenticated senders not allowed)

配置/初始化/ email_setup.rb

ActionMailer::Base.smtp_settings = {
   :address              => "smtp.sendgrid.net",
   :domain               => DOMAIN,
   :user_name            => ENV['SENDGRID_USERNAME'],
   :password             => ENV['SENDGRID_PASSWORD'],
   :authentication       => "plain",
   :enable_starttls_auto => true
}

配置/初始化/ devise.rb

config.mailer_sender = 'simmi@mydomain.com'

配置/环境/ production.rb

# Default URL
config.action_mailer.default_url_options = { host: 'mysite.mydomain.com' }

DOMAIN = 'mysite.mydomain.com'

4 个答案:

答案 0 :(得分:8)

根据sendgrid支持团队的说法,当用户名或密码不正确时会出现此错误。我尝试通过telnet手动登录到smtp服务器,它正在运行。

在我的服务器命令行上,我按照以下步骤操作:

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

将文字转换为Base64的链接 - http://www.opinionatedgeek.com/dotnet/tools/base64encode/

ENV变量在某种程度上不适用于我的生产环境。作为一种解决方法,我尝试直接添加用户名和密码,但它确实有效。

答案 1 :(得分:2)

我也遇到了同样的问题并通过添加以下内容来修复它:

到config / environment.rb

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.sendgrid.net",
  :domain               => DOMAIN,
  :user_name            => ENV['SENDGRID_USERNAME'],
  :password             => ENV['SENDGRID_PASSWORD'],
  :authentication       => "plain",
  :enable_starttls_auto => true
}
ActionMailer::Base.default_url_options = { host: 'mysite.mydomain.com' }

config / application.rb

ActionMailer::Base.delivery_method = :smtp

如果您想在开发模式下测试发送电子邮件,则letter_opener gem非常有用。 如果要覆盖letter_opener,请添加以下配置

配置/环境/ development.rb

ActionMailer::Base.delivery_method= :letter_opener

并在ActionMailer :: Base.smtp_settings。

下添加端口

答案 2 :(得分:1)

在尝试初始化邮件程序后,您可能正在加载环境变量。您可以在加载变量后直接进行初始化,以确保它们存在。

使用您的用户名和密码变量设置配置文件:

# config/mailer.yml
production:
   SENDGRID_USERNAME: 'username'
   SENDGRID_PASSWORD: 'password'

设置初始化文件:

# config/initializers/mailer.rb
if Rails.env.production?
  config_path = File.expand_path(Rails.root.to_s + '/config/mailer.yml')
  if File.exists? config_path
    ENV.update YAML.load_file(config_path)[Rails.env]
  end

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

答案 3 :(得分:-1)

如果您的生产环境是Heroku:

登录您的Heroku帐户并选择该应用程序。在&#34;设置&#34;下,点击&#34;显示配置变量&#34;按钮。输入您的sendgrid键和值对,然后提交。运行:hyphens:none;

相关问题