Sending mail with Pony and Sinatra - ruby

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?

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>

Related

upload image and files padrino

i want to put index image to my blog's posts
and have an upload form in 'new post 'in admin's panel
the form is written like this :
- error = #post.errors.include?(:file)
%fieldset.control-group{:class => error ? 'has-error' : ''}
=f.label :file, :class => 'control-label'
.controls
=f.file_field :file ,:name => 'file'
- error = #post.errors.include?(:title)
%fieldset.control-group{:class => error ? 'has-error' : ''}
=f.label :title, :class => 'control-label'
.controls
=f.text_field :title, :class => 'form-control input-large input-with-feedback', :autofocus => true
%span.help-inline=error ? f.error_message_on(:title, :class => 'text-error') : pat(:example)
- error = #post.errors.include?(:body)
%fieldset.control-group{:class => error ? 'has-error' : ''}
=f.label :body, :class => 'control-label'
.controls
~f.text_area :body, :class => 'form-control input-large input-with-feedback'
%span.help-inline=error ? f.error_message_on(:body, :class => 'text-error') : pat(:example)
.form-actions
=f.submit pat(:save), :class => 'btn btn-primary'
=f.submit pat(:save_and_continue), :class => 'btn btn-info', :name => 'save_and_continue'
=link_to pat(:cancel), url(:posts, :index), :class => 'btn btn-default'
but i don't know what i must do in functions to save file .
An easy-to-follow guide (assuming you are using activerecord. otherwise change 1st line on example).
Add carrierwave to your Gemfile, and execute bundle install.
Generate a migration: padrino g AddImageToPosts image:string and execute it.
Add mount_uploader :image, Uploader to your Post model.
Inside your lib folder create a file, named uploader.rb (or whatever, but then do not forget to change Uploader on 3rd step.)
Add lines from 7 to 83 to uploader.rb (do not forget uncomment lines, and fix them so they to match your needs).
Browse admin, click browse button for file upload - select a file from filesystem - you are done.
example (step 3)
require 'carrierwave/orm/activerecord'
class Post < ActiveRecord::Base
belongs_to :category
mount_uploader :image, Uploader
...
end

Pony yandex.ru and mail.ru specifics

I am creating a form in Sinatra that will be sending data to an e-mail on submit using Pony gem. This is my code so far:
post '/pemco' do
Pony.mail(
:from => params[:name] + "<" + params[:email] + ">",
:to => '___#yandex.ru',
:subject => params[:name] + " has contacted you",
:body => params[:message],
:via => :smtp,
:via_options => {
:address => 'smtp.yandex.ru',
:port => '465',
:enable_starttls_auto => true,
:user_name => '___',
:password => '___',
:authentication => :plain
})
redirect '/'
end
I press submit, response pends for some time and then I get Net::ReadTimeout
file: protocol.rb location: rescue in rbuf_fill line: 158 error. What am I doing wrong?
This code works for yandex.ru (and you need to go here https://mail.yandex.ru/neo2/#setup/client and allow everything):
post '/sent' do
Pony.mail(
:to => "_yourEmail_#yandex.ru",
:from => "_sameYourEmail_#yandex.ru",
:via => :smtp,
:via_options => {
:address => 'smtp.yandex.ru',
:port => '25',
:enable_starttls_auto => true,
:user_name => '_yourUsername_',
:password => '_yourPassword_',
:authentication => :plain
})
end
And same code works for mail.ru (and generally you don't need to do anything else).

Rails 3 and Twitter bootstrap's sidebar (affix) - how?

I have simple RoR 3.2 application with Twitter bootstrap 2.1.0 (i implemented it via twitter-bootstrap-rails gem). I want to integrate sidebar with few links (as you can see on twitter bootstrap page on the left side) but i can't get how to implement this (yes, i'm noob). Does anyone have solution how to do that in Rails?
My application layout:
!!!
%html
%head
%title MyApp
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%meta{ :name => "viewport", :content => "width=device-width, initial-scale=1.0" }
%body
%div{ :class => "wrapper" }
= render 'layouts/navbar_template'
%div{ :class => "container-fluid" }
- flash.each do |key, msg|
%div{ :class => "alert alert-#{key}" }
%button{ :type => "button", :class => "close", "data-dismiss" =>"alert" }×
= msg
%div{ :class => "row-fluid" }
%div{:class => "span10"}
=yield
%div{:class => "span2"}
-# I would like to have sidebar here
%ul.nav.nav-list.bs-docs-sidenav.affix
%li.active
%a{:href => "#dropdowns"}
%i.icon-chevron-right
Dropdowns
%li
%a{:href => "#buttonGroups"}
%i.icon-chevron-right
Button groups
%li
%a{:href => "#buttonDropdowns"}
%i.icon-chevron-right
Button dropdowns
%li
%a{:href => "#navs"}
%i.icon-chevron-right
Navs
%li
%a{:href => "#navbar"}
%i.icon-chevron-right
Navbar
%li
%a{:href => "#breadcrumbs"}
%i.icon-chevron-right
Breadcrumbs
%li
%a{:href => "#pagination"}
%i.icon-chevron-right
Pagination
%li
%a{:href => "#labels-badges"}
%i.icon-chevron-right
Labels and badges
%li
%a{:href => "#typography"}
%i.icon-chevron-right
Typography
%li
%a{:href => "#thumbnails"}
%i.icon-chevron-right
Thumbnails
%li
%a{:href => "#alerts"}
%i.icon-chevron-right
Alerts
%li
%a{:href => "#progress"}
%i.icon-chevron-right
Progress bars
%li
%a{:href => "#misc"}
%i.icon-chevron-right
Misc

Upload file form with Pony in Sinatra

I'm trying to get an upload form working in Sinatra using Pony. Right now everything works fine, the file's getting read, the emails gets mailed successfully, I just can't seem to get the attachment to attach. I don't think I'm calling the file's path correctly? I'm not entirely sure, new to the whole Ruby/Sinatra/Pony scene. Any help? MUCH appreciated!
Here's what I have right now:
post '/upload' do
unless params[:file] &&
(tmpfile = params[:file][:tempfile]) &&
(name = params[:file][:filename])
#error = "No file selected"
return :success
end
STDERR.puts "Uploading file, original name #{name.inspect}"
while blk = tmpfile.read(65536)
# here you would write it to its final location
STDERR.puts blk.inspect
end
logger.info "some"
Pony.mail(
:from => params[:uname] + "<" + params[:email] + ">",
:to => 'example#example.com',
:subject => "Internship Prospect " + params[:uname] + " has contacted you",
:body => "Hello,\n\nYou have a new contact request\n\nName: "+params[:uname]+"\nEmail: "+params[:email]+"\n\nMessage:\n"+params[:message]+"\n\nThanks,\The Team",
:port => '587',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'name#example.com',
:password => 'password',
:authentication => :plain,
:domain => 'localhost.localdomain',
:attachments => {params[:file][:filename] => File.read(params[:file][:tempfile])}
})
redirect "/success"
end
The :attachments key should be part of the first hash:
Pony.mail(
:from => params[:uname] + "<" + params[:email] + ">",
:to => 'example#example.com',
:subject => "Internship Prospect " + params[:uname] + " has contacted you",
:body => "Hello,\n\nYou have a new contact request\n\nName: "+params[:uname]+"\nEmail: "+params[:email]+"\n\nMessage:\n"+params[:message]+"\n\nThanks,\The Team",
:attachments => {params[:file][:filename] => File.read(params[:file][:tempfile])}
:port => '587',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'name#example.com',
:password => 'password',
:authentication => :plain,
:domain => 'localhost.localdomain',
})

