Contact Page with ActionMailer

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=404
http://forums.site5.com/showthread.php?t=18522
http://www.buildingwebapps.com/podcasts/6798-adding-a-contact-form-and-mailer/show_notes
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:

... (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:

class Emailer < ActionMailer::Base
  def contact_email(email_params, sent_at = Time.now)
    # You only need to customize @recipients.
    @recipients = "[email protected]"
    @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)

Name:

<%= @email_name %>

Message:

<%= @email_body %>

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

<% 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:

...
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 :slight_smile:

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”,
:port => “26”,
:domain => “mydomain.com”,
:user_name => “[email protected]”,
:password => “xxxx”,
:authentication => :login
}

end narayan

===

Hope it works!

-Narayan

On Oct 23, 8:47 am, Victor V. [email protected]

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?

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. [email protected]

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?

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. [email protected]

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.

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)

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 “[email protected]” 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

Hope it helps.
-Narayan

On Oct 26, 5:29 am, Victor V. [email protected]