Rails 在 Heroku 上使用邮戳 API 的电子邮件 -- 连接由对等方重置

Rails email using Postmark API on Heroku -- connection reset by peer

我今天身上多处瘀伤,试图同时学习两件事...API 邮戳和 Rails HTTP 请求。

目标:使用 Postmark add-on 让 Heroku 发送生产电子邮件。

我正在尝试结合这篇关于 HTTP 请求的文章... http://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html ...使用此 API 邮戳参考... http://developer.postmarkapp.com/developer-send-api.html

不幸的是,Postmark 中的示例是在 curl 中完成的,我没有成功将它们转换为 HTTP 请求。我怀疑问题集中在 headers -- 除了 body.

之外的传输部分

下面代码中的 rescue 子句捕获了错误 'connection reset by peer'。在这一点上,我不知道我是否接近提供邮戳身份验证的 headers 的正确格式。

我有正确的服务器令牌(在配置条目中)并且发件人电子邮件已获得所需的邮戳签名。

   def send_production_email(email_address, subject, email_body)

      # Use API to interact with Heroku add-on Postmark
      # http://developer.postmarkapp.com/developer-send-api.html

      uri = URI('https://api.postmarkapp.com/email')

      # Form the request
      req = Net::HTTP::Post.new(uri)

      # Set request headers -- SUSPECT THIS IS WRONG
      req['Accept'] = 'application/json'
      req['Content-Type'] = 'application/json'
      req['X-Postmark-Server-Token'] = Rails.application.config.postmark_token

      rbody ={
          'From' => 'Support <michael@mydomain.com>',
          'To' => email_address,
          'Subject' => subject,
          'HtmlBody' => wrap_html(email_body),
          'TextBody' => email_body
      }.to_json

      req.body = rbody

      # Send the request, waiting for the response

      begin
         response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
      rescue Exception => e
         logthis("http request error: #{e.message}")
         return
      end

      # ...parsing section omitted since I do not get that far...

   end

第二次尝试以这种方式格式化,但导致相同的对等重置错误:

      rbody ={
          'From' => 'Support <michael@disambiguator.com>', # TODO: replace email when domain is live
          'To' => email_address,
          'Subject' => subject,
          'HtmlBody' => wrap_html(email_body),
          'TextBody' => email_body
      }.to_json

      uri = URI('https://api.postmarkapp.com/email')

      http = Net::HTTP.new(uri.host, uri.port)
      # http.use_ssl = true

      request = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Postmark-Server-Token' => Rails.application.config.postmark_token})
      request.body = rbody

      # Send the request, waiting for the response

      begin
         response = http.request(request)
      rescue Exception => e
         logthis("http request error: #{e.message}")
         return
      end

非常感谢您的指导!

我是 Wildbit 的员工,也是 official Postmark Ruby gem.

的维护者

"connection reset by peer" 错误是由于您尝试将未加密的 HTTP 请求发送到期望通过 HTTPS 进行安全通信的端点而导致的。因此,如果您更改此行:

Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }

至:

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.start { |http| http.request(req) }

那么您应该能够收到来自 API 的回复。我看到你在第二个例子中有这一行,但它被评论了。既然你是作为练习来做的,我想补充一点,当使用 net/http 时,你通常不必使用底层的 classes,比如 Net::HTTP::Post。使用 Net::HTTP class 的实例提供的更高级别 API 通常更简单。以下是如何使用它简化您的方法的示例:

def send_production_email(email_address, subject, email_body)
  uri = URI('https://api.postmarkapp.com/email')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  headers = {'Accept' => 'application/json',
             'Content-Type' => 'application/json',
             'X-Postmark-Server-Token' => Rails.application.config.postmark_token}
  payload = {'From' => 'tema@wildbit.com',
             'To' => email_address,
             'Subject' => subject,
             'HtmlBody' => email_body,
             'TextBody' => email_body}

  http.post(uri.request_uri, payload.to_json, headers)
rescue => e
  puts "http request error: #{e.message}"
end

而且,如果您对如何在官方邮戳 Ruby gem 中使用 net/http 感兴趣,请查看 HttpClient class’ source