Class: MultiMail::Sender::Postmark

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/multi_mail/postmark/sender.rb

Overview

Postmark's outgoing mail sender.

Instance Attribute Summary collapse

Attributes included from Base

#settings, #tracking

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Postmark

Initializes a Postmark outgoing email sender.

Parameters:

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

    required and optional arguments

Options Hash (options):

  • :api_key (String)

    a Postmark API key

See Also:



16
17
18
19
# File 'lib/multi_mail/postmark/sender.rb', line 16

def initialize(options = {})
  super
  @api_key = settings.delete(:api_key)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/multi_mail/postmark/sender.rb', line 9

def api_key
  @api_key
end

Instance Method Details

#deliver!(mail) ⇒ Object

Delivers a message via the Postmark API.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/multi_mail/postmark/sender.rb', line 27

def deliver!(mail)
  parameters = settings.dup
  parameters.delete(:return_response)
  message = MultiMail::Message::Postmark.new(mail).to_postmark_hash.merge(parameters)

  response = Faraday.post do |request|
    request.url 'https://api.postmarkapp.com/email'
    request.headers['Accept'] = 'application/json'
    request.headers['Content-Type'] = 'application/json'
    request.headers['X-Postmark-Server-Token'] = @api_key
    request.body = JSON.dump(message)
  end

  body = JSON.load(response.body)

  unless response.status == 200
    case body['ErrorCode']
    when 10
      raise InvalidAPIKey, body['Message']
    when 300
      case body['Message']
      when "Header 'Content-Type' not allowed."
        raise InvalidHeader, body['Message']
      when "Header 'Date' not allowed."
        raise InvalidHeader, body['Message']
      when "Invalid 'From' value."
        raise MissingSender, body['Message']
      when 'Zero recipients specified'
        raise MissingRecipients, body['Message']
      when 'Provide either email TextBody or HtmlBody or both.'
        raise MissingBody, body['Message']
      else
        raise InvalidMessage, body['Message']
      end
    else
      raise InvalidRequest, body['Message']
    end
  end

  if settings[:return_response]
    body
  else
    self
  end
end