rubyformsemailsinatrapony

Sending mail with Pony and Sinatra


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?


Solution

  • 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>