# Contact Page with ActionMailer

**URL:** <https://www.ruby-forum.com/t/contact-page-with-actionmailer/151580>\
**Category:** Rails\
**Created:** [October 23, 2008, 5:47am UTC](https://www.ruby-forum.com/t/contact-page-with-actionmailer/151580 "2008-10-23T05:47:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [October 23, 2008, 5:47am UTC](https://www.ruby-forum.com/t/contact-page-with-actionmailer/151580/1 "2008-10-23T05:47:23Z")

</div>

Hello, I am trying to make a very basic “contact me” form on my website.  
I have tried the following tutorials:  
[http://railsforum.com/viewtopic.php?id=15172](http://railsforum.com/viewtopic.php?id=15172)  
[http://railsforum.com/viewtopic.php?id=404](http://railsforum.com/viewtopic.php?id=404)  
[http://forums.site5.com/showthread.php?t=18522](http://forums.site5.com/showthread.php?t=18522)  
[http://www.buildingwebapps.com/podcasts/6798-adding-a-contact-form-and-mailer/show\_notes](http://www.buildingwebapps.com/podcasts/6798-adding-a-contact-form-and-mailer/show_notes)  
[http://digitalpardoe.co.uk/blog/show/56](http://digitalpardoe.co.uk/blog/show/56)

I found the last one to fit my needs the best. I need to have forms for  
a person’s name, email address and message. Upon clicking a submit  
button, I want that information emailed to a hardcoded email address.

My setup is as follows:

/config/environment.rb:

```auto
... (at the end)
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
    :address => 'localhost', #was told by hostgator that these
are the correct settings
    :port => 25,
    :authentication => :login, # Don't change this one.
    :user_name => "mailer_username",
    :password => "mailer_password"
}
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
```

/app/models/emailer.rb:

```auto
class Emailer < ActionMailer::Base
  def contact_email(email_params, sent_at = Time.now)
    # You only need to customize @recipients.
    @recipients = "removed_email_address@domain.invalid"
    @from = email_params[:name] + " <" + @email_params[:address] + ">"
    @subject = email_params[:subject]
    @sent_on = sent_at
    @body["email_body"] = email_params[:body]
    @body["email_name"] = email_params[:name]
  end
end
```

/app/controllers/emailer\_controller.rb

`
class EmailerController < ApplicationController
  def send_mail
    Emailer::deliver_contact_email(params[:email])
  end
end`  
/app/views/emailer/contact\_email.rhtml (this is the formatting of the  
message sent)

```auto
Name:

<%= @email_name %>

Message:

<%= @email_body %>
```

/app/views/emailer/contact\_page.rhtml (this is the form from which the  
message is made)

```auto
<% form_tag :action => "send_mail" do %>
<table>
    <tr>
        <td><label for="email_name">Name:</label></td>
        <td><%= text_field "email", "name", :size => 30 %></td>
    </tr>
    <tr>
        <td><label for="email_address">Email Address:</label></td>
        <td><%= text_field "email", "address", :size => 30 %></td>
    </tr>
    <tr>
        <td><label for="email_subject">Subject:</label></td>
        <td><%= text_field "email", "subject", :size => 30 %></td>
    </tr>
    <tr>
        <td><label for="email_body">Body:</label></td>
        <td><%= text_area "email", "body", :rows => 8, :cols => 30
%></td>
    </tr>
</table>

<input type="submit" value="Send Email" class="primary" />
<% end %>
```

/app/config/routes.rb:

```auto
...
map.connect 'contactme', :controller => 'emailer', :action =>
'contact_page' #testing
...
```

When I fill out the contact forms and send, nothing happens. The only  
error I was getting with script/server was a depreciation error with the  
view, but Duplex helped me fix that.

I contacted hostgator to see if my settings in environment.rb were  
correct, and they said they were. So I’m thinking I wasn’t supposed to  
put the “def send\_mail…” in it’s own separate controller? I’m really  
not sure because this looks right to me.

Some other information: I’m using simplelog 2.0.2 as my blogging  
platform on rails 2.0.2 and I’m trying to add this contact form to it.  
Simplelog works wonderfully, but maybe its causing problems for the  
mailer. Another thing, when I change “Base.server\_settings” to  
“Base.smtp\_settings” in /config/environment.rb, rails fails to start. I  
thought “Base.smtp\_settings” was the new, better way for that setting.  
Does anyone have any hints for me on how to get this mailer working?  
I’ve spent days trying to figure this out, please help.

If you read all of that, thank you very much for your time 🙂

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [October 23, 2008, 12:06pm UTC](https://www.ruby-forum.com/t/contact-page-with-actionmailer/151580/2 "2008-10-23T12:06:38Z")

</div>

Try modifying your environment.rb to include the following

===  
…  
Rails::Initializer.run do |config|

# Settings in config/environments/\* take precedence over those

specified here.

# …

##### start narayan

config.action\_mailer.delivery\_method = :smtp  
config.action\_mailer.smtp\_settings = {  
:address =\> “[mail.mydomain.com](http://mail.mydomain.com)”,  
:port =\> “26”,  
:domain =\> “[mydomain.com](http://mydomain.com)”,  
:user\_name =\> “removed\_email\_address@domain.invalid”,  
:password =\> “xxxx”,  
:authentication =\> :login  
}

##### end narayan

===

Hope it works!

-Narayan

On Oct 23, 8:47 am, Victor V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [October 23, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/contact-page-with-actionmailer/151580/3 "2008-10-23T18:52:49Z")

</div>

First of all, thank you for replying. When I use your suggestion I get  
an “application fails to start” error. I narrowed it down to one thing  
though. What causes the fails to start error is the “smtp” in the  
“config.action\_mailer.smtp\_settings = {” line in  
/config/environment.rb. If I use “server” as in  
“config.action\_mailer.server\_settings = {” The app starts again, but  
the email doesn’t send. I know the “server\_settings” option is now  
depreciated, so what can be done?

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [October 25, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/contact-page-with-actionmailer/151580/4 "2008-10-25T15:00:43Z")

</div>

Not sure why

it works fine for me, so send me your environment.rb and i can take a  
look at it (pls clear the password field )

also send me the Rails version no.

-Narayan

On Oct 24, 6:25 am, Victor V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [October 24, 2008, 3:25am UTC](https://www.ruby-forum.com/t/contact-page-with-actionmailer/151580/5 "2008-10-24T03:25:32Z")

</div>

Rake produces the error:

undefined method `smtp\_settings=’ for ActionMailer::Base:Class

How do I define that method? Do I put it in environment.rb?

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [October 26, 2008, 2:55am UTC](https://www.ruby-forum.com/t/contact-page-with-actionmailer/151580/6 "2008-10-26T02:55:22Z")

</div>

Narayan wrote:

> Not sure why
> 
> it works fine for me, so send me your environment.rb and i can take a  
> look at it (pls clear the password field )
> 
> also send me the Rails version no.
> 
> -Narayan
> 
> On Oct 24, 6:25ï¿½am, Victor V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Okay, sorry for the delayed reply. Hostgator upgraded rails from 2.0.2  
to 2.1.2 last night. So my app wouldn’t start at all. Now I’m  
rebuilding, and I’m starting with the mailer. I’m using exactly the same  
code as above and good news. Using smtp now works, but still no email is  
being sent. Here is my environment.rb.

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [October 26, 2008, 3:29am UTC](https://www.ruby-forum.com/t/contact-page-with-actionmailer/151580/7 "2008-10-26T03:29:34Z")

</div>

Okay, I just noticed that my code isn’t --exactly-- the same in  
environment.rb,  
I left this out:  
ActionMailer::Base.perform\_deliveries = true  
ActionMailer::Base.raise\_delivery\_errors = true  
ActionMailer::Base.default\_charset = “utf-8”

But when I put it back in the app fails to start. What should my  
environment.rb look like?

(Thank you so much for helping me)

---

<div class="post-metadata">

**Author:** ![bbnnt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@bbnnt](https://www.ruby-forum.com/u/bbnnt)\
**Post date:** [October 29, 2008, 6:15am UTC](https://www.ruby-forum.com/t/contact-page-with-actionmailer/151580/8 "2008-10-29T06:15:51Z")

</div>

Try the following step-by-step

1. remove all the 3 lines that sets ActionMailer::Base - let it take  
the default
2. in the user name don’t you need “removed\_email\_address@domain.invalid” instead of just  
myuser? - so make appropriate changes
3. try to send an email
4. if the above still does not work - try removing the quote from  
the :port parameter i.e. :port =\> 26
5. try to send an email
6. if the above still has problems, check out this link for dreamhost:  
[http://wiki.dreamhost.com/ActionMailer](http://wiki.dreamhost.com/ActionMailer)

Hope it helps.  
-Narayan

On Oct 26, 5:29 am, Victor V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
