1

I am trying to send an email from a contact form (built in HTML) with the Pony Gem within sinatra, I have followed the docs but something must be missing.

This is the Pony config

 get '/contact' do
  erb :contact, :layout => :layout
 end

 post '/contact' do
 require 'pony'
 Pony.mail({
:from => params[:name],
    :to => 'myemailaddress',
    :subject => params[:name] + "has contacted you via the Website",
    :body => params[:comment],
    :via => :smtp,
    :via_options => {
     :address              => 'smtp.gmail.com',
     :port                 => '587',
     :enable_starttls_auto => true,
     :user_name            => 'myemailaddress',
     :password             => 'mypassword',
     :authentication       => :plain, 
     :domain               => "localhost.localdomain" 
     }
    })
    redirect '/success' 
   end


   get('/success') do
@notification = "Thanks for your email. I'll be in touch soon."
erb :index, :layout => :layout
   end

So after clicking submit the contact page gets re rendered with no message

here is my submit button

 <button type="submit" class="btn" value="send">Submit</button>

Am i missing a trigger here somewhere?

Richlewis
  • 15,070
  • 37
  • 122
  • 283

1 Answers1

1

Are you sure you have the form setup to do a post? If it seems to be refreshing the page the form tag may not be setup properly. Also the button to submit should be an input tag of type submit. The HTML would need to look something like this:

<form action="/contact" method="post">
   <!-- your form elements go here -->

   <input type="submit" value="Sign in">
</form>
Batkins
  • 5,547
  • 1
  • 28
  • 27
  • you where right, silly error on my part not setting the action and post...Now it doesnt like:subject => params[:name] + "has contacted you via Website", it is throwing a nil class error regarding the + – Richlewis Feb 05 '13 at 15:17
  • Hmm. Seems like `params[:name]` could be `nil`. Make sure a form element with `name` set to `name`. Like so: ``. Also, if you want to prevent the `nil` pointer error regardless, you can do something like this `:subject => "#{params[:name] || 'Someone'} has contacted you via the Website"`. That will make it default to `Someone` if the `name` parameter is not passed in. – Batkins Feb 05 '13 at 15:30
  • thats exactly what it was i had type = text, id = name, got mixed up there, thanks again – Richlewis Feb 05 '13 at 15:34
  • All HTTP forms are automatically assumed to be `method='POST'` if the attribute is not specified. Forms will also POST to the same url if it's not specified, which makes defining routes a lot easier too. – ian Feb 06 '13 at 00:42