Email Form for Contact Page

Can anyone point me to or provide me with code or a tutorial (preferably
a tutorial) for creating simple contact forms where the data is sent
over email to a specific email address. I have search all over the
internet and there doesn’t see to be anything anywhere.

Thanks In Advance,
Alex.

You can try this link:

http://depot.iamjp.com/contact

It uses Ajax too.

-Chris

Digital P. wrote:

Can anyone point me to or provide me with code or a tutorial (preferably
a tutorial) for creating simple contact forms where the data is sent
over email to a specific email address. I have search all over the
internet and there doesn’t see to be anything anywhere.

Thanks In Advance,
Alex.

Hi,

That was sort of what I was looking for, useful for the AJAX but I need
the RoR code that connects to the ActionMailer and actually sends the
formatted email to my email address. I should have been more specific in
my first post.

Thanks Again,
Alex.

Digital P. wrote:

Hi,

That was sort of what I was looking for, useful for the AJAX but I need
the RoR code that connects to the ActionMailer and actually sends the
formatted email to my email address. I should have been more specific in
my first post.

Thanks Again,
Alex.

Hi Alex,

There are several steps for ActionMailer and to some degree I hope I can
help you.

First, environment.rb:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
:address => ‘your.smtpout.com’, # replace with your SMTP
server
:port => 80, # replace with your SMTP server’s port

:domain => “yourdomain.com”,

	:authentication => :login,
	:user_name => "[email protected]",
	:password  => "yourpassword"

}

ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = “utf-8”

Next, you need a model for mailing. Call it ContactMailer.rb or
something like that.

First line: class ContactMailer < ActionMailer::Base

This allows it to inherit from ActionMailer.

Then, in that file, create a function for your stuff: E.G.

def customer_notification(contact, sent_at = Time.now)
@subject = “Your custom subject”
@body[“contact”] = contact
@recipients = @contact.email_address
@from = “Admin or Whatever [email protected]
@sent_on = sent_at
@headers = {}
end

Then, you need a view (contact_mailer) to format the email. I have done
this with plain text, xml and html. As per this example, create an
rhtml file called customer_notification.rhtml. The file will look
something like this:

etc...

Contact Name: <%= @contact.first_name %>

etc…

etc...

Finally, in your controller that gets the contact form submitted to, do
something like this in the action:

email = ContactMailer.create_customer_notification(@contact)
email.set_content_type(“text/html”)
ContactMailer.deliver(email)

Whew…I hope this helps. I sure hope I answered your question! :slight_smile:
Or did I miss the request all together?

Regards,

Michael
mmodica at cox dot net

Hi,

That’s brilliant, just what I was looking for thanks. I will get
straight on to implementing it.

Thanks Again,
Alex/

I have worked my way through adapting the large example above. So far I
have got;

My environment.rb all set up (I think I got that bit right), all the
above environment.rb code customized and placed at the very end of the
file, not between the ‘Rails::Initializer.run do |config|’ and ‘end’
bit.

In the main view for ‘Contact’:

<%= start_form_tag :action=> “send_mail” %>

            <!-- Change this to a text box once I know it is working 

–>

	<td><%= text_field "email", "body", :size => 30 %></td>
</tr>
Name: <%= text_field "email", "name", :size => 30 %>
Email Address: <%= text_field "email", "address", :size => 30 %>
Subject: <%= text_field "email", "subject", :size => 30 %>
Body:


<%= end_form_tag %>

In the controller for ‘Contact’:

class ContactController < ApplicationController
layout ‘blog’
def index
render :action => ‘main’
end

def send_mail
email = Contact.create_mail(@params[:email])
email.set_content_type(“text/html”)
Contact.deliver(email)
end
end

In the model for ‘Contact’:

class Contact < ActionMailer::Base
def mail(email_params, sent_at = Time.now)
@subject = email_params[:subject]
@body[“email_params”] = email_params
@recipients = “[email protected]
@from = email_params[:name] + " <" + email_params[:address] + “>”
@sent_on = sent_at
@headers = {}
end
end

The problem occurs in the the view for mail (mail.rhtml), so far it
looks like this:

<%= email_params[:body] %>

I have tried so many different combinations of variables in the model
and the view but I just get the error:

undefined local variable or method `email_params’ for
#ActionView::Base:0xb759aeec (or other ones to do with nil arrays
depending what I try).

I can’t work out how to pass the body of the email to the view at all. I
believe this is the only thing left causing an error as things like:
email_params[:subject] do not produce an error so I assume I’ve got that
right.

At quick glance, try:

<%= @email_params[:body] %>

instead of:

<%= email_params[:body] %>

Michael

I tried that and it didn’t work so I re-wrote everything, going through
what you said more carefully and using elements of the ActionMailer Wiki
entry on the Ruby on Rails website (which I didn’t understand before but
linked with your example I do now) and everything is working as it
should.

Thanks For All Your Help,
Alex

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs