Skip to content

Instantly share code, notes, and snippets.

@tomciopp
Created February 12, 2012 02:25
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomciopp/1805852 to your computer and use it in GitHub Desktop.
Save tomciopp/1805852 to your computer and use it in GitHub Desktop.
Sinatra contact form
-------------------------------
The code for the contact form
-------------------------------
<div id='contact-box' class="group">
<div class="span6 clear">
<form action="/" method="post" class="well">
<label for="name">Your Name:</label>
<input type="text" name="name" class="span4" placeholder="John Smith...">
<label for="email">Your email address:</label>
<input type="text" name="email" class="span4" placeholder="email@example.com">
<label for="to">Your message:</label>
<textarea name="message" cols='30' rows="10"></textarea><br>
<button type="submit" class="btn" value="Send">Submit</button>
</form>
</div>
</div>
------------------------------
In the base application file:
------------------------------
post '/' do
configure_pony
name = params[:name]
sender_email = params[:email]
message = params[:message]
logger.error params.inspect
begin
Pony.mail(
:from => "#{name}<#{sender_email}>",
:to => 'example@gmail.com',
:subject =>"#{name} has contacted you",
:body => "#{message}",
)
redirect '/success'
rescue
@exception = $!
erb :boom
end
end
def configure_pony
Pony.options = {
:via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => '587',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true,
:domain => 'heroku.com'
}
}
end
-------------
boom.erb is just a template that will show the exception message and the submitted params.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment