App42 Cloud API

Ruby Cloud SDK

Email

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

In order to use the various functions available under a particular API, a developer will have to create an instance of ServiceAPI by passing the apiKey and the secretKey which has been received while creating an app.

api = App42::ServiceAPI.new("<API_KEY>","<SECRET_KEY>")    

After initialization, developer needs to call the buildXXXService method on ServiceAPI instance to get the instance of the particular API that you wish to build. For example, To build an instance of UserService, buildUserService() method needs to be called.

	emailService = api.buildEmailService();

Once the API instance has been retrieved, You are ready to use the functions available for that particular API.

The methods available under a particular API will return the domain object (like Resize in this case) as a response which will have the accessor / mutator to access the available properties for that object. You can get the response in the form of JSON as well. We have provided the JSON response with every function detail which can be retrieved by calling the toString() on the returned object.

emailHost = "smtp.gmail.com";
emailPort = 465;
mailId = "nick@gmail.com";
emailPassword = "********";
isSSL = true;
						
email = email.create_mail_configuration(emailHost,emailPort,mailId,emailPassword,isSSL); /* returns the Email object. */
configList = Array.new();
configList = email.configList();  /* returns the list of Email configuration  objects. */
for config in configList do
  puts "emailId is #{config.emailId}";
  puts "host is #{config.host}";
  puts "port is #{config.port}";
end
jsonResponse = email.to_s(); /* returns the response in JSON format. (as shown below)*/ 

The functions available under Email API can throw some exceptions in abnormal conditions. Example of the same has been given below.

E.g. If App developer is removing the email configuration that does not exist, the function will throw the App42Exception (as shown below) with message as "Not Found" and the appErrorCode as "2303" and the details as "Email with the id '<emailId>' does not exist.".

emailId = "nick@gmail.com";
									
begin
  email =  emailService.remove_email_configuration(emailId);
rescue App42Exception => ex
  appErrorCode = ex.app_error_code;
  httpErrorCode = ex.http_error_code;

  if(appErrorCode == 2303)
    #Handle here for Not Found (Email with the id '<emailId>' does not exist.)

  elsif(appErrorCode == 1401)
    #handle here for Client is not authorized

  else(appErrorCode == 1500)
    #handle here for Internal Server Error
  end

  jsonText = ex.getMessage(); /* returns the Exception text in JSON format. (as shown below)*/
end          
	{
	   "app42Fault": {
		   "httpErrorCode": 404,
		   "appErrorCode": 2303,
		   "message": "Not Found",
		   "details": "Email with the id user1@shephertz.com does not Exist"
	   }
	} 

Below are the HTTP Error Codes and their description, the function under the Email API can throw.

/*
1400 - BAD REQUEST - The Request parameters are invalid
1401 - UNAUTHORIZED - Client is not authorized
1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again	
*/	

Below are the Application Error Codes and their description, the function under the Email API can throw.

/*
2300 - NOT FOUND - Email parameters not found.
2301 - BAD REQUEST - The request parameters are invalid. Email id '<emailId>' already exists. 
2302 - NOT FOUND - Email configurations do not exist.
2303 - NOT FOUND - Email with the id '<emailId>' does not exist.
*/ 

Various functions available under Email API has been explained below.

def create_mail_configuration(emailHost,emailPort,emailId,emailPassword,isSSL)

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
emailId - 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

Response: Email Object

Exception:

The method throws the following exception with the error code
/*
2301 - BAD REQUEST - The request parameters are invalid. Email id '<emailId>' already exists. 
*/ 	
For more details click Exception
emailHost = "smtp.gmail.com";
emailPort = 465;
mailId = "nick@gmail.com";
emailPassword = "********";
isSSL = true;
						
email = emailService.create_mail_configuration(emailHost,emailPort,mailId,emailPassword,isSSL);  /* returns the Email object. */
configList = Array.new();
configList = email.configList();  /* returns the list of Email configuration  objects. */
for config in configList do
  puts "emailId is #{config.emailId}";
  puts "host is #{config.host}";
  puts "port is #{config.port}";
