Class: Pepipost::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/pepipost/controllers/email_controller.rb

Constant Summary collapse

@@instance =
Email.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject

Singleton instance of the controller class



5
6
7
# File 'lib/pepipost/controllers/email_controller.rb', line 5

def self.instance
  @@instance
end

Instance Method Details

#create_api_web_send_json(data) ⇒ Object

‘Sending Mails` – This API is used for sending emails. Pepipost supports REST as well JSON formats for the input. This is JSON API.

Parameters:

  • data (Emailv1)

    Required parameter: Data in JSON format

Returns:

  • mixed response from the API call



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pepipost/controllers/email_controller.rb', line 83

def create_api_web_send_json(data)
  # the base uri for api requests
  query_builder = Configuration.base_uri.dup

  # prepare query string for API call
  query_builder << '/api/web.send.json'

  # validate and preprocess url
  query_url = APIHelper.clean_url query_builder

  # prepare headers
  headers = {
    'user-agent' => 'APIMATIC 2.0',
    'accept' => 'application/json',
    'content-type' => 'application/json; charset=utf-8'
  }

  # invoke the API call request to fetch the response
  response = Unirest.post query_url, headers: headers, parameters: data.to_json

  #Error handling using HTTP status codes
  if !response.code.between?(200, 206) # [200,206] = HTTP OK
    raise APIException.new 'HTTP Response Not OK', response.code, response.raw_body
  end

  response.body
end

#get_api_web_send_rest(api_key, content, from, recipients, subject, att_name = nil, attachmentid = nil, bcc = nil, clicktrack = true, footer = true, fromname = nil, opentrack = true, replytoid = nil, tags = nil, template = nil, x_apiheader = nil) ⇒ Object

‘Sending Mails` – This API is used for sending emails. Pepipost supports REST as well JSON formats for the input

Parameters:

  • api_key (String)

    Required parameter: Your API Key

  • content (String)

    Required parameter: Email body in html (to use attributes to display dynamic values such as name, account number, etc. for ex. [% NAME %] for ATT_NAME , [% AGE %] for ATT_AGE etc.)

  • from (String)

    Required parameter: From email address

  • recipients (String)

    Required parameter: Email addresses for recipients (multiple values allowed)

  • subject (String)

    Required parameter: Subject of the Email

  • att_name (String) (defaults to: nil)

    Optional parameter: Specify attributes followed by ATT_ for recipient to personalized email for ex. ATT_NAME for name, ATT_AGE for age etc. (Multiple attributes are allowed)

  • attachmentid (String) (defaults to: nil)

    Optional parameter: specify uploaded attachments id (Multiple attachments are allowed)

  • bcc (String) (defaults to: nil)

    Optional parameter: Email address for bcc

  • clicktrack (Boolean) (defaults to: true)

    Optional parameter: set ‘0’ or ‘1’ in-order to disable or enable the click-track

  • footer (Boolean) (defaults to: true)

    Optional parameter: Set ‘0’ or ‘1’ in order to include footer or not

  • fromname (String) (defaults to: nil)

    Optional parameter: Email Sender name

  • opentrack (Boolean) (defaults to: true)

    Optional parameter: set open-track value to ‘0’ or ‘1’ in-order to disable or enable

  • replytoid (String) (defaults to: nil)

    Optional parameter: Reply to email address

  • tags (String) (defaults to: nil)

    Optional parameter: To relate each message. Useful for reports.

  • template (Numeric) (defaults to: nil)

    Optional parameter: Email template ID

  • x_apiheader (String) (defaults to: nil)

    Optional parameter: Your defined unique identifier

Returns:

  • mixed response from the API call



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
72
# File 'lib/pepipost/controllers/email_controller.rb', line 27

def get_api_web_send_rest(api_key, content, from, recipients, subject, att_name = nil, attachmentid = nil, bcc = nil, clicktrack = true, footer = true, fromname = nil, opentrack = true, replytoid = nil, tags = nil, template = nil, x_apiheader = nil)
  # the base uri for api requests
  query_builder = Configuration.base_uri.dup

  # prepare query string for API call
  query_builder << '/api/web.send.rest'

  # process optional query parameters
  query_builder = APIHelper.append_url_with_query_parameters query_builder, {
    'api_key' => api_key,
    'content' => content,
    'from' => from,
    'recipients' => recipients,
    'subject' => subject,
    'ATT_NAME' => att_name,
    'attachmentid' => attachmentid,
    'bcc' => bcc,
    'clicktrack' => if clicktrack.nil? then true else clicktrack end,
    'footer' => if footer.nil? then true else footer end,
    'fromname' => fromname,
    'opentrack' => if opentrack.nil? then true else opentrack end,
    'replytoid' => replytoid,
    'tags' => tags,
    'template' => template,
    'X-APIHEADER' => x_apiheader
  }

  # validate and preprocess url
  query_url = APIHelper.clean_url query_builder

  # prepare headers
  headers = {
    'user-agent' => 'APIMATIC 2.0',
    'accept' => 'application/json'
  }

  # invoke the API call request to fetch the response
  response = Unirest.get query_url, headers: headers

  #Error handling using HTTP status codes
  if !response.code.between?(200, 206) # [200,206] = HTTP OK
    raise APIException.new 'HTTP Response Not OK', response.code, response.raw_body
  end

  response.body
end

#send(data) ⇒ Object

This is a common function send email using Pepipost API



75
76
77
78
# File 'lib/pepipost/controllers/email_controller.rb', line 75

def send(data)
    response = self.create_api_web_send_json(data)
    return response
end