# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'App42Response.rb' require 'email/EmailResponseBuilder.rb' require 'email/Email.rb' module App42 module Email # # This Service is used to send Emails. This service can be used by app to send mail to one or multiple recipients. # # @see Email # class EmailService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # 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 # # Creates Email Configuration using which in future the App developer can send mail # # @param emailHost # - Email Host to be used for sending mail # @param emailPort # - Email Port to be used for sending mail # @param mailId # - Email id to be used for sending mail # @param emailPassword # - Email Password to be used for sending mail # @param isSSL # - Should be send using SSL or not # # @return Email object containing the email configuration which has been created # # @raise App42Exception # 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 # # Removes email configuration for the given email id. Note: In future the developer # wont be able to send mails through this id # # @param emailId # - The email id for which the configuration has to be removed # # @return Email object containing the email id which has been removed # # @raise App42Exception # 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 # # Gets all Email Configurations for the app # # @return Email object containing all Email Configurations # # @raise App42Exception # 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 # # Sends the Email to the specified recipient with the provided detail # # @param fromEmail # - The Email Id using which the mail(s) has to be sent # @param sendTo # - The email ids to which the email has to be sent. Email can be sent to multiple email ids. # Multiple email ids can be passed using comma as the seperator e.g. sid@shephertz.com, info@shephertz.com # @param sendSubject # - Subject of the Email which to be sent # @param sendMsg # - Email body which has to be sent # @param emailMIME # - MIME Type to be used for sending mail. EmailMIME available options are PLAIN_TEXT_MIME_TYPE or HTML_TEXT_MIME_TYPE # # @return Email object containing all the details used for sending mail # # @raise App42Exception # 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 end end end