Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails SendGrid Email From Development Environment

We use SendGrid in a production app and it works fine. We were recently trying to test a new feature/email in development however and cannot seem to get an email to send. Any idea where we're going wrong? We are using similar features to production and we also followed SendGrid's implementation guide. Feels like I'm missing something simple!

First I exported the SENDGRID_USERNAME and SENDGRID_PASSWORD and for kicks added it to my .bash_profile

export SENDGRID_USERNAME=xxxxxxx
export SENDGRID_PASSWORD=xxxxxxx

I've confirmed in the console that these exist and are correct.

Created a developer_email.html.erb file:

<p>Hi! Sendgrid test</p>

And a DeveloperMailer file:

class DeveloperMailer < ActionMailer::Base
  default from: "[email protected]"

  def developer_email(developer_id)
    @recipients = ["[email protected]"] 
    mail(to: @recipients, subject: 'Does sendgrid work?')
  end
end

Updated the development.rb file:

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
  address:              'smtp.sendgrid.net',
  port:                 '587',
  domain:               'localhost:3000',
  user_name:            ENV['SENDGRID_USERNAME'],
  password:             ENV['SENDGRID_PASSWORD'],
  authentication:       :plain,
  enable_starttls_auto: true  } 

When I go to send the email in the console, it acts like it sent, but the email never actually arrives:

DeveloperMailer.developer_email(1) #to send the email. Seems to work:

2.3.1 :001 > DeveloperMailer.developer_email(1)
  Rendered developer_mailer/developer_email.html.erb (1.5ms)

DeveloperMailer#developer_email: processed outbound mail in 133.3ms
 => #<Mail::Message:70263824429080, Multipart: false, Headers: <From: [email protected]>, <To: ["[email protected]"]>, <Subject: Does SendGrid Work?>, <Mime-Version: 1.0>, <Content-Type: text/html>> 

#But I never get anything sent to my email

Any idea what I might be missing?

EDIT

Updated development.rb file:

config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
  address:              'smtp.sendgrid.net',
  domain:               'example.com',
  user_name:            ENV['SENDGRID_USERNAME'],
  password:             ENV['SENDGRID_PASSWORD'],
  authentication:       :plain,
  enable_starttls_auto: true  } 

Still no email though.

like image 691
Tom Hammond Avatar asked Jun 28 '26 13:06

Tom Hammond


1 Answers

You'd need to verify the sender email or domain of the sender email before you can send an email with SendGrid. See https://sendgrid.com/docs/ui/sending-email/sender-verification/ and https://sendgrid.com/docs/for-developers/sending-email/sender-identity/#domain-authentication

like image 126
Joshua Ugba Avatar answered Jul 01 '26 06:07

Joshua Ugba