Thursday, July 31, 2014

Using Mandrill Send Template from Rails

Last time I posted I covered creating a method that can invoke the Mandrill API to send an e-mail message via a template. This time I'll cover how I actually used it.
First off, I used Mail Chimp to create our outbound messages. You need to create both a Mail Chimp account and a Mandrill account. Mail Chimp then allows you to link the two accounts together. Mail Chimp uses merge tags of the form: *|attribute_name|*. They are case insensitive and I used only letters and underscore in naming them.
Mail Chimp will include a few extra meta tags like if statements: *|IFNOT:ARCHIVE_PAGE|*and some common variable such as: *|LIST:COMPANY|*. The above if statement is used to alter content in the message when displaying a copy of it on a website instead of the recipient using their mail client directly. I opted not to implement that capability at this time so those tags get removed when I edit the message in Mandrill.
After saving the templates, use the Send To Mandrill option on the Edit button in Mail Chimp. This will create a copy of the template in Mandrill. At this time the GUI integration between Mail Chimp and Mandrill isn't very tight so you may have to create copies of templates and export them to Mandrill to do updates. I hope Mail Chimp will tighten this integration up soon.
Once the template is exported to Mandrill, switch over to your Mandrill account and edit the template. Remove the IF tags that I mentioned. Also, remove any other default tags that you don't want. I opted to keep the *|LIST:COMPANY|* tag, however, Mandrill doesn't recognize that tag format (another area I hope Mail Chimp will tighten up) so I replaced colons with underscores like: *|LIST_COMPANY|*. Note the template slug name. This is the name by which you will invoke this message.
Now we have a template that we are ready to send. Over in Ruby we need to invoke our sendtemplate method. So here it is: Again, sorry about the formatting, I don't think Linkedin intended for code examples to be included.
def sendtouser(user)
  subject = "Welcome and thank you for signing up!"
  mandrillsend = Mandrill::Mandrill.new
  result = mandrillsend.sendtemplate(
    "getting-started", # Mandril slug name
    Rails.configuration.returnaddress, # return email address
    Rails.configuration.returnname, # name to show for return
    user.email, # target email address
    user.full_name, # name to show for target
    subject, # Message subject
    [{"name" => "subject", "content" => subject},
      {"name" => "CURRENT_YEAR","content" => Time.now.year.to_s},
      {"name" => "LIST_COMPANY","content" => "My Company, LLC."},
      {"name" => "HTML_LIST_ADDRESS_HTML","content" => OfficeAddress},
      {"name" => "unsub","content" => Rails.application.routes.url_helpers.url_for(:host => Rails.configuration.serverroot, only_path: false, controller: "contacts", action: "unsub", id: encrypted_id)},
      {"name" => "UPDATE_PROFILE","content" => Rails.application.routes.url_helpers.url_for(:host => Rails.configuration.serverroot, only_path: false, controller: "users", action: "edit", id: user.id)}
    ]
  )
    
  Rails.logger.debug("--- manmailer.signed_up result")
  Rails.logger.debug(result.code)
  Rails.logger.debug(result.body)
  Rails.logger.debug("-------------------------------)
  
  status = result.code
  
  result_body = result.body
  
  return status.to_i == 200 # expect back 200 on the return code
end # sendtouser
All of the replacement variables are referenced by name (CURRENT_YEAR) in an array [] of name value pair hashes {}.
So you can see that {"name" => "CURRENT_YEAR","content" => Time.now.year.to_s} will result in the variable *| CURRENT_YEAR|* being replaced by the value generated by Time.now.year.to_s.
I hope this post, when coupled with my last post might help get a few folks integrating with Mandrill.

No comments:

Post a Comment