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

17,309

Solution 1

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

Solution 2

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/

Solution 3

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

                            }];

Solution 4

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

Solution 5

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

Share:
17,309

Related videos on Youtube

Sharon
Author by

Sharon

Web developer, mostly working in CakePHP, with a bit of WordPress. Starting to get into Android app development. Sometimes confused.

Updated on June 04, 2022

Comments

  • Sharon
    Sharon almost 2 years

    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?

  • user94154
    user94154 over 11 years
    stop voting for my answer. the one below should be the accepted answer.
  • ninehundreds
    ninehundreds about 10 years
    Thanks for the write up. Saved me some time in my Android implementation. Cheers!
  • M.Y.
    M.Y. over 9 years
    which one is the best? :)
  • Pravin Raj
    Pravin Raj almost 9 years
    I am getting the response as null
  • silentsudo
    silentsudo almost 9 years
    You should follow @uudaddy's answer to setup the server rest all is downslide from there.
  • Shreyash Mahajan
    Shreyash Mahajan almost 9 years
    @sector11 i want to send email from parse it self. Should i need to do anycode in my backend server or without that it is possible? I just want to code in Android platform to send email nothing else because i do not have other server to handle it.
  • silentsudo
    silentsudo almost 9 years
    Yes read this post clearly with comments and u'll be through... :)
  • Vishal Thakkar
    Vishal Thakkar about 8 years
    @sector11 can i send file as attachments?
  • Akshit Zaveri
    Akshit Zaveri almost 8 years
    How the hell do you add TXT and CNAME records? and where?
  • lxknvlk
    lxknvlk over 7 years
    But parse has its own library for sending email. The way they send emails when verify email is turned "on". Isnt there any way to access it?
  • Israel Meshileya
    Israel Meshileya over 5 years
    @hris.to Thanks for your response, please how can i use parse cloud function to send just a mail using SendGrid...although, I have actually been able to use it to do both email verification and password reset, but I will like to send just emails using the SendGrid as well, any pointer will really be appreciated. Thanks.