Child pages
  • Send emails
Skip to end of metadata
Go to start of metadata

Sending emails

Sending emails with Apstrata is as simple as a function call. All you need to do is invoke the SendEmail API, from your client-side or the back-end of your application.

Back to the map      

Example 1: Allow your players to contact your support team by email from the app

In the mobile game app that your are developing, you decide to add a feature to allow your end users to send email to your support team. Let us see how to do this:

 

 curl -X POST  "https://varick.apstrata.com/apsdb/rest/O763A7F690/SendEmail?apsws.time=1418992125166&apsws.user=user1@mail.com"
-F apsws.authSig=1933c4b633b2ad05c40c2bd7c7ad0aa8 -F apsws.authMode=simple -F apsws.responseType=json 
-F apsma.to=support@angrypoultry.com 
-F apsma.subject=How to unlock a level? 
-F apsma.htmlBody=Hello, <br> I have finished level 2 of the game but cannot find a way to unlock level 3. Please help.<br>
 Thanks.
 String response = "no response yet";
try {
	
	String authKey = "O71307F690";
	String baseURL = "https://varick.apstrata.com/apsdb/rest";
	 
	connection = new UserConnection(baseURL, authKey, "user1@mail.com", "somePassword"); // We create a user connection using the end user's password
	Client client = new Client(BASE_URL, ACCOUNT_KEY, connection);
	List<NameValuePair> parameters = new ArrayList<NameValuePair>();
	 
	// Create an instance of the Apstrata Android Client with the above Connection instance
	Client client = new Client(baseURL, authKey, ownerConnection);
	 
	// Determine the signature mode to use (Complex or Simple)
	Client.AuthMode mode = Client.AuthMode.SIMPLE;
	 
	// Prepare the parameters to send to the SendEmail API
	// Notice that when invoking the API from the client side, you do not have to pass 
	// the apsma.from parameter, as it is deduced from the username of the caller
	List<NameValuePair> parameters = new ArrayList<NameValuePair>();	
	parameters.add(new BasicNameValuePair("apsma.to","support@angrypoultry.com"));
	parameters.add(new BasicNameValuePair("apsma.subject","How to unlock a level?"));
	parameters.add(new BasicNameValuePair("apsma.htmlBody","Hello, <br> I have finished level 2 of the game but cannot find a way to unlock level 3. Please help.<br>  Thanks."));
											
	// Invoke the API using the Client instance and signature mode 
	// note that since we're not passing files, we send "null" in place of the "files" parameter
	response = client.callAPIJson(SendEmail, parameters, files, mode);
	return response;					
} catch (Exception e) {
	response = "Error: " + e.getMessage();
	e.printStackTrace();
}
Try it!

Example 2: send a promotion code by email to all players who have been registered for more than 6 months

In order to generate more traction to your cool mobile app game, let us imagine that you decide to regularly send promotions through email to some of your players, in the current case, those who have been registered to your game for more that 6 months. Let us show you how easy it is to achieve this by using an Apstrata server-side script:

 

<script>
	<scriptACL>
		<execute>anonymous</execute>
		<read>nobody</read>
		<write>nobody</write>
	</scriptACL>
	<code>
		<![CDATA[
			
			// Prepare the parameters to pass to the ListUsers API in order to search for users
			// who registered more than n months ago
			var queryUsersParams = {
			    // Search for users who have a creation time <= the given date and that
			    "apsdb.query": "apsdb.creationTime<date> <= \"2014-07-01\" and pushToken is not null", 
			    // only return the login and email attributes of the user's profile. We assume that we added the "pushToken" field to the users profiles
			    "apsdb.attributes": "login, email" 
			}
			
			// Execute the request by invoking the ListUsers API
			var response = apsdb.callApi("ListUsers", queryUsersParams, null);
			if (response.metadata.status == "failure") {
			    return response.metadata;
			}
			
			var users = response.result.users;
			if (users.length == 0) {
			    return {
			        "result": "No matching users"
			    }
			}
			
			// Create an array of emails from the data returned by the query
			var emails = [];
			for (var i = 0; i < users.length; i++) {
			    emails.push(users[i].email[0]);
			}
			
			// Prepare the parameters to pass to the push SendEmail API
    	    var email = {
            		
        		"apsma.bcc": emails,
        		"apsma.subject": "Promotion code",
        		"apsma.body": "Get 50% using this promotion code for your next purchase: AZX34W"
        	}
        	
        	// Invoke the SendEmail API
        	var sendEmailResponse = apsdb.callApi("SendEmail", email, null);
            if (sendEmailResponse.metadata.status == "failure") {
                return sendEmailResponse.metadata;
            }
            
            return sendEmailResponse.result;
		]]>
	</code>
</script>
Try it!

Dig Deeper

Related tutorials