Getting Pony or Mail gems to work with Sinatra / Heroku

2.8k Views Asked by At

I'm a Ruby newbie so apologies if this question is simple. And many thanks in advance for your help.

We have a Sinatra application that's been deployed onto Heroku. We're trying to add a page that sends a simple email. I've added the SendGrid add-on to the Heroku app. Now, I'm just trying to add the Ruby code that creates and sends the message using the SendGrid SMTP server information.

The problem I'm having is that even though I installed the Mail gem on the Heroku app (by adding to the Gemfile), I get an error when the Heroku app is launched complaining that 'treetop/runtime' is not installed:

←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:75:in `require': no such file to load -- treetop/runtime (LoadError)
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:75:in `rescue in block in <module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:69:in `block in <module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:68:in `each'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:68:in `<module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:2:in `<top (required)>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /usr/ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in `require'

Similarly, when I try to install the Pony gem, it complains that it can't load Mail. Oddly, this is all works on my local system, so I think it's a problem with Heroku. I just can't get Heroku to fully load everything it needs from either Pony or Mail to launch successfully. (Note: I can't even get Heroku to launch, so I can't even test the actual sending of the email code.)

Any specific help/insight would be greatly appreciated. Has anyone encountered this with Heroku? Are there other gems that might work for this simple purpose?

Thanks!

P.S. The bundle successfully installed mail (2.4.4) and pony (1.4).

2

There are 2 best solutions below

0
On

It would be helpful to see code snippets for your primary routing ruby script. But for my sinatra app, I configured Mail configs as.

Mail.defaults do
  delivery_method :smtp, {
    :address => 'smtp.sendgrid.net',
    :port => 587,
    :domain => 'heroku.com',
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :authentication => 'plain',
    :enable_starttls_auto => true
  }
end

And in my sending the contact email

mail = Mail.deliver do
    to <youremail>
    from <someone>
    subject 'Feedback for my Sintra app'
    text_part do
        body <your feedback>
    end
end

Hope this helps.

0
On

In case you want some settings for Pony instead of Mail (they're very similar):

configure :production do # I have a different hash for development settings.
  email_options, {      
    :from => "[email protected]",
    :via => :smtp,
    :via_options => {
      :address => 'smtp.sendgrid.net',
      :port => '587',
      :domain => 'heroku.com',
      :user_name => ENV['SENDGRID_USERNAME'],
      :password => ENV['SENDGRID_PASSWORD'],
      :authentication => :plain,
      :enable_starttls_auto => true
    },
  }
end

Pony.options = settings.email_options

Make sure you've set the sendgrid username and password via the config settings e.g.

heroku config:add SENDGRID_USERNAME=GIVEN_USERNAME SENDGRID_PASSWORD=GIVEN_PASSWORD

To send is very straightforward, just call Pony.mail and pass it a hash with 'to' and 'body' etc. (I'm sure you know this but I'm just trying to be thorough!)

I also use v1.4 but haven't had the same problem, but if it's saying it can't load Mail when you've switched to Pony I'd check for a rogue require statement.