Class: PostmailRuby::DeliveryMethod::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/postmail_ruby/delivery_method/http.rb

Overview

Implements a delivery method for Action Mailer that sends messages via the Postal HTTP API. Instead of speaking SMTP directly, this class packages the email into a JSON payload and posts it to the configured Postal endpoint. An API key must be provided via configuration for authentication.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HTTP

Initialize the HTTP delivery method. Accepts a hash of options that may override configuration defaults. Options are stored but not used directly; configuration is read from PostmailRuby.config for each delivery to ensure the most current environment variables are respected.

Parameters:

  • options (Hash) (defaults to: {})

    delivery options (currently unused)



25
26
27
# File 'lib/postmail_ruby/delivery_method/http.rb', line 25

def initialize(options = {})
  @settings = options
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



16
17
18
# File 'lib/postmail_ruby/delivery_method/http.rb', line 16

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object

Deliver a Mail::Message via the Postal API. Builds a JSON payload including recipients, subject, plain and HTML parts and attachments. Sends the payload as a POST request with the API key in the X-Server-API-Key header. Raises an exception if the request returns a non-success response.

Parameters:

  • mail (Mail::Message)

    the message to send



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/postmail_ruby/delivery_method/http.rb', line 36

def deliver!(mail)
  config = PostmailRuby.config
  uri = URI.parse(config.api_endpoint)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  http.read_timeout = 15
  http.open_timeout = 5

  request = Net::HTTP::Post.new(uri.request_uri, {
                                  'Content-Type' => 'application/json',
                                  'X-Server-API-Key' => config.api_key.to_s
                                })
  request.body = build_payload(mail, config).to_json

  response = http.request(request)
  unless response.is_a?(Net::HTTPSuccess)
    raise "Postal API responded with status #{response.code}: #{response.body}"
  end

  response
end