1
votes

My app has a contact page where users can input their name/email/subject/content and send me a message. I'm using the Sendgrid addon to heroku and think I've done almost everything right. I've got this in my

emails_controller.rb

if @email.save
  ContactMailer.contact_message(@email).deliver
  flash.now[:success] = "Your email has sent! I'll try to get back to you shortly."
  render :new
else
  flash.now[:error] = "Please correct the highlighted errors and try again."
  render :new
end

(the idea here is to keep a record of each email and also send it to me).

My config/initializers/mail.rb

# SendGrid Settings
ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :enable_starttls_auto => true,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => 'myappname.com'
}
ActionMailer::Base.delivery_method = :smtp

Then, for good measure, following what someone else did in a sendgrid support question. config/environments/production.rb:

config.action_mailer.default_url_options = { :host => 'myappname.com' }

My contact_email.html.erb:

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

  def contact_message(email)
    @name = email.name
    @address = email.address
    @subject = email.subject
    @content = email.content
    mail(:to => ENV['EMAIL_ADDRESS'], :subject => @subject)
  end

end

I added all the necessary environmental variables through

heroku config:add [email protected]
heroku config:add SENDGRID_USERNAME=blah

When I push to heroku and try to send myself an email, though, it gives me the "We're sorry. Something went wrong" screen of death, and the logs are (to my eyes) totally useless.

Here's the log starting from when I last pushed to heroku and tried to send myself an email. Any ideas what's possibly going wrong?

2013-02-03T20:34:18+00:00 heroku[router]: at=info method=GET path=/ host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=0ms connect=7ms service=201ms status=304 bytes=0
2013-02-03T20:34:18+00:00 heroku[router]: at=info method=GET path=/assets/application-cd9c41b78562d03bb04bcaaa585b31e8.js host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=0ms connect=1ms service=11ms status=200 bytes=125507
2013-02-03T20:34:19+00:00 heroku[router]: at=info method=GET path=/assets/pig3-b2d5155bd2811a87cafec6a447459580.png host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=0ms connect=1ms service=4ms status=200 bytes=3010
2013-02-03T20:34:19+00:00 heroku[router]: at=info method=GET path=/assets/github-e8c3b3f49b0cc737afe9b3cd6e9ab159.png host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=0ms connect=1ms service=4ms status=200 bytes=3687
2013-02-03T20:34:19+00:00 heroku[router]: at=info method=GET path=/assets/odesk2-38dadc2e6b0e94aa6c6766df301716bc.png host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=1ms connect=2ms service=4ms status=200 bytes=3932
2013-02-03T20:34:19+00:00 heroku[router]: at=info method=GET path=/assets/star-02718a47b29575c2d7a0e75ac8bebf4d.png host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=0ms connect=2ms service=4ms status=200 bytes=3104
2013-02-03T20:34:19+00:00 heroku[router]: at=info method=GET path=/assets/empty_star-9bf002077eafec40cf239068f7d4d1ca.png host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=0ms connect=1ms service=43ms status=200 bytes=3072
2013-02-03T20:34:23+00:00 heroku[router]: at=info method=GET path=/emails/new host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=0ms connect=1ms service=119ms status=200 bytes=3022
2013-02-03T20:34:23+00:00 heroku[router]: at=info method=GET path=/assets/application-74fd2354261a1131b26861b47bef1d87.css host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=0ms connect=2ms service=4ms status=304 bytes=0
2013-02-03T20:34:19+00:00 heroku[router]: at=info method=GET path=/assets/headshot-767c8913c7af4fc97cb8e15881884f0e.png host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=4ms connect=6ms service=11ms status=200 bytes=146892
2013-02-03T20:34:39+00:00 heroku[router]: at=info method=POST path=/emails host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=0ms connect=7ms service=131ms status=500 bytes=643
2013-02-03T20:34:39+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=<appname>-2591.herokuapp.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=1ms connect=3ms service=5ms status=200 bytes=0

Also, I DID add Sendgrid (followed Heroku's instructions), and the Sendgrid user interface shows no emails sent. And the last time I tried this, the following line seemed to be the only one in the heroku logs that was relevantly timestamped when I tried to send another message. Not very helpful:

2013-02-04T18:15:45+00:00 heroku[router]: at=info method=GET path=/contact host=myappname.com fwd=69.181.104.85 dyno=web.1 queue=0 wait=0ms connect=2ms service=334ms status=200 bytes=3695
2

2 Answers

1
votes

Since it's not showing up on your SendGrid dashboard, my guess would be that your credentials are wrong in the app. If you type heroku config do you see that SENDGRID_USERNAME and SENDGRID_PASSWORD are set correctly?

Also, you might want to try setting delivery errors to display in your config file.

config.action_mailer.raise_delivery_errors = true
0
votes

Just for a bit of clarification, you can't access the Account Settings on the SendGrid site because Heroku has locked it down. You adjust those same settings from the Heroku interface directly.