Search code examples
ruby-on-railsherokusmtpdevelopment-environmentsendgrid

Why can't I see any emails in my inbox sent from my Heroku Rails app using SendGrid SMTP Relay in development mode?


I'm trying to switch over to SendGrid from Mandrill in my Rails 4.2 app through SendGrid's SMTP Relay. I have set the 'To Email' to be my personal email address so that I can view the emails that have been sent, however none of the emails actually appear in my inbox despite the rails console claiming to have processed and sent the email.

I am fairly certain all my mailers have the appropriate smtp settings as I have mostly followed the instructions provided on the SendGrid website: https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html

I have also tested my connectivity to SendGrid's SMTP Relay through telnet and the connection is succesful.

My SendGrid dashboard indicated that 0 emails have been sent. None of my emails appear under the Suppressions tab either so it's not like they have bounced or have been blocked.

This is in my config/environment.rb:

ActionMailer::Base.smtp_settings = {
:user_name => 'apikey',
:password => ENV['SENDGRID_API_KEY'],
:domain => 'heroku.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}

This is in my config/environments/development.rb:

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'smtp.sendgrid.net' }

This is the line in my controller that calls my ApplicationMailer:

ApplicationMailer.send_email(user, 'mypersonalemail@email.com', 'Test Subject').deliver

And this is what gets printed in the console when the mailer method is executed:

ApplicationMailer#send_email: processed outbound mail in 789.9ms
Sent mail to mypersonalemail@email.com (103.4ms)

But I still don't get any emails in my inbox or spam folder. Does anyone know how I can solve this? Thanks in advance.


Solution

  • Your domain and host options are wrong. Use localhost:3000 (unless you're using docker or something at which point replace localhost:3000 with 0.0.0.0:8000)

    #/environments/development.rb
      #Mailer Options
      config.action_mailer.delivery_method = :smtp
      config.action_mailer.perform_deliveries = true
      ActionMailer::Base.smtp_settings = {
        :address        => 'smtp.sendgrid.net',
        :port           => '587',
        :authentication => :plain,
        :user_name      => ENV['SENDGRID_USERNAME'],
        :password       => ENV['SENDGRID_PASSWORD'],
        :domain         => 'localhost:3000',
        :enable_starttls_auto => true
      }
      config.action_mailer.default_url_options = { host: 'http://localhost:3000' }
      config.action_mailer.asset_host = 'http://localhost:3000'
    

    Make sure to add the sendgrid credentials to your local machine as environment vars. To get them, go to your heroku app and click on settings, then "reveal config vars". Then add those sendgrid credentials to your local machine as env. vars and you're done.