Sharon
Sharon

Reputation: 3909

How can my Parse.com app send an email using JavaScript?

I'm using Parse.com (JavaScript SDK), and I want users to be able to send an email from my app. Basically, they create a page using the app, and then I need to allow them to enter a list of email addresses; the app will then send each address a link to the page they've created.

I can find anything in the documentation which tells me how to send the email, though. I can take the list of email addresses and generate the email, I just can't figure out how to send it.

Is this possible with Parse?

Upvotes: 17

Views: 17780

Answers (5)

nicko
nicko

Reputation: 309

Parse Cloud Code Modules now support sending email through a number of Cloud Mail Providers:

Upvotes: 30

hris.to
hris.to

Reputation: 6363

Someone might find useful example using Mailgun, iOS and Parse Cloud.

I decided to went with Mailgun as Mandril currently had only 4k free mails.

Please note that you had to have access to your domain in order to setup 'TXT' and 'CNAME' records prove Mailgun you are the owner of the domain.

Cloud code:

// 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("mailSend", function(request, response) {

    var Mailgun = require('mailgun');
    Mailgun.initialize('DOMAIN_NAME', 'API_KEY');

    Mailgun.sendEmail({
      to: request.params.target,
      from: request.params.originator,
      subject: request.params.subject,
      text: request.params.text
    }, {
      success: function(httpResponse) {
        console.log(httpResponse);
        response.success("Email sent!");
      },
      error: function(httpResponse) {
        console.error(httpResponse);
        response.error("Uh oh, something went wrong");
      }
    });

});

And now somewhere in your ObjC project:

[PFCloud callFunctionInBackground:@"mailSend"
                   withParameters:@{
                                    @"target": @"[email protected]",
                                    @"originator": @"[email protected]",
                                    @"subject": @"Hey There",
                                    @"text": @"This is your iOS originated mail"
                                    }
                            block:^(NSString *result, NSError *error){

                                NSLog(@"error %@", error);
                                NSLog(@"result %@", result);

                            }];

Upvotes: 6

silentsudo
silentsudo

Reputation: 6963

Here is the android version for @uudaddy's answer

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", "[email protected]");
    params.put("fromName", "Source User");
    params.put("toEmail", "[email protected]");
    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");
}
});
});

Upvotes: 5

uudaddy
uudaddy

Reputation: 381

I created a simple iOS example here, using Mandrill, and Parse Cloud Code http://www.stlplace.com/2013/11/24/send-email-via-cloud-code-in-parse/

Upvotes: 8

user94154
user94154

Reputation: 16564

There is no native method to do this. your best bet is to wait until Parse's Cloud Code supports 3rd-party HTTP requests. I made a quick mockup of how you could accomplish this using IronWorker + Ruby to send the email, but you could certainly use other languages:

http://news.ycombinator.com/item?id=4506888

Upvotes: 4

Related Questions