end
jsonResponse = email.to_s(); /* returns the response in JSON format. (as shown below)*/    
{
    "app42": {
        "response": {
            "success": "true",
            "email": {
                "configurations": {
                    "config": {
                        "emailId": "nick@gmail.com",
                        "host": "smtp.gmail.com",
                        "port": 465,
                        "ssl": true
                    }
                }
            }
        }
    }
}
def remove_email_configuration(emailId)

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:

App42Response if removed successfully

Response: App42Response Object

Exception:

The method throws the following exception with the error code
/*
2303 - NOT FOUND - Email with the id '<emailId>' does not exist. 
*/   
For more details click Exception
emailId = "nick@gmail.com";
							
response = emailService.remove_email_configuration(emailId); /* returns the App42Response object. */
success = response.isResponseSuccess();
jsonResponse = email.to_s(); /* returns the response in JSON format. (as shown below)*/     
{
    "app42": {
        "response": {
            "success": "true",
            "email": {
                "configurations": {
                    "config": {
                        "emailId": "nick@gmail.com",
                        "host": "smtp.gmail.com",
                        "port": 465,
                        "ssl": true
                    }
                }
            }
        }
    }
}
def get_email_configurations()

Gets all Email Configurations for the app

Parameters:

N/A

Returns:

Email object containing all Email Configurations

Response: Email Object

Exception:

The method throws the following exception with the error code
/*
2302 - NOT FOUND - Email configurations do not exist. 
*/	   
For more details click Exception
email = emailService.get_email_configurations();  /* returns the Email object. */
configList = Array.new();
configList = email.configList();  /* returns the list of Email configuration  objects. */
for config in configList do
  puts "emailId is #{config.emailId}";
  puts "host is #{config.host}";
  puts "port is #{config.port}";
end
jsonResponse = email.to_s(); /* returns the response in JSON format. (as shown below)*/   
{
    "app42": {
        "response": {
            "success": "true",
            "email": {
                "configurations": {
                    "config": [
                        {
                            "emailId": "nick@gmail.com",
                            "host": "smtp.gmail.com",
                            "port": 465,
                            "ssl": true
                        },
                        {
                            "emailId": "bob@gmail.com",
                            "host": "smtp.gmail.com",
                            "port": 465,
                            "ssl": true
                        },
                        {
                            "emailId": "jack@gmail.com",
                            "host": "smtp.gmail.com",
                            "port": 465,
                            "ssl": true
                        }
                    ]
                }
            }
        }
    }
}
def send_mail(sendTo,sendSubject,sendMsg,fromEmail,emailMime)

Sends the Email to the specified recipient with the provided detail

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. Multiple email ids can be passed using comma as the seperator e.g. support@shephertz.com, info@shephertz.com
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

Response: Email Object

Exception:

The method throws the following exception with the error code
/*
2300 - NOT FOUND - Email parameters not found 
*/ 
For more details click Exception
sendTo =  "bob@gmail.com";
sendSubject = "Here we are sending an Email";
sendMsg = "Body of message";
fromEmailId = "jack@gmail.com";
emailMime = App42::Email::EmailMIME.new();			
emailMimeType = emailMime.enum("PLAIN_TEXT_MIME_TYPE");

email = emailService.sendMail(sendTo, sendSubject, sendMsg, fromEmailId, emailMimeType);  /* returns the Email object. */
puts "from  #{email.from}";
puts "to #{email.to}";
puts "subject is #{email.subject}";
puts "body #{email.body}";
jsonResponse = email.to_s(); /* returns the response in JSON format. (as shown below)*/  
{
    "app42": {
        "response": {
            "success": "true",
            "email": {
                "from": "jack@gmail.com",
                "to": "bob@gmail.com",
                "subject": "Here we are sending an Email!! ",
                "body": "Body of message"
            }
        }
    }
}