In this blog post I would be talking about how to create dynamic templates for sending mails to your app users via mandrill templates. I used mandrill-mailer gem, which is a wrapper for mandrill api for sending transnational emails to users. You can see the official doc’s from git-hub to set-up your gem. Before setting up your gem , you need to have a mailchimp account and integrate mandrill with mailchip, for dynamic templates to be created or edited. You can integrate from here the official page of madrill documentation. Once your smtp settings and configurations are set-up for your mail by using the given by mandrill, your all set to go.
Creation of Dynamic Templates:
You can start by creating a template from your mailchimp account which gives you a lot options to customize the templates. Once you have a desired template, after saving it will be listed. Now we need to push this template to your mandrill account so that you can use it for sending mails to your user using the newly created templates. To do this click on the drop down and click on “Send to Mandrill” that’s it, now this template will be available for your use.
Now we need to move to your mandrill account to edit this template, but there is a back door entry from your mailchimp account to do this.
From your mailchimp dashboard click on:
reports > View mandrill reports(top right had corner) > outbound(on dash board) > templates
On this page you can see your newly created template from mailchimp or you can create a new one. Now for adding dynamic content to this mail and to be sent to your users you need to add few key words as mentioned by the docs. You need to add a editable section to your created templates so that when we send the content from app it will get place here dynamically.
<div mc:edit="dynamic_content"></div>
After setting this in your template you can send dynamic content from your app.The gem doc mentions about a variable to be set ‘template_content’ along with the call to mandrill template. There you have to mention to which editable tag the content is set for. In the call you also mention the name of the template that will be use for sending to your user. example :
mandrill_mail template: 'Template To Test ', subject: 'Template To Test Notifications', to: 'abc@idontknow.com', template_content:{'dynamic_content' => "<span> this is the dynamic content </span>" }, vars: {'LIST_COMPANY' => "xyz" }
Using Dynamic Mandrill Template With Device:
Above I have mentioned how to send dynamic templates using mandrill, the same can be done for sending custom templates for device related mails. The doc tells how to write custom mailer templates using rails action mailer, but here I show you how to do using ‘Mandrill-templates’. I guess the code should completely self explanatory.
class CustomMailer < Devise::Mailer include MailersHelper include Devise::Controllers::UrlHelpers def reset_password_instructions (record, token, opts={}) opts = { :template => 'Custom Template On Mandrill', :template_content => [ { :name => 'dynamic_activities_content', :content => dynamic_mandrill_content_reset_password(record, token) }] } message = { :subject=> "Password Reset", :from_name=> "XYZ", :from_email=>"info@xyz.com", :to=> [{ "email"=> record.email}], :global_merge_vars => [{"name"=>'MAIL_TITLE', "content"=>"Welcome"}] } begin MANDRILL.messages.send_template opts[:template], opts[:template_content], message rescue Mandrill::Error => e Rails.logger.debug("#{e.class}: #{e.message}") raise end end end
That’s it the user user will recieve this dynamic content. Hope this post helps you. Thanks for reading!