Forum: Ruby on Rails Contact Page with ActionMailer

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.
aa b. (Guest)
on 2008-10-23 07:47
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-addin...
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:
[code=]
... (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"
[/code]
/app/models/emailer.rb:
[code=]
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
[/code]
/app/controllers/emailer_controller.rb
[code=]
class EmailerController < ApplicationController
  def send_mail
    Emailer::deliver_contact_email(params[:email])
  end
end[/code]
/app/views/emailer/contact_email.rhtml (this is the formatting of the
message sent)
[code=]
Name:

<%= @email_name %>

Message:

<%= @email_body %>
[/code]
/app/views/emailer/contact_page.rhtml (this is the form from which the
message is made)
[code=]
<% 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 %>
[/code]
/app/config/routes.rb:
[code=]
...
map.connect 'contactme', :controller => 'emailer', :action =>
'contact_page' #testing
...
[/code]
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 :)
Narayan (Guest)
on 2008-10-23 14:06
(Received via mailing list)
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 => "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>
aa b. (Guest)
on 2008-10-23 20:52
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?
aa b. (Guest)
on 2008-10-24 05:25
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 (Guest)
on 2008-10-25 17:00
(Received via mailing list)
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>
aa b. (Guest)
on 2008-10-26 03:55
Attachment: environment.rb (0 Bytes)
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>

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.
aa b. (Guest)
on 2008-10-26 04:29
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)
Narayan (Guest)
on 2008-10-29 07:15
(Received via mailing list)
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


Hope it helps.
-Narayan



On Oct 26, 5:29 am, Victor V. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.