Class: Smtp2go::Smtp2goClient

Inherits:
Object
  • Object
show all
Defined in:
lib/smtp2go/core.rb

Overview

Ruby Library for interacting with the smtp2go API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSmtp2goClient

Returns a new instance of Smtp2goClient.



11
12
13
14
15
16
# File 'lib/smtp2go/core.rb', line 11

def initialize
  @api_key = ENV['SMTP2GO_API_KEY']
  @headers = HEADERS
  @send_endpoint = SEND_ENDPOINT
  raise Smtp2goAPIKeyException unless @api_key
end

Instance Attribute Details

#headersObject (readonly)

Client to handle API interfacing



10
11
12
# File 'lib/smtp2go/core.rb', line 10

def headers
  @headers
end

#send_endpointObject (readonly)

Client to handle API interfacing



10
11
12
# File 'lib/smtp2go/core.rb', line 10

def send_endpoint
  @send_endpoint
end

Instance Method Details

#send(sender:, recipients:, subject:, text: nil, html: nil) ⇒ Smtp2goResponse

Returns response object.

Parameters:

  • sender (String)

    the from email address

  • recipients (Array <String>)

    the email address of the recipient(s)

  • subject (String)

    the email subject

  • text (String) (defaults to: nil)

    the email text content (optional if html is passed)

  • html (String) (defaults to: nil)

    the email html content (optional if text is passed)

Returns:

Raises:



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

def send(sender:, recipients:, subject:, text: nil, html: nil)
  raise Smtp2goParameterException unless [html, text].any?
  payload = {
    api_key: @api_key,
    sender: sender,
    to: recipients,
    subject: subject,
    text_body: text,
    html_body: html
  }
  response = HTTParty.post(
    @send_endpoint,
    body: JSON.dump(payload),
    headers: @headers
  )
  Smtp2goResponse.new response
end