Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 Sendgrid integration giving error - Unauthenticated senders not allowed

I have integrated Sendgrid settings on a Rails 4 server. These settings work fine for development environment. But this is giving error on production environment.

Net::SMTPFatalError (550 Cannot receive from specified address <[email protected]>: Unauthenticated senders not allowed)

config/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
}

config/initializers/devise.rb

config.mailer_sender = '[email protected]'

config/environments/production.rb

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

DOMAIN = 'mysite.mydomain.com'
like image 710
Simmi Badhan Avatar asked Jul 21 '14 12:07

Simmi Badhan


3 Answers

According to sendgrid support team, this error comes when username or password are incorrect. I tried logging manually into the smtp server through telnet and it was working.

On my server commandline, I followed these steps:

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

Link to convert text into Base64 - http://www.opinionatedgeek.com/dotnet/tools/base64encode/

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

like image 70
Simmi Badhan Avatar answered Nov 15 '22 11:11

Simmi Badhan


I have also faced the same problem and fixed it by adding the following:

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

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

config/environments/development.rb

ActionMailer::Base.delivery_method= :letter_opener

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

like image 39
user1099939 Avatar answered Nov 15 '22 09:11

user1099939


You are probably loading your environment variables after you are trying to initialize your mailer. You can do the initialization directly after loading your variables to be sure that they exist.

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

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

Set up an initializer 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
like image 45
littleforest Avatar answered Nov 15 '22 10:11

littleforest