mercredi 29 juillet 2015

Using SendCloud with 'X-SMTPAPI' in Rails

I hope this post will help someone.I need to use SendCloud to send emails through smtp.At the begin i add header in ActionMailer:

headers["X-SMTPAPI"] = JSON.dump({"to" => emails, "sub" => {"%name%" => names}})

But it can't work, and i also can't receiver the return error code through Rails.Then i find the way through communication with their support:

headers["X-SMTPAPI"] = Base64.encode64(JSON.dump({"to" => emails, "sub" => {"%name%" => names}}))

But it also can't work correctly.Then i compare the generated headers["X-SMTPAPI"] with the sent headers["X-SMTPAPI"], and found Rails insert '\n' in it for format.In the end, Mail gem convert the '\n':

def encode_crlf(value)
  value.gsub!(CR, CR_ENCODED)
  value.gsub!(LF, LF_ENCODED)
  value
end

So, if i want it's success, i need to do like this:

headers["X-SMTPAPI"] = Base64.encode64(JSON.dump({"to" => emails, "sub" => {"%name%" => names}})).gsub!(/\n/,'')

Wow, i can send 'x-smtpapi' header in Rails successfully!

Aucun commentaire:

Enregistrer un commentaire