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

Methods included from Base

included

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.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/multi_mail/postmark/sender.rb', line 49

def deliver!(mail)
  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

#parametersHash

Returns the additional parameters for the API call.

Returns:

  • (Hash)

    the additional parameters for the API call



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/multi_mail/postmark/sender.rb', line 24

def parameters
  parameters = settings.dup
  parameters.delete(:return_response)

  if tracking.key?(:opens)
    parameter = :TrackOpens
    case tracking[:opens]
    when true, false, nil
      parameters[parameter] = tracking[:opens]
    when 'yes'
      parameters[parameter] = true
    when 'no'
      parameters[parameter] = false
    end # ignore "htmlonly"
  end

  parameters
end