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.
Implementation:
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:
<?php // sending emails $to ="". htmlspecialchars($_GET["orderEmail"]); // email address of customer $subject = "Order Confirmation: Your order with Company.com is successfully placed!"; $message = "Thank you for your order!" $from="noreply@company.com"; $reply_to="reply@company.com"; $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..."; } else { 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) { sendEmail(request.object); }); unction sendEmail(order){ var parameters= { orderEmail : order.get("OrderEmail"), orderContact : order.get("OrderContact"), orderName : order.get("OrderName") }; Parse.Cloud.httpRequest({ url: 'http://www.company.com/php/send_mail.php', params:parameters }).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", "someone@example.com"); params.put("fromName", "Source User"); params.put("toEmail", "other@example.com"); params.put("toName", "Target user"); ParseCloud.callFunctionInBackground("sendMail", params, new FunctionCallback<Object>() { @Override 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'); Mandrill.initialize('12AkxxxxxxxxxxxxxxrZEg'); Mandrill.sendEmail({ 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) { console.log(httpResponse); response.success("Email sent!"); }, error: function(httpResponse) { console.error(httpResponse); response.error("Uh oh, something went wrong"); } }); });
5,856 total views, 1 views today