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 MAIL_TO=report@myapplicationdomain.com

Here is the code from my mailer

class StatusMailer < ActionMailer::Base
  default from: "reports@myapplicationdomain.com"
  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 Answers2

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?

Jordan Allan
  • 4,408
  • 7
  • 32
  • 35
  • 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 : 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' – Eric M. Seitz Jan 31 '15 at 14:28
  • I think the problem is with this: export MAIL_TO=report@myapplicationdomian.com. I am uncertain what email address should be used there, and why? – Eric M. Seitz Jan 31 '15 at 14:34
  • Were you able to properly authenticate? – Jordan Allan Feb 06 '15 at 23:09
  • Yes I was. This post got me there: http://stackoverflow.com/questions/8782274/sendgrid-email-sending-issues-in-ruby-on-rails-hosted-on-heroku. Thank you so much for the help. Using the environments file for mail settings was the key. – Eric M. Seitz Feb 11 '15 at 17:25
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.

Community
  • 1
  • 1