rubyemailherokusinatrapony

Connection refused when using Pony Mail & Sinatra


I am having a lot of trouble getting the Pony Mail gem to work with my Sinatra website (deployed on Heroku). I am aiming to have a basic contact form where a visitor can fill in an email and a subject, their email and phone number and this form will send the information to the clients e-mail.

Here is the code for the form:

<form action="/emailform" method="post">
<p>We'd love to hear from you!</p>
<input type="text" placeholder="Your Full Name" name="full_name">
<input type="text" placeholder="Your E-mail        *required" name="email">
<input type="text" placeholder="Your Phone Number" name="phonenumber">
<input type="text" placeholder="Subject" name="subject">
<textarea rows="5" cols="30" name="comments" placeholder="Comments"></textarea>

<input type="submit" value="Submit" placeholder="Send">
</form>

Here is the code for my actions.rb file (the POST request) (3 dots represent more params/ form fields)

post '/emailform' do
Pony.mail to: 'xxx@gmail.com',
        from: 'xxx@gmail.com',
        subject: 'e-mail from: ' + params[:full_name] + params[:email],
        body: "subject: " + params[:subject] + "phone-number: " + params[:phonenumber]...
redirect '/'

end

post '/homeevaluation' do
  Pony.mail to: 'xxx@gmail.com',
        from: 'xxx@gmail.com',
        subject: 'Home Evaluation E-mail from: ' + params[:full_name],
        body: 'phone number: ' + params[:phonenumber] + params[:comments]...
redirect '/'
end

The error I'm getting right now is

Errno::ECONNREFUSED - Connection refused - connect(2):

I am seeing this by viewing the Heroku logs.

I have been struggling with this issue for a while and can't find any useful information online. If anyone has used the gem or knows of another way to accomplish the same task i'd love to hear it!


Solution

  • This looks like a setup issue. Are you using some kind of Heroku addon for e-mail? We use Sendgrid so we need these lines somewhere in the code:

    Pony.options[:via_options][:user_name] = ENV['SENDGRID_PASSWORD']
    Pony.options[:via_options][:password]  = ENV['SENDGRID_USERNAME']
    

    Edit: More verbose options

    Pony.options = {
      via: :smtp,
      via_options: {
        address:              "smtp.sendgrid.net",
        port:                 "587",
        domain:               "heroku.com",
        authentication:       :plain,
        enable_starttls_auto: true,
        user_name:            "foo@heroku.com",
        password:             "foo"
      }
    }