Add Parameter to confirmation mail

1,096 views
Skip to first unread message

Flo

unread,
Aug 27, 2012, 5:51:06 AM8/27/12
to plataforma...@googlegroups.com
Hi Devise Team :)

I need to add a extra parameter to the configuration email.
Didn't find the answer here, on stack overflow and google, so I ask here.

What is the best way to achieve this?
change the method:
  def(confirmation_instructions(record)
    devise_mail(record, :confirmation_instuctions)
  end
but than I would also need to change devise_mail() ?
like devise_mail(record, :action, :parameter) ? 

Is it possible to extend the devise_mail method?

Thank you very much for your help.

Cheers,
Flo

Carlos Antonio da Silva

unread,
Sep 8, 2012, 8:39:50 AM9/8/12
to plataforma...@googlegroups.com
The question that remains is: why do you need to do that? What's your requirement? It may be better solved with another solution, different than changing Devise internals.

--
 
 
 



--
At.
Carlos Antonio

Hassan Shahid

unread,
Sep 8, 2012, 10:09:10 AM9/8/12
to plataforma...@googlegroups.com
I've found that one useful way of doing this without changing devise internals is to dynamically generate the link inside the confirmation_email template.

If you haven't already done so, you should use the devise views generator to create local versions of all devise views in your app.

This is the url generator in the confirmation email template:
confirmation_url(@resource, :confirmation_token => @resource.confirmation_token)

You could replace the parameters with a  param generator that holds custom logic to dynamically generate the appropriate link based on the scenario, maybe something like:

confirmation_url(@resource, generate_confirmation_params_for(@resource))

and perhaps in your application_helper, you'd have that method:

def generate_confirmation_params_for(resource)
confirmation_params = {}
confirmation_params[:confirmation_token] = resource.confirmation_token
confirmation_params[:param_2] = 'whatever' if resource.passes_this_test?
(etc…)
confirmation_params
end

One small caveat is that you'll have to create an initializer for helper methods to be available to ActionMailer, which is pretty trivial and I find it to be a good, useful idea.  You'd do that by something like this in app/config/initializers/action_mailer_helpers.rb (new file, call it whatever you want):

class ActionMailer::Base
add_template_helper(ApplicationHeper)
end

Lastly, this does require you to override the confirmation acceptance, so whether that be by overriding the method that the confirmation_url points to, or by changing the url altogether to point to a custom action.  Because something like a confirmation acceptance doesn't do that much (I believe it just finds the confirmation token in the users table and nils it out and redirects you to the after_sign_in_path_for(resource) url), you can go either way with it.  For my needs, I just wrote  a custom route.  I also didn't do it with confirmations, I did with it Devise::Invitation, but it's virtually the same set of steps.

This is just a less hack-ish way of doing what you want; rather than fiddling with the devise internals.  Hope this helps you or someone out.

-Hassan

--
 
 
 

Tom Harrison

unread,
Sep 9, 2012, 11:14:58 AM9/9/12
to plataforma...@googlegroups.com
Following Hassan Shahid's reply but perhaps boiling it down a little... 

I did this last week -- my case was to add Google Analytics tracking codes to link on our emails, Devise or otherwise.  For example I might want to record that the incoming visit was from an email, from our system, and was the confirmation response.

Generate the Devise views, including mailers within your app as Hassan says.

Then within the email you can simply add parameters to the existing URL code within the link_to, e.g.

The code from Devise looks like

<%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %>

and ours just tacks on some additional query-string parameters (Rails does the work of turning a hash into a query string)

<%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token, :utm_medium => 'email', :utm_source => 'mycompany', :utm_content => 'confirmation') %>

There are far more elegant ways to do this, and I think Hassan's solution is a good one, just wanted to provide the raw materials for a working solution :-)

Tom

Flo

unread,
Sep 10, 2012, 1:30:07 AM9/10/12
to plataforma...@googlegroups.com
Thank you very much, good solution. You're right its much better than touching the Devise internals.

Bests,
Flo

Andrés Mejía

unread,
Sep 10, 2012, 1:37:45 PM9/10/12
to plataformatec-d...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages