Accessing Params Hash form Action Mailer Model

I have used Action Mailer to collect data from a form and send it to a
recipient. Rather than hard-code things like the subject, recipients,
and return address into the model, I would like to include that
information in hidden fields in my form thereby causing that
information to be included in the params hash. My question is: How
can I access that information from the params hash in the model? For
example:

In the model:
@subject=???
@recipients=???
@from'???

Thanks for any input.

         ... doug

doug wrote:

I have used Action Mailer to collect data from a form and send it to a
recipient. Rather than hard-code things like the subject, recipients,
and return address into the model, I would like to include that
information in hidden fields in my form thereby causing that
information to be included in the params hash. My question is: How
can I access that information from the params hash in the model? For
example:

In the model:
@subject=???
@recipients=???
@from'???

Thanks for any input.

         ... doug

Call your mailer deliver_xxx method with parameters.

In controller...
  def contact
      @email_message = params[:email_message]
      Notifier.deliver_contact_email(@email_message)
      redirect_to :action => 'contact'
  end

In Notifier mailer model...
  def contact_email(email_message=nil)
      if email_message
        subject email_message['subject']
        recipients email_message['recipients']
        etc.
      end
  end

Note that if you are calculating these field values, then there's no
need to put them in the form as hidden fields (where you will be
unnecessarily exposing email addresses and some insight as to how your
application works) - just figure them out codewise in the controller
that processes the form, rather than beforehand.

Problem solved. Thanks for the input.

BTW, Cayce, the reason that I include these items as hidden fields in
the form is that in the relevant case the particular person who has
the responsibility for updating that information can easily make any
needed change to the html; but, would have difficulty making a change
to the controller. The email addresses that are entered into the html
are obvuscated by scrambling so that they don't look at all like any
sort of email address let alone a valid one. That should go a long
way toward preventing them from being harvested by spammers. I
unscramble them in the controller.

Thanks again to all for the help.

          ... doug

Since params is just an object, you can pass it to any method. I do
this all the time:

in controller

AdminMailer.deliver_some_email(params)

in admin_mailer model

def some_email(params)
@recipients = params[:to_email]
blah blah blah
end

Phillip --

Your basic solution is better than mine. My solution works; but, it's
not as elegant as yours. Therefore, I want to use yours. The only
thing is, when using your solution I can't seem to access params from
the mail template. Could you please extend your example by telling me
what I need to do to access params from the mail template? Thanks.

       ... doug

doug wrote:

@recipients = params[:to_email]
� blah blah blah
end

Phillip --

Your basic solution is better than mine. My solution works; but, it's
not as elegant as yours. Therefore, I want to use yours. The only
thing is, when using your solution I can't seem to access params from
the mail template. Could you please extend your example by telling me
what I need to do to access params from the mail template? Thanks.

       ... doug

Hi Doug,

No problem. Access to values in the template has to go through the @body
global in the model method. Basically, you assign a hash key/value to
@body for whatever you want to access in the template. For example, I
might do something like

def some_email(params)
  @subject = "Some Subject"
  @recipients = params[:to_email]
  @from = <your from address>
  @sent_on = Time.now # you might want to use TimeZone for this, though
  @headers = {}
  @body[:name] = params[:name]
  @body[:email] = params[:email]
  @body[:group] = params[:group]
  @body[:description] = params[:description]
  @body[:comments] = params[:comments]
end

then in the template, I'd have access to globals

@name
@email
@group
@description
@comments

Again, you're dealing with standard hashes and objects, so you can pass
anything into the @body hash. Many times, I do something like

def some_email(user)
  @body[:user] = user
end

and in the template, use the methods on my User model

@user.first_name
@user.full_name

or whatever.

Hope that helps clear it up for you. I also discovered a tricky little
feature of mailer templates, which I described here

http://per-snicket-y.blogspot.com/2007/12/actionmailer-and-mail-templates.html

It might also be of interest to you.

Peace,
Phillip

OK. I got it working. I had to deviate some in order to accommodate
my particular needs. Specifically, I need the entire params hash to
be available in the template because I need to access its length.
Therefore, in the model, I used:

@body['params'] = params

Then in the template, I am able to access @params.keys.length

I don't do it, but I assume that I would also be able to access things
like @params['name'].

I understand your assigning to various elements of the @body hash in
the model with statments like:

@body[:name] = params[:name]

What I don't understand is why that causes @name to become available
in the template, unless that's just a nuance of Action Mailer. I
would think that one would need to access @body[:name] in the
template. After all, that is what was assigned to.

Thanks for all the help.

          ... doug

I understand your assigning to various elements of the @body hash in
the model with statments like:

@body[:name] = params[:name]

What I don't understand is why that causes @name to become available
in the template, unless that's just a nuance of Action Mailer. I
would think that one would need to access @body[:name] in the
template. After all, that is what was assigned to.

That's what action mailer does - the contents of @body are copied into
the template

Fred

doug wrote:

OK. I got it working.

Great!

I had to deviate some in order to accommodate my particular needs.

No two programs are exactly the same. As developers, we should feel free
to deviate to meet our own needs.

I don't do it, but I assume that I would also be able to access things
like @params['name'].

I would assume so, yes.

I understand your assigning to various elements of the @body hash in
the model with statments like:

@body[:name] = params[:name]

What I don't understand is why that causes @name to become available
in the template, unless that's just a nuance of Action Mailer. I
would think that one would need to access @body[:name] in the
template. After all, that is what was assigned to.

I don't know for sure as I have not delved into the innards to find out,
but my assumption is that since what you are dealing with is the "body"
of the message, there isn't a need to expose anything else, so
ActionMailer just creates global variables for each hash key for you.

Thanks for all the help.

No problem. Glad I could actually be helpful for a change :slight_smile:

          ... doug

Phillip