Hopefully all the code is ok, I've cut-n-pasted it from my working copy, but I've cut out the stuff that sends to different recipients based on user id, so can't verify 100% it's working - let me know if you run into trouble and I'll see what I can do

Edit: apologies for the bad formatting, the instruction for where the code goes is ABOVE the code block, even though the next instruction is closer - have numbered the instructions and put dots in the break up the post, hopefully will help - can't wait for a nice RoR forum

Re: How to make a simple Contact Form

Thanks, your code works great. I've implemented it and made only a few changes to it. The

email = 'anon@example.net'

bit of your Model code won't work here, so I just left it out. I also added a 'name' variable because I want to know the names of my visitors!

It's a shame there aren't any validation methods for ActionMailer (like in ActiveRecord), so I'll have to come up with my own validation methods. Do you have any in your contact form? I wonder how you would deal with visitors entering a non-valid email address and things like that...

Re: How to make a simple Contact Form

Glad it works, my one was a bit different, I had a select box filled with the user names of the authors on the site and then the visitor could choose which author to send an email to, enter their own email address (which wasn't checked) and then send the message. Hence the default 'anon@example.net' was there in case they didn't bother filling out the field. In my case I didn't care about getting back in touch with the visitor, I just wanted an easy, anonymous way for the authors to get feedback.

I suppose you could do a regex pattern match within the controller, something like

if request.post? and params[:email] ~= /emailregex/

but it's not exactly fool proof(!) - I'm like you and would love to know the 'proper' way to do validations on ActionMailer - anyone out there know??

Re: How to make a simple Contact Form

I haven't been on the net for a week so I'm a bit late. But this is how I did mine. It is a bit more verbose but the benefit is that it can be used to send HTML email as well as normal plain text email.

begin#First check if the senders email is validif from_email =~ /^[a-zA-Z0-9._%-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/#put all the contents of my form in a hashmail_info = {"from_name" => from_name, "from_email" => from_email, "message" => message, "the_subject" => the_subject}#Call the Notifier class and send the emailNotifier.deliver_multipart_alternative(mail_info)#Display a message notifying the sender that his email was delivered.flash[:notice] = 'Your message was successfully delivered.'#Then redirect to index or any page you want with the messageredirect_to(:action => 'index')

else#if the senders email address is not valid#display a warning and redirect to any action you wantflash[:warning] = 'Your email address appears to be invalid.'redirect_to(:action => 'index')end

rescue#if everything fails, display a warning and the exception#Maybe not always advisable if your app is public#But good for debugging, especially if action mailer is setup wrongflash[:warning] = "Your message could not be delivered at this time. #$!. Please try again later"redirect_to(:action => 'index')end

end

Next I add the following to the file, notifier.rb in app/models

#this is the method that actually sends the email def multipart_alternative(mail_info)

Finally the view for sending emails in your application. If added some javascript to verify if the contents of the form are filled in so you don't get blank emails. Also the input type being used is a button so that it can verify before submitting the form.

Re: How to make a simple Contact Form

nice write up daibatzu!

never really used html emails much but can see the attraction now in having my ruby minions deliver pretty emails!

I think your method of inline javascript is probably the easiest way of making sure the form is filled corectly, does seem a pity that Notifier can't have some validates_ rules in it, but I'm sure DHH and the team have some reasons

Re: How to make a simple Contact Form

Apparently Javascript can also validate regular expressions so it may be possible to remove the email validation check from the controller and place it in the javascript. But I'll have to look into that.

Re: How to make a simple Contact Form

Hi, why not post your code. I'm sure you've made a mistake somewhere while copying the example above. I've just tried it again and it works perfectly. If you post it the my_mailer action in your controller, your view, notifier.rb model and both multipart_alternative and multipart_plain I'll see what I can do. Good Luck.

Re: How to make a simple Contact Form

Hello, I've noticed one mistake and think I know what your problem is.

Firstly, your contact form should not be my_mailer.rhtml. That is a seperate action on it's own and it doesn't have a view associated with it. You can define a new action in your front_controller, maybe call it contact. So you would have:

def contact#doesn't really have to do anything.end

Now create a view called contact.rhtml and put everything you've put in my_mailer.rhtml into this file (i.e the contents of my_mailer.rhtml into contact.rhtml). After this you MUST delete my_mailer.rhtml because it isn't necessary and is conflicting with your application.

This should fix your problem. I'm not sure but I think this is the problem you are having.

If you want to call my_mailer from a seperate controller remember that your form action should have the controller name prefixed. So you would have: