Search code examples
rubystringsmtpmandrillmailer

Ruby Quotes Syntax when using Mandrill


I have a Ruby on Rails app and I am writing my mailer to send SMTP headers to Mandrill. I think I have an issue with getting a variable into a string:

class MyMailer < Devise::Mailer

helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`

def confirmation_instructions(record, token, opts={})

     headers['X-MC-MergeVars'] = '{"ctoken1": "Drip Email"}'

     temp_var = "Hello Everyone"
     headers['X-MC-MergeVars'] = '{"tag_test_1": "yoyoyo"}'           # 1

    # headers['X-MC-MergeVars'] = "{'tag_test_1': 'yoyoyo'}"          # 2
    # headers['X-MC-MergeVars'] = "{'tag_test_1': #{temp_var}}"       # 3
    #  headers['X-MC-MergeVars'] = '{"tag_test_1": #{temp_var}}'      # 4

     headers['X-MC-MergeVars'] = '{"ctoken3": "Test at the End"}'

     super

end

end

I want to get the "temp_var" variable into the header. You can see my 4 attempts.

Firstly - a sanity check - The syntax in #1 works fine - the variable "tag_test_1" in my mandrill email gets replaced by "yoyoyo" but of course this is not using a ruby variable.

None of my attempts 2,3 or 4 work. My Mandrill email receives TAG_TEST_1 rather than the variable replacement ("Hello Everyone").

Ultimately what I am trying to do is get the Account Activation link into an email via Mandrill. So I think I will need to send the 'token' variable, but for now I cannot seem to get a known variable (temp_var) to get passed to the email.

My suspicion is that I am not understanding the Ruby string syntax correctly.

Any help appreciated!


Solution

  • I would try this:

    headers['X-MC-MergeVars'] = "{\"tag_test_1\": \"#{temp_var}\"}"
    

    Or this:

    headers['X-MC-MergeVars'] = %Q'{"tag_test_1": "#{temp_var}"}'