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. -- Posted via http://www.ruby-forum.com/.
You can try this link: http://depot.iamjp.com/contact It uses Ajax too. -Chris Digital Pardoe 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.-- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
Digital Pardoe 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 => "yourusername@yourdomain.com", :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 <admin@yourdomain.com>" @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: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> etc... <body> <p>Contact Name: <%= @contact.first_name %></p> etc... </body> 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 -- Posted via http://www.ruby-forum.com/.
Hi, That''s brilliant, just what I was looking for thanks. I will get straight on to implementing it. Thanks Again, Alex/ -- Posted via http://www.ruby-forum.com/.
Digital Pardoe
2006-Jul-21 10:44 UTC
[Rails] Re: Email Form for Contact Page (A Little More Help Please)
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" %>
<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>
<!-- Change this to a text box once I know it is working
-->
<td><%= text_field "email", "body", :size =>
30 %></td>
</tr>
</table>
<input type="submit" value="Send Email"
class="primary" /><br /><br />
<%= 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 = "contact@website.co.uk"
@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.
--
Posted via http://www.ruby-forum.com/.
Michael
2006-Jul-21 15:34 UTC
[Rails] Re: Email Form for Contact Page (A Little More Help Please)
At quick glance, try: <%= @email_params[:body] %> instead of: <%= email_params[:body] %> Michael -- Posted via http://www.ruby-forum.com/.
Digital Pardoe
2006-Jul-21 17:13 UTC
[Rails] Re: Email Form for Contact Page (A Little More Help Please)
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 -- Posted via http://www.ruby-forum.com/.