Rails 4 Sendgrid sending error - invalid senders not allowed

I have built-in Sendgrid options on the Rails 4 server. These settings are great for the development environment. But this gives an error in the production environment.

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

configurations / Initializers / 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 } 

configurations / Initializers / devise.rb

 config.mailer_sender = ' simmi@mydomain.com ' 

configurations / environment / production.rb

 # Default URL config.action_mailer.default_url_options = { host: 'mysite.mydomain.com' } DOMAIN = 'mysite.mydomain.com' 
+6
source share
4 answers

According to the sendgrid support team, this error occurs when the username or password is incorrect. I tried to manually enter the smtp server via telnet and it worked.

On the command line of my server, I performed the following steps:

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

Link for converting text to Base64 - http://www.opinionatedgeek.com/dotnet/tools/base64encode/

ENV variables somehow did not work on my production environment. As a workaround, I tried adding the username and password directly, and it worked.

+7
source

I also ran into the same problem and fixed it by adding the following:

configurations / 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 

The letter_opener letter is very useful if you want to test sending emails in design mode. If you want to overwrite letter_opener, add the following configuration

configurations / environment / development.rb

 ActionMailer::Base.delivery_method= :letter_opener 

And also add the port in ActionMailer :: Base.smtp_settings.

+2
source

You may be loading your environment variables after you try to initialize your mail program. You can initialize immediately after loading your variables to make sure they exist.

Set up a configuration file with your username and password variables:

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

Set up the initialization file:

 # 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 
+1
source

If your production environment is Heroku:

Log in to your Heroku account and select the application. In the "Settings" section, click the "Reveal Config Vars" button. Enter sendgrid value pairs in your keys and then send. Run: heroku restart .

0
source

Source: https://habr.com/ru/post/972582/


All Articles