今日は複数の打撲傷を負っており、同時に 2 つのことを学ぼうとしています... Postmark と Rails HTTP リクエストの API です。
目標: Heroku の Postmark アドオンを使用して本番メールを送信する。
HTTPリクエストに関するこの記事を組み合わせようとしています... http://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html ... PostmarkのこのAPIリファレンス... http: //developer.postmarkapp.com/developer-send-api.html
残念ながら、Postmark の例は curl で作成されており、HTTP 要求に変換することに成功していません。問題はヘッダー周辺に集中しているのではないかと思います。つまり、本体以外のトランスミッションの部分です。
以下のコードに見られるレスキュー句は、エラー「接続がピアによってリセットされました」をトラップします。この時点では、Postmark 認証を提供するヘッダーの正しい形式に近づいているかどうかさえわかりません。
私は適切なサーバー トークンを (config エントリに) 持っており、From メールには必要な Postmark 署名が与えられています。
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
2 回目の試行はこの方法でフォーマットされましたが、同じピア リセット エラーが発生します。
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
ご指導ありがとうございます!