3

I am building my first portfolio page with Sinatra.

I have a 'textbook' contact page with a straight-forward form containing 'name', 'email' and 'content' fields. When someone submits the form, I want to recieve an email notification.

Pony claims that it can send email via simple 'one-line' of code. I have read the Pony documentation but it is not very detailed in how to set it up.

I don't know if I am not setting it up properly, the code is not right, Pony is not the best tool, or if my development environment is not allowing the mail to be sent.

The code below is supposed to be sending an email from the post method, it is then saving the data to a PostgreSQL database via the save_message method. The data is being persisted correctly.

#server.rb
require 'sinatra'
require 'pony'
require_relative 'model/methods'

get '/contact' do
  erb :contact
end

post '/thankyou' do
  unless params[:name] == '' || params[:email] == '' || params[:content] == ''
    Pony.options = {
      :subject => "Portfolio page: Message delivery from #{params[:name]}",
      :body => "#{params[:content]}",
      :via => :smtp,
      :via_options => {
        :address              => 'smtp.1and1.com',
        :port                 =>  '587',
        :enable_starttls_auto => true,
        :user_name            => ENV["USER_EMAIL_ADDRESS"],
        :password             => ENV["SMTP_PASSWORD"],
        :authentication       => :login,
        :domain               => 'nterrafranca.com'
        }
      }
    Pony.mail(:to => ENV["DESTINATION_EMAIL_ADDRESS"])
    save_message(params[:name], params[:email], params[:content])
  end
  redirect '/'
end
NickTerrafranca
  • 109
  • 1
  • 8
  • Welcome to Stack Overflow. While it's nice to be very friendly and familiar in a question, that's really not the writing style to be used. Consider Stack Overflow to be a programming cookbook, community-edited similar to Wikipedia, where questions are problem statements in the cookbook, with their associated answers. Fluff isn't necessary, nor necessarily desired; Instead write well thought-out questions and you'll do well. – the Tin Man Feb 16 '15 at 19:44

2 Answers2

3

Pony needs to know how to send the email, not just who it's to, from, what the subject and body are, etc.

From the pony documentation, it will default to use sendmail, otherwise configures SMTP to use localhost. Depending on where this application is running, it's highly likely that sendmail is not available, and that there is no SMTP configured on localhost.

I've used Pony for several applications. Each one, I configure a "noreply@" email address for Pony to use to authenticate for SMTP, therefore using my own domain email (usually Google Apps, or even Gmail) for my SMTP connection. For example:

Pony.options = {
  :subject => "Some Subject",
  :body => "This is the body.",
  :via => :smtp,
  :via_options => {
    :address              => 'smtp.gmail.com',
    :port                 => '587',
    :enable_starttls_auto => true,
    :user_name            => 'noreply@cdubs-awesome-domain.com',
    :password             => ENV["SMTP_PASSWORD"],
    :authentication       => :plain, # :plain, :login, :cram_md5, no auth by default
    :domain               => "localhost.localdomain"
  }
}

In the case of a Sinatra app, I perform the exact above code (with the obvious substitutions) right before I call:

Pony.mail(:to => <some_email>)

I've configured Pony multiple times - comment if you still have issues and I'll be glad to help.

CDub
  • 13,146
  • 4
  • 51
  • 68
  • Thank you for the help! I am admittedly ignorant to the inner workings of email so I do have a couple of questions: Do I need to setup a domain to replace "noreply@cdubs-awesome-domain.com"? Currently I am using my-email-address@gmail.com. When I use noreply@gmail.com I get a "Username and Password not accepted" error in the browser. I am developing using OS X and I plan to deploy to Heroku. – NickTerrafranca Feb 17 '15 at 16:14
  • You can use your personal email credentials for Pony, but I'd highly recommend against it, as your personal email and password will be out in the ether, even if it is Heroku's config. – CDub Feb 17 '15 at 17:35
  • I set up an email with the 1and1, the company that I have my domaine with. I have gotten further down the path to success but it is not working yet. I spent a while on the phone with the folks at 1and1 to make sure that I have all of the proper settings. The error I am getting now is: Net::SMTPServerBusy at /thankyou 421 invalid sender domain 'unknown' (misconfigured dns?) http://postmaster.1and1.com/error-messages/#invalidsenderdomain – NickTerrafranca Feb 17 '15 at 21:03
  • Sounds like the `:domain` section of the `Pony.options` hash isn't configured correctly. Unfortunately without seeing more details, it'd be hard to troubleshoot. – CDub Feb 17 '15 at 22:40
  • What is :enable_starttls_aut doing? – NickTerrafranca Feb 17 '15 at 22:57
  • No idea off the top of my head. Quick Google turned up: http://thelucid.com/2011/01/17/rails-3-and-the-little-known-enable_starttls_auto-option/ – CDub Feb 17 '15 at 23:02
0

If you are using a gmail account with 2-step verification, you must generate an application specific password for the Pony mailer, and NOT use your usual SMTP password.

See https://support.google.com/accounts/answer/185833?hl=en

Insert the application specific password in the place of your usual password.

This is from the Pony project page on Github.

davedub
  • 94
  • 1
  • 8