Chad
Chad

Reputation: 183

Can't send an email with Parse Cloud Code and Mandrill

New to coding and never used Cloud Code before.

I need to send a confirmation email when someone submits their email in a form on my webpage with Parse Cloud Code but I can't get it to work. I'm using the Mandrill Cloud Module to send the emails.

My questions are -

a) Am I calling the Cloud Function correctly? b) The only variable that changes is the persons email address. Am I passing that variable correctly?

Example code would really help.

Thanks

Here's my Cloud Code:

Parse.Cloud.define("introEmail", function(request, response) {
var Mandrill = require('mandrill');
Mandrill.initialize('*************');

mandrill.sendEmail({
message: {
  text: "Hello!",
  subject: "Thanks for Signing Up!",
  from_email: "[email protected]",
  from_name: "Chad",
  to: [
    {
      email: request.params.Address,
      name: ""
    }
  ]
},
async: true
}, {
success: function(httpResponse) { response.success("Email sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
});
});

Here's my JS code:

$(".input-group-btn").click(function() {
    console.log("Notify Me");

    var Address = $(".form-control").val();

    var Email = Parse.Object.extend("Email");
    var email = new Email();

    email.set("Address", Address);

    console.log(Address);

    email.save(null, {
        success: function(email) {
            console.log('New object created with objectId: ' + email.id);
            Parse.Cloud.run(introEmail,Address)

        },
        error: function(email, error) {             
            alert('Could not accept email address: ' + error.message);
        }
    });

});

Upvotes: 1

Views: 930

Answers (1)

Chad
Chad

Reputation: 183

a) Call the Cloud function with Parse.Cloud.run(), however, you must pass name, data, options. These are 'introEmail' (the name of the cloud function), the variable which is {Address: $(".form-control").val()} and the success/error handlers.

b)See a

Parse.Cloud.define("introEmail", function(request, response) {
var mandrill = require("mandrill");
mandrill.initialize('*************');

  mandrill.sendEmail({
    message: {
      text: "Hello!",
      subject: "Thanks for Signing Up!",
      from_email: "[email protected]",
      from_name: "Test",
      to: [
        {
          email: request.params.Address,
          name: ""
        }
      ]
    },
    async: true
  }, {
    success: function(httpResponse) { response.success("Email sent!"); },
    error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
  });
});

Here's the JS for the client-side:

$(".input-group-btn").click(function() {
    console.log("Notify Me");

    var Address = $(".form-control").val();

    var Email = Parse.Object.extend("Email");
    var email = new Email();

    email.set("Address", Address);

    console.log(Address);

    email.save(null, {
        success: function(email) {
        // Execute any logic that should take place after the object is saved.
        console.log('New object created with objectId: ' + email.id);
        // Invoke our cloud function, using the phone number in the text field
        Parse.Cloud.run('introEmail', {
            Address: $(".form-control").val()
            }, {
                // Success handler
                success: function(message) {
                    alert('Success: ' + message);
                },
                // Error handler
                error: function(message) {
                    alert('Error: ' + message);
                }
            });
        }
    });
});

Upvotes: 1

Related Questions