Skip to content

Commit 17520dd

Browse files
author
Christine Abernathy
committed
Initial commit
0 parents  commit 17520dd

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Overview
2+
--------
3+
4+
This sample Cloud Module used in the [Integrating with Third Party Service Tutorial](https://www.parse.com/tutorials/integrating-with-third-party-services) to show developers how to create a Cloud Module. The module showcases sending email using the Mailgun service. To use the official Mailgun module, head over to the [Mailgun Cloud Module guide](https://www.parse.com/docs/cloud_modules_guide#mailgun).
5+
6+
Mailgun is a set of powerful APIs that allow you to send, receive, track and store email effortlessly. You can check out their service at [www.mailgun.com](http://www.mailgun.com/). To use this module, you will need to head over to the [Mailgun website](http://www.mailgun.com/) and create an account.
7+
8+
Installation
9+
------------
10+
11+
1. Clone this respository to get the Cloud Module.
12+
```
13+
git clone https://github.com/ParsePlatform/MyMailCloudModule.git
14+
```
15+
16+
2. Copy `myMailModule-1.0.0.js` over to your Cloud Code Directory, placing it in the `cloud` directory.
17+
18+
Usage
19+
-----
20+
21+
To use the module in your Cloud Code functions, start by requiring the module and initializing it with your credentials:
22+
23+
```javascript
24+
var client = require('cloud/myMailModule-1.0.0.js');
25+
client.initialize('myDomainName', 'myAPIKey');
26+
```
27+
28+
Then inside of your Cloud Code function, you can use the `sendEmail` function to fire off some emails:
29+
30+
```javascript
31+
Parse.Cloud.define("sendEmailToUser", function(request, response) {
32+
client.sendEmail({
33+
to: "email@example.com",
34+
from: "MyMail@CloudCode.com",
35+
subject: "Hello from Cloud Code!",
36+
text: "Using Parse and My Mail Module is great!"
37+
}).then(function(httpResponse) {
38+
response.success("Email sent!");
39+
}, function(httpResponse) {
40+
console.error(httpResponse);
41+
response.error("Uh oh, something went wrong");
42+
});
43+
});
44+
```
45+
46+
This function takes two parameters. The first is a hash with the mail parameters you want to include in the request. The typical ones are from, to, subject and text, but you can find the full list on their documentation page. The second parameter to this function is an object with a success and an error field containing two callback functions.
47+
48+
Once you've deployed your Cloud Code function and this Cloud Module to Parse, you can test it out. The example below shows how to do this using cUrl:
49+
50+
```
51+
curl -X POST \
52+
-H "X-Parse-Application-Id: YOUR_APPLICATION_ID" \
53+
-H "X-Parse-REST-API-Key: YOUR_REST_API_KEY" \
54+
-H "Content-Type: application/json" \
55+
-d '{ }' \
56+
https://api.parse.com/1/functions/sendEmailToUser
57+
```
58+
59+
For additional information about this Cloud Module, take a look at the [API Reference](https://www.parse.com/docs/js/symbols/Mailgun.html).

myMailModule-1.0.0.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Sample Mailgun Cloud Module
3+
* @name Mailgun
4+
* @namespace
5+
*
6+
* Sample Cloud Module for using <a href="http://www.mailgun.com">Mailgun</a>.
7+
*
8+
* <ul><li>Module Version: 1.0.0</li>
9+
* <li>Mailgun API Version: 'v2'</li></ul>
10+
*
11+
* Copyright 2013 Parse, Inc.
12+
* This module is freely distributable under the MIT license.
13+
*/
14+
15+
(function() {
16+
17+
var url = 'api.mailgun.net/v2';
18+
var domain = '';
19+
var key = '';
20+
21+
module.exports = {
22+
/**
23+
* Get the version of the module.
24+
* @return {String}
25+
*/
26+
version: '1.0.0',
27+
28+
/**
29+
* Initialize the Mailgun module with the proper credentials.
30+
* @param {String} domainName Your Mailgun domain name
31+
* @param {String} apiKey Your Mailgun api key
32+
*/
33+
initialize: function(domainName, apiKey) {
34+
domain = domainName;
35+
key = apiKey;
36+
return this;
37+
},
38+
39+
/**
40+
* Send an email using Mailgun.
41+
* @param {Object} params A hash of the paramaters to be passed to
42+
* the Mailgun API. They are passed as-is, so you should
43+
* consult Mailgun's documentation to ensure they are valid.
44+
* @param {Object} options A hash with the success and error callback
45+
* functions under the keys 'success' and 'error' respectively.
46+
* @return {Parse.Promise}
47+
*/
48+
sendEmail: function(params, options) {
49+
return Parse.Cloud.httpRequest({
50+
method: "POST",
51+
url: "https://api:" + key + "@" + url + "/" + domain + "/messages",
52+
body: params,
53+
}).then(function(httpResponse) {
54+
if (options && options.success) {
55+
options.success(httpResponse);
56+
}
57+
}, function(httpResponse) {
58+
if (options && options.error) {
59+
options.error(httpResponse);
60+
}
61+
});
62+
}
63+
64+
}
65+
}());

0 commit comments

Comments
 (0)