Form Mail

What is the most RoR friendly way of making a form mailer similar to
this php one?

http://tips-scripts.com/?tip=form_mail#tip

Or is it easier just to use the php with RoR?

Sending emails from your Rails app is so easy, however I don’t think
that is your point. Rails is not intended for replacing simple scripts
like that on your website, but you certainly can. Rails is for building
applications. Just look at the docs for ActionMailer.

Timothy J.

Thanks, I sure its simple if you know how but I can’t work it out and
the ActionMailer doc doesn’t go into much detail.

what I can’t work out is how connect the users form (with email & text
area ) in the view to communicate to the actionmailer.

Rob B. wrote:

Thanks, I sure its simple if you know how but I can’t work it out and
the ActionMailer doc doesn’t go into much detail.

what I can’t work out is how connect the users form (with email & text
area ) in the view to communicate to the actionmailer.

I’ll give you sample code where I use a form to ask for someone’s e-mail
address then I reset their user-pw:

This is in a model… MailSender.rb

class MailSender < ActionMailer::Base

def sent(subject, body, from, recipient,sent_at = Time.now)
@subject = subject
@body = body
@recipients = recipient
@from = from
@sent_on = sent_at
end
end

Here is the form in a partial:

Please Supply Email: <%= start_form_tag :action => :rpass%> <%= text_field_tag :email, params[:email] %> <%= submit_tag "Reset" %> <%= end_form_tag %>

Here is the rpass action in the controller:

def rpass
@email = params[:email]
@thisUser = User.find(:all, :conditions => “email=’#{@email}’”)
if @thisUser[0]!=nil
@random = rand(1000000000).to_s
@thisUser[0].change_password(@random)
@thisUser[0].update()
@thisUser[0].save
@message = "Hello - Your password has been reset to: " + @random
flash[:notice] = “Your password has been changed!”
email = MailSender.create_sent(“Password Reset”, @message,
@thisUser[0].email, [email protected])
MailSender.deliver(email)
else
flash[:notice] = “No User with that e-mail”
end
redirect_to :controller => “account”, :action => “index”
end

Hope this helps
-Ben

Brian H. wrote:

See an article I wrote on how to handle forms with validations.

http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf

Brian H. you are a Legend! I have been looking for something like
this for about 4 days now.

You should add this to the Rails Wiki. Its on the wishlist
http://wiki.rubyonrails.org/rails/pages/WishList

Thanks again!

David S.

Brian H. wrote:

http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf

Great - exactly what I needed. Huge thank you!

See an article I wrote on how to handle forms with validations.

http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf

Even though it’s acknowledged in the article I posted, it wouldn’t have
been
possible without the inspiration from Rick O… Thanks Rick!

Rob B. wrote:

Brian H. wrote:

http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf

I ran into a problem that I can’t solve.

When I click on Submit I get the following error:
NoMethodError in #

undefined method `const_available?’ for FrontController:Class

RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:234:in
traverse_to_controller' generated/routing/recognition.rb:3:in recognize_path’
-e:3:in `load’
-e:3

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:234:in
traverse_to_controller' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:458:in recognize!’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in
dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in handle_dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in
service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in service’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in start_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in start_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in each’
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in start' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
require__' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in require’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
require' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in require__’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in require’
./script/server:3

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:234:in
traverse_to_controller' generated/routing/recognition.rb:3:in recognize_path’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:458:in
recognize!' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in
handle_dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in service’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run’
c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in each' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in start’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in
dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in require__’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in require’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
require__' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in require’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
require' ./script/server:3 -e:3:in load’
-e:3

Request

Parameters: {“commit”=>“Send”, “contact”=>{“name”=>“”, “message”=>“”,
“subject”=>“”, “email_address”=>“”}}

Any ideas on what its looking for. My code looks exactly like Brian’s

David S. wrote:

Rob B. wrote:

Brian H. wrote:

http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf

I ran into a problem that I can’t solve.

When I click on Submit I get the following error:

NoMethodError in #

undefined method `const_available?’ for FrontController:Class

RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace

Request

Parameters: {“commit”=>“Send”, “contact”=>{“name”=>“”, “message”=>“”,
“subject”=>“”, “email_address”=>“”}}

Any ideas on what its looking for. My code looks exactly like Brian’s

Contact View:
<%=error_messages_for “contact” %>
<%= start_form_tag :controller=>“front_controller”,
:action=>“send_contact_request” %>

Name
<%= text_field("contact", "name") %>
Email
<%= text_field("contact", "email_address") %>
Subject
<%= text_field("contact", "subject") %>
Message
<%= text_area("contact", "message") %>
<%= submit_tag "Send" %> <%= end_form_tag %>

Conrtoller:
def contact
@contact = Contact.new
end

def send_contact_request
breakpoint
@contact = Contact.new(params[‘contact’])
if @contact.save
begin
Mailer::deliver_contact_message(@contact)
flash[:notice] = “Success!”
redirect_to :action=> “contact”
rescue
flash[:notice] = “It broke!”
render :action=> “contact”
end
else
render :action=> “contact”
end
end