0

I would like to use Sendgrid to manage outgoing emails from a 3.2.2 version rails app I am developing with the help of a friend. She has email working from within the app using gmail, on her local/dev build. I need sendgrid up and running.

I cannot even get it to work locally.

From my development.rb file

config.action_mailer.default_url_options = { :host => 'localhost:3030' }
  
  config.action_mailer.smtp_settings = {
    :user_name      => ENV['EMAIL_USERNAME'],
    :password       => ENV['EMAIL_PASSWORD'],
    :domain         => 'myapplicationdomain.com',
    :address        => 'smtp.sendgrid.net',
    :port           => 587,
    :authentication => 'plain',
    :enable_starttls_auto => true
  }
  config.action_mailer.delivery_method = :smtp

Then I have a variable file in the root of my application that includes the following:

export EMAIL_USERNAME=sendgridusername
export EMAIL_PASSWORD=sendgridpassword
export [email protected]

Here is the code from my mailer

class StatusMailer < ActionMailer::Base
  default from: "[email protected]"
  def status_report(report)
      @greeting = "Hello"
      @report = report
      if ENV['MAIL_TO']
        email = ENV['MAIL_TO'] if ENV['MAIL_TO']
      else
        email = @report.user.email
      end
      @statuses = @report.statuses
      @reviewers = @report.user.reviewers
      bcc = []
      @reviewers.each do |reviewer|
        bcc.append(reviewer.email)
      end
      @bcc = bcc
      mail(to: email, bcc: bcc, subject: 'Status Report')
  end
end

Am I missing some other setting? What about the MAIL_TO field in the variable, I am not certain what that should be set to, or if it even needs to be declared.

Is there another file that I should be editing? I had this working several days ago, but functionality somehow slipped away :0

Rails server says that emails were sent, but sendgrid has no record; nor are the emails being received by addresses on the distribution list.

Thank you in advance for any assistance.

2 Answers 2

0

Do you have the following settings in your config/environments/development.rb?

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true

If not, add them to your config file and restart your server.

Update:

This error suggests that you're not authenticated. Are you sure the values of your ENV['EMAIL_USERNAME'] and ENV['EMAIL_PASSWORD'] variables are present/correct?

Sign up to request clarification or add additional context in comments.

4 Comments

Very helpful. Neither setting was there and I now have an error to deal with. Thank you! Net::SMTPFatalError in StatusesController#cast 550 Cannot receive from specified address <[email protected]>: Unauthenticated senders not allowed Rails.root: /vagrant/src/statuscaster2 Application Trace | Framework Trace | Full Trace app/models/report.rb:10:in email_report' app/controllers/statuses_controller.rb:120:in cast'
I think the problem is with this: export [email protected]. I am uncertain what email address should be used there, and why?
Were you able to properly authenticate?
Yes I was. This post got me there: stackoverflow.com/questions/8782274/…. Thank you so much for the help. Using the environments file for mail settings was the key.
0

This post: Sendgrid / email sending issues in Ruby on Rails (hosted on Heroku)

Got me up and running. The key being putting the SMTP and sendgrid information in the environment.rb file.

I can't explain exactly why that made the difference, but it did.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.