Contact form in ruby, sinatra, and haml

I'm new to all three, and I'm trying to write a simple contact form for a website. The code I have come up with is below, but I know there are some fundamental problems with it (due to my inexperience with sinatra). Any help at getting this working would be appreciated, I can't seem to figure out/find the documentation for this sort of thing.
haml code from the contact page:
%form{:name => "email", :id => "email", :action => "/contact", :method => "post", :enctype => "text/plain"}
%fieldset
%ol
%li
%label{:for => "message[name]"} Name:
%input{:type => "text", :name => "message[name]", :class => "text"}
%li
%label{:for => "message[mail]"} Mail:
%input{:type => "text", :name => "message[mail]", :class => "text"}
%li
%label{:for => "message[body]"} Message:
%textarea{:name => "message[body]"}
%input{:type => "submit", :value => "Send", :class => "button"}
And here is my code in sinatra's app.rb:
require 'rubygems'
require 'sinatra'
require 'haml'
require 'pony'
get '/' do
haml :index
end
get '/contact' do
haml :contact
end
post '/contact' do
name = #{params[:name]}
mail = #{params[:mail]}
body = #{params[:body]}
Pony.mail(:to => '*emailaddress*', :from => mail, :subject => 'art inquiry from' + name, :body => body)
end
I figured it out for any of you wondering:
haml:
%form{ :action => "", :method => "post"}
%fieldset
%ol
%li
%label{:for => "name"} Name:
%input{:type => "text", :name => "name", :class => "text"}
%li
%label{:for => "mail"} email:
%input{:type => "text", :name => "mail", :class => "text"}
%li
%label{:for => "body"} Message:
%textarea{:name => "body"}
%input{:type => "submit", :value => "Send", :class => "button"}
And the app.rb:
post '/contact' do
name = params[:name]
mail = params[:mail]
body = params[:body]
Pony.mail(:to => '*emailaddress*', :from => "#{mail}", :subject => "art inquiry from #{name}", :body => "#{body}")
haml :contact
end
In case anyone can use this, here is what you might need to use your gmail account to send mail.
post '/contact' do
require 'pony'
Pony.mail(
:name => params[:name],
:mail => params[:mail],
:body => params[:body],
:to => 'a_lumbee#gmail.com',
:subject => params[:name] + " has contacted you",
:body => params[:message],
:port => '587',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'lumbee',
:password => 'p#55w0rd',
:authentication => :plain,
:domain => 'localhost.localdomain'
})
redirect '/success'
end
Note the redirect at the end, so you will need a success.haml to indicate to the user that their email was sent successfully.
Uhmm, i tried in irb the following:
foo = #{23}
Of course it wont work! the '#' is for comments in Ruby UNLESS it occurs in a string! Its even commented out in the syntax highlighting.
What you wanted was:
name = "#{params[:name]}"
as you did in your solution (which is not necessary, as it already is a string).
Btw, the reason why the code does not throw an error is the following:
a =
b =
42
will set a and b to 42. You can even do some strange things (as you accidentally did) and set the variables to the return value of a function which takes these variables as parameters:
def foo(a,b)
puts "#{a.nil?} #{b.nil?}" #outputs 'true true'
return 42
end
a =
b =
foo(a,b)
will set a and b to 42.
#{} is interpolation that is used inside "". Just using it outside for a variable assignment won't work.
It would be more likely to be used like this:
number_of_people = 15
Puts "There are #{number_of_people} scheduled tonight"
I've created an example of this in two parts that is available on github. The signup form app is here: signup-form-heroku and an example of the static website that interacts with this is here: static-website-to-s3-example.
The form app is built using Sinatra and is ready to deploy straight onto Heroku. The static site is ready to deploy straight to S3 and use amazon cloudfront.

Resources