0

I am migrating from Mandrill to SparkPost and have a Rails back-end.

The data structure I currently have is the following --

message = {
  subject: "Welcome",
  merge_vars: [{
    rcpt: user.email,
    vars: email_vars(user)
  }],
  to:[{
    email: user.email,
    name: user.name
  }],
  track_clicks: true,
  track_opens: true,
  inline_css: true,
}

This sends the response --

m = Mandrill::API.new
template_content = []
result = m.messages.send_template 'email-confirmation', template_content, message

Would I need to update the JSON data structure at all? Once JSON is good, how do I pass values to specific template with SparkPost?

I attempted the following --

m = SparkPost::Client.new()
template_content = []
result = m.messages.send_template 'email-confirmation', template_content, message

But I have also seen this --

host = 'https://api.sparkpost.com'

SparkPost::Request.request("#{host}/api/v1/transmissions", API_KEY, {
  recipients: [
    {
      address: { email: user.email },
      substitution_data: {
        first_name: user.name,
        email: user.email
      }
    }
  ],
  content: {
    template_id: 'email-confirmation'
  },
  substitution_data: {
    name: user.name,
    email: user.email
  }
})

Appreciate the help!

Yasir
  • 879
  • 5
  • 13
  • 31

1 Answers1

1

If you're using the official gem, it has a convenient method called send_payload which you can use to send a prepared payload.

The substitution_data inside recipients collection is a per recipient substitution.

For example, I've following templates.

template

To send using this template, this is my complete code

sp = SparkPost::Client.new() # pass api key or get api key from ENV

payload  = {
  recipients: [
    {
      address: { email: 'RECIPIENT1' },
      substitution_data: {
        name: 'User one',
        username: 'userone'
      }
    }, {
      address: { email: 'RECIPIENT2' },
      substitution_data: {
        name: 'User two',
        username: 'user2'
      }
    }
  ],
  content: {
    template_id: 'test-template'
  },
  substitution_data: {
    company: 'Awesome company'
  }
}

response = sp.transmission.send_payload(payload)
p response

The email will look like

Hello User one, Your username, userone, is created. Thanks Awesome company

HungryCoder
  • 7,506
  • 1
  • 38
  • 51
  • thanks! this is what i was looking for. appreciate it – Yasir Apr 25 '16 at 18:32
  • user = User.find user_id sp = SparkPost::Client.new(ENV['SPARKPOST_API_KEY']) message = { recipients: [ { address: { email: user.email }, substitution_data: { name: user.name, email: user.email } } ], content: { template_id: 'email-confirmation' } } response = sp.transmission.send_payload(message) p resonse – Yasir Apr 25 '16 at 23:10
  • I have the following, doing some tests, not getting emails, does this look correct? – Yasir Apr 25 '16 at 23:10
  • okay, with the api key add all works! thanks for your help again – Yasir Apr 26 '16 at 17:28
  • @HungryCoder how does the translation work when using the template ? – BilalReffas Jan 27 '17 at 01:30