Send Email via Cloud Code in Parse

Posted in :

stlplace
Reading Time: 2 minutes

(Update 05-10-2016) Came across this post about hosting your own Parse server from raywenderlich.com

(Update 01-28-2016) All good things came to end (see today’s Facebook announcement on shutting down Parse on January 28, 2017, a year from today). They also listed migration path. A lot discussion on reddit on this topic. Also a tutorial on Firebase by raywenderlich, note Firebase is owned by Google (they are also famous for shutting down things 🙂

(Update 10-12-2015) Here is the “get started on Cloud Code“. This talks about how to set up the Parse cloud code (including email), the integration with Mandrill is also there. You will need two accounts (both are free for small apps): Mandrill and Parse. Just replace the keys in my code below and in the parse command with your own keys. I also create a github repo here. It has a README just like this blog post and has all the cloud code.

(Original 11-24-2013)
The cloud code (javascript)
refer to this stackoverflow thread; and this Parse documentation. Note I used ManDrill (free for up to 12,000 mails per month), the main things I noticed is they requires 10 letters password, and the API key can be created in Settings (this will be used later, we will use API key instead of SMTP server)

cat ./cloud/main.js

// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("hello", function(request, response) {
response.success("Hello world!");
});

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");
}
});
});

To test it out, do the “parse deploy” after put the code above in main.js (under cloud dir), then issue this command:
curl -X POST -H "X-Parse-Application-Id: your-parse-application-id" -H "X-Parse-REST-API-Key: your-parse-REST-API-key" -H "Content-Type: application/json" -d '{"toEmail":"major_xu@yahoo.com","toName":"Major Xu","fromEmail":"minjie.xu@gmail.com","fromName":"Minjie Xu","text":"testing ManDrill email","subject":"this is just a test"}' https://api.parse.com/1/functions/sendMail

Objective-C code
Refer to Parse documentation here. Note this is a simple “Hello World” example.
[PFCloud callFunctionInBackground:@"hello"
withParameters:@{}
block:^(NSString *result, NSError *error) {
if (!error) {
// result is @"Hello world!"
}
}];

My email code is a bit more complex compared to “HelloWorld” 🙂


[PFCloud callFunctionInBackground:@"sendMail"
withParameters:@{@"toEmail":toEmail,@"toName":@"Minnjie Xu",@"fromEmail":@"uudaddy@gmail.com",@"fromName":@"uudaddy",@"text":@"resetPasswordButtonPressed...",@"subject":@"myNestEgg ~ testing ManDrill email"}
block:^(NSString *result, NSError *error) {
if (!error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reset Passoword" message:@"Email Sent :-)"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];

}
}];

%d bloggers like this: