Skip to content

Instantly share code, notes, and snippets.

@rrmartins
Last active December 12, 2015 06:38
Show Gist options
  • Select an option

  • Save rrmartins/4731068 to your computer and use it in GitHub Desktop.

Select an option

Save rrmartins/4731068 to your computer and use it in GitHub Desktop.
The case is that I'm trying to make a contact form, which has an outgoing e-mail, and when the form is submitted it does not arrive in the post '/contact' of the file app.rb.
# encoding: utf-8
require 'sinatra/base'
require 'haml'
require 'sass'
require 'mail'
require File.expand_path('../nesta', File.dirname(__FILE__))
require File.expand_path('env', File.dirname(__FILE__))
require File.expand_path('cache', File.dirname(__FILE__))
require File.expand_path('config', File.dirname(__FILE__))
require File.expand_path('models', File.dirname(__FILE__))
require File.expand_path('helpers', File.dirname(__FILE__))
require File.expand_path('navigation', File.dirname(__FILE__))
require File.expand_path('overrides', File.dirname(__FILE__))
require File.expand_path('path', File.dirname(__FILE__))
Encoding.default_external = 'utf-8' if RUBY_VERSION =~ /^1.9/
configure :development do
set :mail_delivery_method, :smtp
set :mail_settings, {
:address => 'smtp.corp.myfreecomm.com.br',
:port => 25,
:authentication => :plain,
:enable_starttls_auto => true
}
set :mail_to, "Equipe MyFinance <[email protected]>"
end
module Nesta
class App < Sinatra::Base
register Sinatra::Cache
set :root, Nesta::Env.root
set :views, File.expand_path('../../views', File.dirname(__FILE__))
set :cache_enabled, Config.cache
set :haml, { :format => :html5 }
helpers Overrides::Renderers
helpers Navigation::Renderers
helpers View::Helpers
before do
if request.path_info =~ Regexp.new('./$')
redirect to(request.path_info.sub(Regexp.new('/$'), ''))
end
end
not_found do
set_common_variables
haml(:not_found)
end
error do
set_common_variables
haml(:error)
end unless Nesta::App.development?
Overrides.load_local_app
Overrides.load_theme_app
get '/robots.txt' do
content_type 'text/plain', :charset => 'utf-8'
<<-EOF
# robots.txt
# See http://en.wikipedia.org/wiki/Robots_exclusion_standard
EOF
end
get '/css/:sheet.css' do
content_type 'text/css', :charset => 'utf-8'
cache stylesheet(params[:sheet].to_sym)
end
get %r{/attachments/([\w/.-]+)} do |file|
file = File.join(Nesta::Config.attachment_path, params[:captures].first)
if file =~ /\.\.\//
not_found
else
send_file(file, :disposition => nil)
end
end
get '/articles.xml' do
content_type :xml, :charset => 'utf-8'
set_from_config(:title, :subtitle, :author)
@articles = Page.find_articles.select { |a| a.date }[0..9]
cache haml(:atom, :format => :xhtml, :layout => false)
end
get '/sitemap.xml' do
content_type :xml, :charset => 'utf-8'
@pages = Page.find_all
@last = @pages.map { |page| page.last_modified }.inject do |latest, page|
(page > latest) ? page : latest
end
cache haml(:sitemap, :format => :xhtml, :layout => false)
end
get '*' do
set_common_variables
parts = params[:splat].map { |p| p.sub(/\/$/, '') }
@page = Nesta::Page.find_by_path(File.join(parts))
raise Sinatra::NotFound if @page.nil?
@title = @page.title
set_from_page(:description, :keywords)
cache haml(@page.template, :layout => @page.layout)
end
post '/contato' do
puts "*"*80
puts "Aqui"
puts "*"*80
person = params[:person_type]
email = params[:email]
phone = params[:phone_number]
body = params[:body].to_s.gsub(/<\/?[^>]*>/, "")
title = person == "Pessoa Física" ? "Nome" : "Razão Social"
person_type = person == "Pessoa Física" ? params[:name] : params[:company_name]
is_spam = params[:ocult_body].to_s != ''
unless is_spam
mail = Mail.new do
from "#{person_type} <#{email}>"
to settings.mail_to
subject "Contato #{person} - MyFinance (#{Time.now})"
html_part do
content_type 'text/html; charset=UTF-8'
body "<p><strong>#{title}:</strong> #{person_type}</p>
<p><strong>Email:</strong> #{email}</p>
<p><strong>Telefone:</strong> #{phone}</p>
<p><strong>Mensagem:</strong> #{body}</p>"
end
end
mail.delivery_method settings.mail_delivery_method, settings.mail_settings
mail.deliver!
end
redirect "/contato?sent=true#{'&spam=true' if is_spam}"
end
end
end
Nesta::Plugin.load_local_plugins
Nesta::Plugin.initialize_plugins
Title: Contato - MyFinance
Template: page
.notIndex.pagePlans.content
%h2 Nós queremos ouvir você!
- if params[:sent] == 'true'
.submitSuccess
%p Agradecemos seu contato!
%p Brevemente nossa equipe de atendimento retornará sua mensagem.
%p Equipe MyFinance.
.contact
%form#contact_form{:action => "/contato", :method => "post"}
%fieldset
%legend Fale Conosco
%p
Para atendimento comercial relacionado a novos negócios e parcerias, <br />
preencha nosso contato comercial abaixo:
%label{:for => "person_type"} Pessoa:
.checkbox
%input#person_juridical{:checked => "checked", :name => "person_type", :tabindex => "1", :type => "radio", :value => "Pessoa Jurídica"}
%label{:for => "person_juridical"} Jurídica
%input#person_natural{:name => "person_type", :tabindex => "2", :type => "radio", :value => "Pessoa Física"}
%label{:for => "person_natural"} Física
%p.name
%label{:for => "name"}
Nome:
%em *
%input#name{:name => "name", :required => "", :tabindex => "3", :type => "text"}
%p.company_name
%label{:for => "company_name"}
Razão Social:
%em *
%input#company_name{:name => "company_name", :required => "", :tabindex => "4", :type => "text"}
%p
%label{:for => "email"}
Email:
%em *
%input#email{:name => "email", :required => "", :tabindex => "5", :type => "email"}
%p
%label{:for => "phone_number"} Telefone:
%input#phone_number{:name => "phone_number", :tabindex => "6", :type => "tel"}
%p
%label{:for => "body"}
Mensagem:
%em *
%textarea#body{:name => "body", :required => "", :tabindex => "7"}
%p.ocult_message
%label{:for => "body"}
Mensagem oculta:
%em *
%textarea#ocult_body{:name => "ocult_body"}
%p
%input.btn{:tabindex => "7", :type => "submit", :value => "Enviar"}
%p
%em *
Todos os campos são obrigatórios
%a{:href => "http://suporte.myfreecomm.com.br/categories/8978-myfinance", :target => "_blank"}
%img{:alt => "Entre em contato com o nosso suporte e tire suas dúvidas.", :height => "645", :src => "/images/banner-support.png", :width => "260"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment