在 Rails 中将 SendCloud 与 'X-SMTPAPI' 一起使用

Using SendCloud with 'X-SMTPAPI' in Rails

我希望这个 post 会有所帮助 someone.I 需要使用 SendCloud 通过 smtp.At 发送电子邮件 我开始在 ActionMailer 中添加 header:

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

但是不行,我也无法通过Rails.Then接收到return错误代码我通过与他们的支持沟通找到了方法:

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

但它也不起作用 correctly.Then 我将生成的 headers["X-SMTPAPI"] 与发送的 headers["X-SMTPAPI"] 进行比较, 发现 Rails 在里面插入 '\n' 为 format.In 结尾, Mail gem 转换 '\n':

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

所以,如果我想成功,我需要这样做:

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

哇,我可以在Rails中成功发送'x-smtpapi'header!

我通过编码和替换'\n'来解决它:

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