Sending MailChimp email with Rails

2.2k views Asked by At

((I've found similar questions to mine, but they are specific to a 3rd party mandrill gem, or Heroku. I'm not using any of those, just the mandrill-api itself.))

I'm trying to use email confirmation (we use Devise) for account creation. I've set up confirmable, everything with that works well. Initially, I used the basic Devise emails for everything, and it was fine and sending - but then I tried to switch it to our MailChimp/Mandrill account, and emails won't send. I've tried everything I can think of, and I'm stuck.

Note that it seems to communicate with Mandrill - when I delete the confirmation-instructions template from MailChimp, or don't send it over to Mandrill, I get a template not found error. But when I create the template, it never actually sends.

The only thing in the logs is:

NotificationMailer#confirmation_instructions: processed outbound mail in 5676.4ms

The mailer:

class NotificationMailer < ActionMailer::Base
    require 'mandrill'
    default from: "XXXXXXXXX <[email protected]>"

    def mandrill_client
        @mandrill_client ||= Mandrill::API.new MANDRILL_API_KEY
    end

    def confirmation_instructions(record, token, opts={})
        template_name = "confirmation-instructions"
        template_content = []
        message = {
            to: [{email: record.email}],
            subject: "Confirmation Instructions",
            var_user_email: record.email,
            merge_vars: [
            {rcpt: record.email,
                vars: [
                    {name: "USER_EMAIL", content: record.email},
                    {name: "CONFIRMATION_LINK", content: user_confirmation_url(confirmation_token: token)}
                ]
            }]
        }
        mandrill_client.messages.send_template template_name, template_content, message
    end

end

Development.rb file relative parts:

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true

config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: '192.168.33.111' }

config.action_mailer.smtp_settings = {
  address: "smtp.mandrillapp.com",
  port: 587,
  enable_starttls_auto: true,
  user_name: "XXXXXXXXXXXXXXXXX",
  password: "XXXXXXXXXXXXXXXXXX",
  authentication: "login",
}

I also tested it from the console, and it gave no errors there either. There seem to be no errors at all, but the email never sends. Any ideas?

1

There are 1 answers

0
Chris Burrus On BEST ANSWER

So after much pain, I'm still not entirely sure what was wrong with the mailer format originally, but I changed it a little basing it on other websites approaches, to the following, and it worked. I changed nothing else.

class NotificationMailer < ActionMailer::Base
  require 'mandrill'

  default from: "XXXXX <XXXXX@XXXXX>"

  def mandrill_client
    @mandrill_client ||= Mandrill::API.new XXXXXXXXXXXX
  end

  def confirmation_instructions(record, token, opts={})   
    template_name = "confirmation-instructions"
    template_content = []
    message = {
      :subject=> "Confirmation Instructions",
      :from_name=> "XXXXXXXXX",
      :text=> "Confirm your email",
      :to=>[
        {
          :email=> record.email,
          :name=> record.first_name + " " + record.last_name
        }
      ],  
      :from_email=>"[email protected]",
      :merge_vars=>[
        {
          rcpt: record.email,
          vars: 
          [
            {name: "USER_EMAIL", content: record.email},
            {name: "USER_NAME", content: record.first_name + " " + record.last_name},
            {name: "CONFIRMATION_LINK", content: user_confirmation_url(confirmation_token: token)}
          ]
        }
      ]
    }
    mandrill_client.messages.send_template template_name, template_content, message
  end
end

Note that the

:text=> "Confirm your email" 

field isn't used, I'm still tweaking it - just wanted to post this so others may be spared the pain I was from MailChimp/Mandrill's rediculously tedious formatting and terrible documentation.

And just for info, the only gem i used was the mandrill-api gem:

gem 'mandrill-api', '~> 1.0.51', require: "mandrill"