8

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?

Mel
  • 2,481
  • 26
  • 113
  • 273
  • Your `:from_email` is set to `["PROD_WELCOME"]` which doesn't look like an email address to me. – VNO Jan 16 '16 at 00:54
  • Thanks - setting the ENV made the email send, but it prints the words in between the *||* rather than pulling in the variable – Mel Jan 16 '16 at 01:06
  • Maybe you're using handlebars as your default merge language? Go to your Mandrill [sending options](https://mandrillapp.com/settings/sending-options) under settings and check if you have the right merge language enabled. – Dylan Moore Jan 17 '16 at 18:38
  • Hi Dylan, the settings were set to Mailchimp. I changed them to handlebars, but the preview still shows the error – Mel Jan 19 '16 at 07:15
  • Pretty sure you *don't* want handlebars. The mergetag syntax you're using above uses the Mailchimp format. – Anthony E Mar 30 '16 at 05:05
  • Hi Anthony - what do you suggest instead? My mandrill is now merged with my mail chimp but i can't get it to work – Mel Mar 30 '16 at 08:13
  • What does the html look like for the anchor tag. The only difference i see is the target='_blank' which should not cause the above behavior. – Shishir Apr 05 '16 at 01:33
  • Hi Shishir, what do you mean? Sorry - I don't understand which html you want to see. – Mel Apr 05 '16 at 03:09
  • Are you sending "test" emails or actual emails? I remember using Mailchimp and facing the same problem. It was because I was sending my "campaigns" to my test emails. In that case, Mailchimp would not replace placeholders with user data. – Uzbekjon Apr 05 '16 at 04:16
  • I'm sending actual emails. I've managed to get the name field to populate with dynamic data (for others struggling with the same problem -you need to set the sending defaults to mail chimp) but that doesnt solve the problem with the link – Mel Apr 05 '16 at 04:39

1 Answers1

-2

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

Community
  • 1
  • 1
Dmitry Polyakovsky
  • 1,535
  • 11
  • 31
  • Hi Dmitry, it doesn't look like you're trying to use mailchimp/mandrill. Thanks anyway. – Mel Jan 27 '16 at 06:31
  • Mandrill API required those opts['from_email'] and opts['from_name'] params. I added custom delivery class per http://www.alanverga.com/blog/2014/01/03/custom-rails-mailer-and-mandrill and https://robots.thoughtbot.com/how-to-send-transactional-emails-from-rails-with-mandrill – Dmitry Polyakovsky Jan 28 '16 at 17:14
  • But I also have those options my file. My problem is that the links don't format as links the way I have them expressed. Instead, the link appears inside visible a tags, with the label of the link next to it as text. – Mel Jan 29 '16 at 00:42