Topic: action mailer and parameters

Im working on a Email Sender, so I submit a form  <%= form_for :mail, :url => {:action => 'send_contact_mail'} do |f| %>  and I catch the parameters in the action and send them to the mailer    def send_contact_mail       Notifier.contact_us(params[:mail]).deliver   end    But i dont know who to get this parameters again in the Mailer, something like    def contact_us(name,email,content)     recipients email     from       name     subject

Re: action mailer and parameters

the contact_us method in the Notifier class needs to accept the parameter just like any other method, you can then assign that param to a global instance variable which will make it available in the email template.
so your contact_us method might look something like this

  def contact_us(mail)
    @mail = mail
    mail :to => 'someone@somewhere.com',
      :subject => "some subject",
      :sent_on => Time.now,
      :from => 'me@mysite.com',
      :headers => {"Organization" => "some company name"}
  end

It's just the same as any other method except it acts like a controller action thus the template gets the @mail instance variable just like a view would get.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: action mailer and parameters

that was a excellent answer, so It was just another method, that's for the help, can I go through User View -> Mailer instead of User View -> User Controller -> Mailer?

BTW: @email = email is a hash, but I get

undefined method `name' for {"name"=>"hjo", "email"=>"fsdf", "content"=>"dfg"}:ActiveSupport::HashWithIndifferentAccess

when using @email.name / @email.email / @email.content

Last edited by jtomasrl (2011-06-26 10:53:20)

Re: action mailer and parameters

You should always post back to a controller's action as your routes know where that is but eMail might be the exception to the rule. I would stick with the controllers action is my best advice.

To access a hash (params is a hash) you index the element you need as follows
@email[:name]
name is not a method of the hash class.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: action mailer and parameters

oh ok, thaks for your help