Sending mail with Pony and Sinatra

筅森魡賤 提交于 2019-12-11 09:33:42

问题


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?


回答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>


来源:https://stackoverflow.com/questions/14709421/sending-mail-with-pony-and-sinatra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!