简体   繁体   中英

Rails 4 Mailchimp Mandrill with Devise

I am trying to setup Rails 4 with my devise and manrill/mailchimp (merged) accounts.

I want to send a devise confirmation email from my mailchimp/mandrill account.

My heroku logs show this message:

MandrillDeviseMailer#confirmation_instructions: processed outbound mail in 504.2ms

However I don't get the email.

I can't figure out how to setup my code/mandrill/mailchimp accounts to work.

I have a mailer as follows:

class MandrillDeviseMailer < Devise::Mailer
  default from: "hello@testhub.com"

    require "mandrill"

  def confirmation_instructions(record, token, opts={})
    # code to be added here later
      options = {
      :subject => 'Confirm your account',
      :email => record.email,
      :name => record.formal_name,
      :global_merge_vars => [
        {
          name: 'email',
          content: record.email
        },
        {
          name: 'confirmation_link',
          content: record.confirmation_token
        }
      ],
      :template => 'devise-confirmation-instructions'
    }
    mandrill_send(options)
  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.testhub.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=> "Welcome aboard",
      :from_email=>ENV["OD_WELCOME"],
      :to=>
            [{"name"=>"#{opts[:formal_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

I have my production.rb setup with:

config.action_mailer.smtp_settings = {
    :port => "587",
    :address => "smtp.mandrillapp.com",
    :user_name => ENV['OD_WELCOME'],
    :password => ENV['ROD_WELCOME'],
    :domain => "testhub.com",
    :authentication => :plain }

I can't figure out how to setup my email templates in mandrill /mailchimp so that these variables work.

I have tried several variations, but instead of populating the variable with the dynamic input, the email is sent with pipes and ruby tags and text which should be complied as code.

In my mailchimp template, I have:

Hi <%= @user.formal_name %>

You can confirm your account email through the link below:

That comes through as text as it appears above. I have also tried putting the ruby tags inside ||tags, but those just print out too.

Does anyone know how to set this up?

Further attempt

I have also tried setting my mail chimp template to use merge tags as:

|password_reset_link |

When I try this, in production, it sends an email with this printed in the body:

<a href="*|password_reset_link|*">Change my password </a>
  1. Your mailer probably works fine but here is a commented version of mine:

     /app/mailers/mandrill_devise_mailer.rb require "mandrill" class MandrillDeviseMailer < Devise::Mailer default( from: "email@yourdomain.com", reply_to: "email@yourdomain.com" ) def reset_password_instructions(record, token, opts={}) subject = "Password reset link from Your App" # These become available in your Mailchimp template and can be used # using the usual *|DISPLAY_NAME|* merge_vars = { "PASSWORD_RESET_LINK" => "https://www.yourdomain.com/users/password/edit?reset_password_token=#{token}", "DISPLAY_NAME" => record.display_name || record.email } # The name of your template in Mailchimp # make sure to "send to mandrill" from the edit dropdown body = mandrill_template("Password Reset Template", merge_vars) send_mail(record.email, subject, body) end private def send_mail(email, subject, body) mail(to: email, subject: subject, body: body, content_type: "text/html") end def mandrill_template(template_name, attributes) mandrill = Mandrill::API.new(ENV["SMTP_PASSWORD"]) merge_vars = attributes.map do |key, value| { name: key, content: value } end mandrill.templates.render(template_name, [], merge_vars)["html"] end end
  2. Your config looks okay too but here is mine:

     # /config/environments/production.rb config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: ENV["SMTP_ADDRESS"], authentication: :plain, domain: ENV["SMTP_DOMAIN"], enable_starttls_auto: true, password: ENV["SMTP_PASSWORD"], port: "587", user_name: ENV["SMTP_USERNAME"] } config.action_mailer.default_url_options = { host: ENV["SMTP_DOMAIN"] }
  3. What you might be missing is the devise.rb initializer changes which need to look like this:

     config.mailer_sender = "email@yourdomain.com" # Configure the class responsible to send e-mails. config.mailer = 'MandrillDeviseMailer'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM