Rails 4, Devise & Mandrill emails

2019-06-16 10:42发布

问题:

I'm trying to make an app in Rails 4.

For the past 3 years, I've been struggling to figure out devise/omniauth (I am still trying to get it to work).

Stepping aside from the main problems while I try and find the will to live through this, I've tried to setup emails with Mandrill.

I found this tutorial, which I am trying to follow along: https://nvisium.com/blog/2014/10/08/mandrill-devise-and-mailchimp-templates/

I have a mailer called mandrill_devise_mailer.rb

class MandrillDeviseMailer < Devise::Mailer

  def confirmation_instructions(record, token, opts={})
    # code to be added here later
  end

  def reset_password_instructions(record, token, opts={})
    options = {
      :subject => "Reset your password",
      :email => record.email,
      :global_merge_vars => [
        {
          name: "password_reset_link",
          # content: "http://www.example.com/users/password/edit?reset_password_token=#{token}"
          content: "http://www.cr.com/users/password/edit?reset_password_token=#{token}"

        },

        {
          name: "PASSWORD_RESET_REQUEST_FROM",
          content: record.full_name 
        }
      ],
      :template => "Forgot Password"
    }
    mandrill_send options  
  end

  def unlock_instructions(record, token, opts={})
    # code to be added here later
  end

  def mandrill_send(opts={})
    message = { 
      :subject=> "#{opts[:subject]}", 
      :from_name=> "Reset Instructions",
      # :from_email=>"example@somecorp.com",
      :from_email=>["PROD_WELCOME"],
      :to=>
            [{"name"=>"#{opts[:full_name]}",
                "email"=>"#{opts[:email]}",
                "type"=>"to"}],
      :global_merge_vars => opts[:global_merge_vars]
      }
    sending = MANDRILL.messages.send_template opts[:template], [], message
    rescue Mandrill::Error => e
      Rails.logger.debug("#{e.class}: #{e.message}")
      raise
  end
end

The differences between the above and what they have done in the tutorial are:

In my mail chimp mandrill template, I have:

<a href="*|password_reset_link|*">Change my password </a>

When I receive the email to reset the instructions, I get an underlined link to the change password form, which says 'change my password next to it. I want 'change my password to be the label which conceals the link text'.

Can anyone see what I've done wrong?

回答1:

Here is how I created custom DeviseMailer

class MyDeviseMailer < Devise::Mailer   
  default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views

  def reset_password_instructions(record, token, opts={})
    opts['from_email'] = "donotreply@mywebsite.com"
    opts['from_name'] = "Password Reset"
    #Rails.logger.mail.info "reset_password_instructions #{record.to_json} \n #{token.to_json} \n #{opts.to_json}"
    super
  end

end

https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer and Add dynamic value in devise email subject