Android sending Ecommerce emails using Parse API with Parse Cloud Code simple example Tutorial

By | September 30, 2015

In this article, we will see a simple example of how to send automated email when a new entry is added to database, using Parse API.

Use Case:
– This can be used in an eCommerce app for sending order confirmation email when a new order is placed.
– For email verification of newly registered user.

We will be using PHP script for sending emails, and will call this script from parse cloud code when a new entry is added to Parse database.
So the code for sending emails will be written in send_mail.php file as follows:

        // sending emails 
         $to ="". htmlspecialchars($_GET["orderEmail"]); // email address of customer
         $subject = "Order Confirmation: Your order with is successfully placed!"; 
	 $message = "Thank you for your order!"

         $headers = "MIME-Version: 1.0" . "\r\n";
         $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
         $headers .= 'From: '.$from . "\r\n" .
            'Reply-To: '.$reply_to . "\r\n" . 'X-Mailer: PHP/' . phpversion();

         $retval = mail ($to,$subject,$message,$headers);
         if( $retval == true )
            echo "....Message sent  successfully...";
            echo "Message could not be sent...";

The above php file will send emails on successful execution. This php file is just a sample, you can send your own email content from this type of script.

Working with parse cloud code

Inside the main.js of your parse cloud code, write the following code to call the php script onSave event of your particular parse data Class:

Parse.Cloud.afterSave("YourParseClass", function(request) {


unction sendEmail(order){
var parameters=  {
            orderEmail : order.get("OrderEmail"),
            orderContact : order.get("OrderContact"),
            orderName : order.get("OrderName")

        url: '',
    }).then(function(httpResponse) {
      // success
      console.log("Email Response:"+httpResponse.text);
    },function(httpResponse) {
      // error
      console.error('Email Request failed with response code ' + httpResponse.status);

That’s all, now whenever a new entry is added to your “YourParseClass”, an email will be sent by the php script.

Alternative to PHP Method
You can also use Mandrill Module for parse to send transactional emails.

The implementation for the using the Mandrill Module is as follows:

Here is the android version:

public void sendMail(View view) {
    Map<String, String> params = new HashMap<>();
    params.put("text", "Sample mail body");
    params.put("subject", "Test Parse Push");
    params.put("fromEmail", "");
    params.put("fromName", "Source User");
    params.put("toEmail", "");
    params.put("toName", "Target user");
    ParseCloud.callFunctionInBackground("sendMail", params, new FunctionCallback<Object>() {
        public void done(Object response, ParseException exc) {
            Log.e("cloud code example", "response: " + response);

Server side JS Code(main.js) Parse Cloud

Parse.Cloud.define("sendMail", function(request, response) {
var Mandrill = require('mandrill');

message: {
text: request.params.text,
subject: request.params.subject,
from_email: request.params.fromEmail,
from_name: request.params.fromName,
to: [
email: request.params.toEmail,
name: request.params.toName
async: true
success: function(httpResponse) {
response.success("Email sent!");
error: function(httpResponse) {
response.error("Uh oh, something went wrong");

2,060 total views, 5 views today

(Visited 967 times, 2 visits today)
  • Clark Thomas

    Great article! I’m a developer and used to When I knew it would shut down, I began to look for alternatives and found I’m happy with my choice, there are several tools that serves me, and best of all, I managed to migrate my project in just 5 minutes!

    • Laxman Marothiya

      Hey, why didn’t you use your own hosted parse? It would be much cheaper! Parse is open source now. You can create your own!

      • George Batschinski

        Laxman, It will cost you more to host Parse Open Source on Amazon AWS or create your own server than to install your application on We have free plans and our paid plans start at $ 14,99