Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Fields not Working #22

Closed
mide opened this issue Oct 20, 2014 · 11 comments
Closed

Merge Fields not Working #22

mide opened this issue Oct 20, 2014 · 11 comments

Comments

@mide
Copy link
Contributor

mide commented Oct 20, 2014

I'm trying to send emails through the mailgun-ruby gem, and it doesn't seem to be merging the fields when sent.

I've been able to reproduce the problem with the following code:

require 'mailgun'

mailgun_client = Mailgun::Client.new("[MAILGUN KEY]")

message = Mailgun::BatchMessage.new(mailgun_client, "mail.domain.com")
message.set_from_address("system@mail.domain.com")
message.set_subject("Merge Fields Test")
message.set_text_body("Hello %recipient.first%, this is a test. Your full name is %recipient.first% %last%.")

message.add_recipient(:to, "john.doe@gmail.com", {"first" => "John", "last" => "Doe"})
message.add_recipient(:to, "jane.doe@gmail.com", {"first" => "Jane", "last" => "Doe"})

message.finalize

Each recipient is getting the non-merged emails with all recipients in the "to" address.

@mide
Copy link
Contributor Author

mide commented Oct 20, 2014

I feel like this could be related to #12.

@mide
Copy link
Contributor Author

mide commented Oct 21, 2014

I've traced the Gem's flow to the following code, with the problem still happening. I'm still working on finding the root cause.

http_client = RestClient::Resource.new("https://api.mailgun.net/v2",
                :user       => "api",
                :password   => "[MAILGUN KEY]",
                :user_agent => "mailgun-sdk-ruby/1.0.2")

data = {"from"                 => ["system@mail.domain.com"],
        "subject"              => ["Merge Fields Test"],
        "text"                 => ["Hello %recipient.first%, this is a test. Your full name is %recipient.first% %last%."],
        "to"                   => ["'John Doe' <john.doe@gmail.com>", "'Jane Doe' <jane.doe@gmail.com>"],
        "recipient-variables"  => [ {"system@mail.domain.com" => {"id" => nil},
                                     "john.doe@gmail.com" => {"first" => "John", "last" => "Doe"},
                                     "jane.doe@gmail.com" => {"first" => "Jane", "last" => "Doe"}
                                  ]
        }

response = http_client["mail.domain.com/messages"].post(data)

@mide
Copy link
Contributor Author

mide commented Oct 21, 2014

Well this is interesting: The API documentation shows that

POST /<domain>/messages

does not accept a recipient-variables parameter.

@mide
Copy link
Contributor Author

mide commented Oct 21, 2014

Alright, I've figured out the following works:

http_client = RestClient::Resource.new("https://api.mailgun.net/v2",
               :user       => "api",
               :password   => "[MAILGUN KEY]",
               :user_agent => "mailgun-sdk-ruby/1.0.2")

data = {"from"                 => ["system@mail.domain.com"],
        "subject"              => ["Merge Fields Test"],
        "text"                 => ["Hello %recipient.first%, this is a test. Your full name is %recipient.first% %last%."],
        "to"                   => ["'John Doe' <john.doe@gmail.com>", "'Jane Doe' <jane.doe@gmail.com>"],
        "recipient-variables"  =>  {"system@mail.domain.com" => {"id" => nil},
                                     "john.doe@gmail.com" => {"first" => "John", "last" => "Doe"},
                                     "jane.doe@gmail.com" => {"first" => "Jane", "last" => "Doe"}
                                   }.to_json

        }

response = http_client["mail.domain.com/messages"].post(data)

Mainly I made sure that the recipient-variables was JSON (not a Hash) and it was outside the parent array.

@mide
Copy link
Contributor Author

mide commented Oct 22, 2014

I've added some code to the send_message method that seems to eliminate the problem. I'm not sure if this is the best way to solve it, but the issue is that the recipient-variables are inside an array.

mailgun-ruby/lib/mailgun/messages/batch_message.rb:

...
def send_message(message)
  simple_setter("recipient-variables", JSON.generate(@recipient_variables))

  if @message.has_key?("recipient-variables")
    @message["recipient-variables"] = @message["recipient-variables"].first
  end

  response = @client.send_message(@domain, @message).to_h!
  message_id = response['id'].gsub(/\>|\</, '')
  @message_ids[message_id] = count_recipients()
  reset_message()
end
...

Edit: I've moved the code into batch_message.rb

@mide
Copy link
Contributor Author

mide commented Oct 22, 2014

I've opened PR #23.

@travelton
Copy link
Contributor

👍 I'll review this soon! Thanks for the details.

@mide
Copy link
Contributor Author

mide commented Oct 22, 2014

Thanks! For what it's worth, I've dug a little deeper and I've found that both simple_setter and complex_setter will set variables inside of arrays. It looks like that might be part of the problem, since recipient-variables can't be inside an array.

@erm213
Copy link

erm213 commented Nov 20, 2014

Is there a workaround for this or an ETA for the pull request to be accepted?

@travelton
Copy link
Contributor

This has been merged. Cutting a new Gem in an hour or so. Thanks for your work on this!

@erm213
Copy link

erm213 commented Nov 23, 2014

Works now. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants