# Email Form for Contact Page

**URL:** <https://www.ruby-forum.com/t/email-form-for-contact-page/99010>\
**Category:** Rails\
**Created:** [July 19, 2006, 7:03pm UTC](https://www.ruby-forum.com/t/email-form-for-contact-page/99010 "2006-07-19T19:03:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![digitalpardoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cc9497/32.png) [@digitalpardoe](https://www.ruby-forum.com/u/digitalpardoe)\
**Post date:** [July 19, 2006, 7:03pm UTC](https://www.ruby-forum.com/t/email-form-for-contact-page/99010/1 "2006-07-19T19:03:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![digitalpardoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cc9497/32.png) [@digitalpardoe](https://www.ruby-forum.com/u/digitalpardoe)\
**Post date:** [July 19, 2006, 8:37pm UTC](https://www.ruby-forum.com/t/email-form-for-contact-page/99010/2 "2006-07-19T20:37:04Z")

</div>

You can try this link:

[http://depot.iamjp.com/contact](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.

---

<div class="post-metadata">

**Author:** ![digitalpardoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cc9497/32.png) [@digitalpardoe](https://www.ruby-forum.com/u/digitalpardoe)\
**Post date:** [July 19, 2006, 9:04pm UTC](https://www.ruby-forum.com/t/email-form-for-contact-page/99010/3 "2006-07-19T21:04:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![digitalpardoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cc9497/32.png) [@digitalpardoe](https://www.ruby-forum.com/u/digitalpardoe)\
**Post date:** [July 19, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/email-form-for-contact-page/99010/4 "2006-07-19T21:39:15Z")

</div>

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](http://your.smtpout.com)’, # replace with your SMTP  
server  
:port =\> 80, # replace with your SMTP server’s port

# :domain =\> “[yourdomain.com](http://yourdomain.com)”,

```
	:authentication => :login,
	:user_name => "removed_email_address@domain.invalid",
	: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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”  
@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! 🙂  
Or did I miss the request all together?

Regards,

Michael  
mmodica at cox dot net

---

<div class="post-metadata">

**Author:** ![digitalpardoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cc9497/32.png) [@digitalpardoe](https://www.ruby-forum.com/u/digitalpardoe)\
**Post date:** [July 19, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/email-form-for-contact-page/99010/5 "2006-07-19T22:18:57Z")

</div>

Hi,

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

Thanks Again,  
Alex/

---

<div class="post-metadata">

**Author:** ![digitalpardoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cc9497/32.png) [@digitalpardoe](https://www.ruby-forum.com/u/digitalpardoe)\
**Post date:** [July 21, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/email-form-for-contact-page/99010/6 "2006-07-21T12:44:27Z")

</div>

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 = “removed\_email\_address@domain.invalid”  
@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.

---

<div class="post-metadata">

**Author:** ![digitalpardoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cc9497/32.png) [@digitalpardoe](https://www.ruby-forum.com/u/digitalpardoe)\
**Post date:** [July 21, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/email-form-for-contact-page/99010/7 "2006-07-21T17:29:26Z")

</div>

At quick glance, try:

\<%= @email\_params[:body] %\>

instead of:

\<%= email\_params[:body] %\>

Michael

---

<div class="post-metadata">

**Author:** ![digitalpardoe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cc9497/32.png) [@digitalpardoe](https://www.ruby-forum.com/u/digitalpardoe)\
**Post date:** [July 21, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/email-form-for-contact-page/99010/8 "2006-07-21T19:12:20Z")

</div>

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
