Thursday, June 4, 2009

Sending mail through ruby on rails

I was working for site and needed something like this for contact form.

http://www.tutorialspoint.com/ruby-on-rails-2.1/rails-send-emails.htm
is really nice tutorial for sending mails , can be used for static sites. This helped me a lot.

But faced some problems,
I didn't get subject and message although the mail was going to the right address.
It was merging with the message and passing through the recipients only .
No other field required for that

so I made some changes in that

in (model) ContactMailer.rd
def contact(recipient, subject, message)
  @subject = "message:: "+subject 
  @message = message
  @recipients = 'your@email.com' 
  @sent_on = Time.now
  @from = recipient
end
and in ContactController.rb
def sendmail
  email = params[:email]
  subject = email[:subject]
  recipient = email[:recipient]
  message = email[:message]
  ContactMailer.deliver_contact(recipient, subject, message)
  return if request.xhr?
  render :text => 'Message sent successfully'
end

No comments:

Post a Comment