Class: App42::Email::EmailService

Inherits:
Object
  • Object
show all
Defined in:
lib/email/EmailService.rb

Overview

This Service is used to send Emails. This service can be used by app to send mail to one or multiple recipients.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, base_url) ⇒ EmailService

this is a constructor that takes

Parameters:

  • apiKey
  • secretKey
  • baseURL


28
29
30
31
32
33
34
35
# File 'lib/email/EmailService.rb', line 28

def initialize(api_key, secret_key, base_url)
  puts "EmailService->initialize"
  @api_key = api_key
  @secret_key = secret_key
  @base_url = base_url
  @resource = "email"
  @version = "1.0"
end

Instance Method Details

#create_mail_configuration(emailHost, emailPort, mailId, emailPassword, isSSL) ⇒ Object

Creates Email Configuration using which in future the App developer can send mail

Parameters:

  • emailHost
    • Email Host to be used for sending mail

  • emailPort
    • Email Port to be used for sending mail

  • mailId
    • Email id to be used for sending mail

  • emailPassword
    • Email Password to be used for sending mail

  • isSSL
    • Should be send using SSL or not

Returns:

  • Email object containing the email configuration which has been created

Raises:

  • App42Exception



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
92
93
94
95
96
97
# File 'lib/email/EmailService.rb', line 56

def create_mail_configuration(emailHost, emailPort, mailId, emailPassword, isSSL)
  puts "create Mail Configuration Called "
  puts "Base url #{@base_url}"
  response = nil;
  emailObj = nil;
  emailObj = Email.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(emailHost, "Host");
  util.throwExceptionIfNullOrBlank(emailPort, "Port");
  util.throwExceptionIfNullOrBlank(mailId, "Email Id");
  util.throwExceptionIfNullOrBlank(emailPassword, "Password");
  util.throwExceptionIfNullOrBlank(isSSL, "isSSL");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"email"=> {
      "host" => emailHost,
      "port" => emailPort,
      "emailId" => mailId,
      "password" => emailPassword,
      "ssl" => isSSL
      }}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("body", body)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/configuration"
    response = connection.post(signature, resource_url, query_params, body)
    email = EmailResponseBuilder.new
    emailObj = email.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return emailObj
end

#get_email_configurationsObject

Gets all Email Configurations for the app

Returns:

  • Email object containing all Email Configurations

Raises:

  • App42Exception



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/email/EmailService.rb', line 149

def get_email_configurations()
  puts "getEmailConfigurations "
  puts "Base url #{@base_url}"
  response = nil;
  emailObj = nil;
  emailObj = Email.new
  util = Util.new
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/configuration"
    response = connection.get(signature, resource_url, query_params)
    emailObj = EmailResponseBuilder.new().buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return emailObj
end

#remove_email_configuration(emailId) ⇒ Object

Removes email configuration for the given email id. Note: In future the developer wont be able to send mails through this id

Parameters:

  • emailId
    • The email id for which the configuration has to be removed

Returns:

  • Email object containing the email id which has been removed

Raises:

  • App42Exception



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/email/EmailService.rb', line 111

def remove_email_configuration(emailId)
  puts "Delete Email config Called "
  puts "Base url #{@base_url}"
  response = nil;
  responseObj = App42Response.new();
  util = Util.new
  util.throwExceptionIfNullOrBlank(emailId, "Email Id");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("emailId", emailId)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/configuration/#{emailId}"
    response = connection.delete(signature, resource_url, query_params)
    responseObj.strResponse=(response)
    responseObj.isResponseSuccess=(true)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#send_mail(sendTo, sendSubject, sendMsg, fromEmail, emailMime) ⇒ Object

Sends the Email to the specified recipient with the provided detail

Multiple email ids can be passed using comma as the seperator e.g. [email protected], [email protected]

Parameters:

  • fromEmail
    • The Email Id using which the mail(s) has to be sent

  • sendTo
    • The email ids to which the email has to be sent. Email can be sent to multiple email ids.

  • sendSubject
    • Subject of the Email which to be sent

  • sendMsg
    • Email body which has to be sent

  • emailMIME
    • MIME Type to be used for sending mail. EmailMIME available options are PLAIN_TEXT_MIME_TYPE or HTML_TEXT_MIME_TYPE

Returns:

  • Email object containing all the details used for sending mail

Raises:

  • App42Exception



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/email/EmailService.rb', line 197

def send_mail(sendTo, sendSubject, sendMsg, fromEmail, emailMime)
  puts "sendMail Called "
  puts "Base url #{@base_url}"
  response = nil;
  emailObj = nil;
  emailObj = Email.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(sendTo, "Send To");
  util.throwExceptionIfNullOrBlank(sendSubject, "Send Subject");
  util.throwExceptionIfNullOrBlank(sendMsg, "Send Message");
  util.throwExceptionIfNullOrBlank(fromEmail, "From Email");
  util.throwExceptionIfNullOrBlank(emailMime, "emailMime");
  begin
    if (EmailMIME.new.isAvailable(emailMime) == nil)
      raise App42NotFoundException.new("Email MIME #{emailMime} does not Exist ");
    end
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"email"=> {
      "to" => sendTo,
      "subject" => sendSubject,
      "msg" => sendMsg,
      "emailId" => fromEmail,
      "mimeType" => emailMime
      }}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("body", body)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}"
    response = connection.post(signature, resource_url, query_params, body)
    emailObj = EmailResponseBuilder.new().buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return emailObj
end