-
-
Save PatTheSilent/5147705 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See blog post at http://vitobotta.com/sinatra-contact-form-jekyll/ | |
%w(rubygems sinatra liquid active_support/secure_random resolv open-uri pony haml).each{ |g| require g } | |
APP_ROOT = File.join(File.dirname(__FILE__), '..') | |
set :root, APP_ROOT | |
set :views, File.join(APP_ROOT, "_layouts") | |
not_found do | |
status 404 | |
not_found_template = File.join(APP_ROOT, "public", "404.html") | |
File.exists?(not_found_template) ? File.read(not_found_template) : "Oops, the page you are looking for does not exist :(" | |
end | |
def valid_email?(email) | |
if email =~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/ | |
domain = email.match(/\@(.+)/)[1] | |
Resolv::DNS.open do |dns| | |
@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX) | |
end | |
@mx.size > 0 ? true : false | |
else | |
false | |
end | |
end | |
def valid_captcha? answer | |
open("http://captchator.com/captcha/check_answer/#{@captcha_id}/#{answer}").read.to_i.nonzero? rescue false | |
end | |
def valid_url? url | |
return true if url == "http://" | |
!(url =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix).nil? | |
end | |
def given? field | |
!field.empty? | |
end | |
def validate params | |
errors = {} | |
[:name, :email, :website, :message, :captcha].each{|key| params[key] = (params[key] || "").strip } | |
errors[:name] = "This field is required" unless given? params[:name] | |
if given? params[:email] | |
errors[:email] = "Please enter a valid email address" unless valid_email? params[:email] | |
else | |
errors[:email] = "This field is required" | |
end | |
errors[:website] = "Please enter a valid web address" unless valid_url? params[:website] | |
errors[:message] = "This field is required" unless given? params[:message] | |
if given? params[:captcha] | |
errors[:captcha] = "The code you've entered is not valid" unless valid_captcha? params[:captcha] | |
else | |
errors[:captcha] = "Please enter the code as shown in the picture" | |
end | |
errors | |
end | |
def send_email params, ip_address | |
email_template = <<-EOS | |
Sent via http://vitobotta.com/contact/ | |
When: {{ when }} | |
IP address: {{ ip_address }} | |
Your name: {{ name }} | |
Email: {{ email }} | |
Website: {{ website }} | |
Message: | |
{{ message }} | |
EOS | |
body = Liquid::Template.parse(email_template).render "name" => params[:name], | |
"email" => params[:email], | |
"website" => params[:website], | |
"message" => params[:message], | |
"when" => Time.now.strftime("%b %e, %Y %H:%M:%S %Z"), | |
"ip_address" => ip_address | |
Pony.mail(:to => "destination email address", :from => params[:email], :subject => "A comment from #{params[:name]}", :body => body) | |
end | |
get '/contact-form/?' do | |
@captcha_id, @errors, @values, @sent = ActiveSupport::SecureRandom.hex(16), {}, {}, false | |
haml :contact_form | |
end | |
post '/contact-form/?' do | |
@captcha_id = (params[:captcha_id] || ActiveSupport::SecureRandom.hex(16)) | |
@errors = validate(params) | |
@values = params | |
if @errors.empty? | |
begin | |
send_email(params, @env['REMOTE_ADDR']) | |
@sent = true | |
rescue Exception => e | |
puts e | |
@failure = "Ooops, it looks like something went wrong while attempting to send your email. Mind trying again now or later? :)" | |
end | |
end | |
haml :contact_form | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment