1

I'd like to work with mailchimp templetes with Ruby. I installed gem mailchimp-api and tried this:

mailchimp = Mailchimp::API.new(ENV['MAILCHIMP_API_KEY'])
lists = mailchimp.templates.list
list  = lists['data']
puts list

It returns lists = Hash (3 elements): 'user'= Empty Array, 'gallery'= Empty Array, 'base'= Empty Array, and as the result list = nil.

I installed gem gibbon and tried this:

gibbon = Gibbon::Request.new(api_key: ENV['MAILCHIMP_API_KEY'])
templates =  gibbon.templates.retrieve(params: {type: 'user'})
templates['templates'].each do |template|
  if template['name'] == 'my_template'
    id = template['id']

I get the template id, but gibbon.templates.info(template_id: id) returns # with @api_endpoint=nil. How can I recieve the template source and used it?

Many many thanks.

dlx
  • 59
  • 7

2 Answers2

0

Don't know exactly but maybe you should use retrieve to find template by id.
Something like

gibbon.templates.retrieve(params: { template_id: your_id_here })

According to official docs.

Aleksey
  • 2,289
  • 17
  • 27
  • It returns first 10 templates from the all of templates( – dlx Jun 27 '16 at 08:35
  • and what about `gibbon.templates(your_id_here).retrieve`? – Aleksey Jun 27 '16 at 08:55
  • Sure it works fine, but this is the same as `template` in my loop `templates['templates'].each` when `if template['name'] == 'my_template'` returns `true`. I didn't find examples about how to use templates with Ruby. – dlx Jun 27 '16 at 10:22
  • I mean example about how to work with template source and so on. – dlx Jun 27 '16 at 10:27
  • Try to debug [this](https://bitbucket.org/mailchimp/mailchimp-api-ruby/src/37dbe82057b96e135881c9379c0a3d5f8f32bf1f/lib/mailchimp.rb?at=master&fileviewer=file-view-default#mailchimp.rb-41) place. You need to see what `r.body` contains. That is what your `lists` is equal to. – Aleksey Jun 27 '16 at 11:16
  • I found the solution. Mailchimp account can be linked with mandrill account to send transactional mails. The solution is to clone template from mailchimp to mandrill and use mandrill-api gem to load template body, like this: `def mandrill_template(template_name, attributes) mandrill = Mandrill::API.new(ENV["SMTP_PASSWORD"]) mandrill.templates.render(template_name, [], merge_vars)["html"] end' – dlx Jun 27 '16 at 11:30
  • Congrats! But this seems like too mush workaround)) I recommend you to debug source code if will have vacant time – Aleksey Jun 27 '16 at 11:33
0

I found the solution. Mailchimp account can be linked with mandrill account to send transactional mails. The solution is to clone template from mailchimp to mandrill and use mandrill-api gem to load template body, like this:

 mandrill = Mandrill::API.new(ENV["SMTP_PASSWORD"])
 body = mandrill.templates.render(template_name, [], vars )["html"]
dlx
  • 59
  • 7