Forum: Ruby on Rails Displaying form body in email

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tom E. (Guest)
on 2007-07-18 15:00
Hi, total newb here.

I want to send an email via a form.  I am at a stage where I can send an
email but am not sure how to display the email content that should come
from the form body.   At the moment I have a subject and nothing else.
Here's what I've got.

#contact_controller.rb
class ContactController < ApplicationController
  def index
    @result = params[:message]
  end

  def send_mail
    #line below sends email
    Notifier.deliver_signup_notification()
    #flash notice and redirect after sending mail
    flash[:notice] = 'email sent to www.example.com.'
    redirect_to "http://www.example.com"
  end
end

#notifier.rb:
class Notifier < ActionMailer::Base
  def signup_notification()
    recipients "removed_email_address@domain.invalid"
    from       "removed_email_address@domain.invalid"
    subject    "New inquiry"
    body       :message => @result
  end
end

#index.rhtml:
<% form_tag( :action => :send_mail ) do %>
  <%= text_area_tag :message, params[:message], :size => "25x10" %>
  <%= submit_tag( "Send" ) %>
<% end %>

#signup_notification.rhtml:
The <%= @message %>

thanks for any help, I've been stuck on this one for awhile.
Tom E. (Guest)
on 2007-07-18 16:10
Tom E. wrote:
> Hi, total newb here.
>
> I want to send an email via a form.  I am at a stage where I can send an
> email but am not sure how to display the email content that should come
> from the form body.   At the moment I have a subject and nothing else.
> Here's what I've got.
>
> #contact_controller.rb
> class ContactController < ApplicationController
>   def index
>     @result = params[:message]
>   end
>
>   def send_mail
>     #line below sends email
>     Notifier.deliver_signup_notification()
>     #flash notice and redirect after sending mail
>     flash[:notice] = 'email sent to www.example.com.'
>     redirect_to "http://www.example.com"
>   end
> end
>
> #notifier.rb:
> class Notifier < ActionMailer::Base
>   def signup_notification()
>     recipients "removed_email_address@domain.invalid"
>     from       "removed_email_address@domain.invalid"
>     subject    "New inquiry"
>     body       :message => @result
>   end
> end
>
> #index.rhtml:
> <% form_tag( :action => :send_mail ) do %>
>   <%= text_area_tag :message, params[:message], :size => "25x10" %>
>   <%= submit_tag( "Send" ) %>
> <% end %>
>
> #signup_notification.rhtml:
> The <%= @message %>
>
> thanks for any help, I've been stuck on this one for awhile.

D'oh, changed things about a touch in controller.rb and notifier.rb and
it now works.

class ContactController < ApplicationController
  def send_mail
    result = params[:message]
    #line below sends email
    Notifier.deliver_signup_notification(result)
    #flash notice and redirect after sending mail
    flash[:notice] = 'email sent to www.example.com.'
    redirect_to "http://www.example.com"
  end
end

class Notifier < ActionMailer::Base
  def signup_notification(result)
    recipients "removed_email_address@domain.invalid"
    from       "removed_email_address@domain.invalid"
    subject    "New inquiry"
    body       :message => result
  end
end
This topic is locked and can not be replied to.