2

So I am having an issue migrating away from Mailgun and start using Mandrill. I followed the Parse Purchase application tutorial and have a very similar code base. Here is what it currently is and successfully runs.

return Mailgun.sendEmail({
    to: currentUser.get('email'),
    from: hostEmail,
    subject: 'Your ticket(s) purchase for ' + eventObject.get('title') + ' was successful!',
    text: body
}).then(null, function(error) {
    return Parse.Promise.error('Your purchase was successful, but we were not able to send you an email.');
});

So this runs successfully, no errors are thrown.

So heres the Mandrill equivalent,

return Mandrill.sendEmail({
    message: {
        text: body,
        subject: 'Your ticket(s) purchase for ' + eventObject.get('title') + ' was successful!',
        from_email: hostEmail,
        from_name: appname,
        to: [{
            email: currentUser.get('email'),
            name: currentUser.get('displayName')
        }]
    },
    async: true
}).then(null, function(error) {
    console.log('Sending email failed. Error: ' + error);
    return Parse.Promise.error('Your purchase was successful, but we were not able to send you an email.');
});

Apparently, this is not working.

The error log shows:

Error: TypeError: Cannot read property 'success' of undefined
at Object.exports.sendEmail (mandrill.js:55:21)
at main.js:115:25
at e (Parse.js:2:6670)
at Parse.js:2:6119
at Array.forEach (native)
at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:661)
at c.extend.resolve (Parse.js:2:6070)
at Parse.js:2:6749
at e (Parse.js:2:6670)
at Parse.js:2:6119 (Code: 141, Version: 1.6.0)

So I think Mandrill successfully sends the email because its searching for the 'success' property, but the Promise is always failing and returns an error response back to the iOS app.

Any help will be appreciated!

Thanks again

jsetting32
  • 1,632
  • 2
  • 20
  • 45
  • Are you sure Mandrill supports promises at all? It seems to look for a `success` callback. – Bergi Mar 17 '15 at 16:31
  • Do you really want `async: false`? – Bergi Mar 17 '15 at 16:31
  • @Bergi No. I do not. That was my bad haha. That would basically halt the entire server until the process is finished. That would be bad! Is that right? Why would they give us that option anyways? – jsetting32 Mar 17 '15 at 16:36
  • 1
    @Bergi, I was able to find a link that integrates Promises with Mandrill sendMail here, https://gist.github.com/ardrian/dcb47afeb59cad0234eb . Ill post an edit – jsetting32 Mar 17 '15 at 16:39
  • Ah well, I think that's an answer not an edit… – Bergi Mar 17 '15 at 16:44
  • Perhaps it's just a copy/paste issue, but your code has a syntax error (as shown by the text color on the last line). – JLRishe Mar 17 '15 at 16:50
  • @JLRishie That was just an edit I made within the post to hide the actual message that shows the support email – jsetting32 Mar 17 '15 at 16:54
  • And @Bergi, you were right. It was an answer :) – jsetting32 Mar 17 '15 at 17:02

1 Answers1

1

Found the problem. Apparently you have to actually declare a promise variable and send it the success/failure callbacks.

The gist link I posted in the comments is what helped megist Link

Heres what I do now,

Parse.Cloud.define('purchase', function(request, response) { 
    ...

    ...

    ...

    var message = {
            text: body,
            subject: 'Your ticket(s) purchase for ' + eventObject.get('title') + ' was successful!',
            from_email: hostEmail,
            from_name: appname,
            to: [{
                email: currentUser.get('email'),
                name: currentUser.get('displayName'),
            }]
        };

    return sendMandrillEmailPromise(message).then(null, function(error) {
        console.log('Sending email failed. Error: ' + error);
        return Parse.Promise.error('Your purchase was successful, but we not able to send you an email');
    ...

    ...

    ...  
});

var sendMandrillEmailPromise = function(message){
    var promise = new Parse.Promise();
    Mandrill.sendEmail({
        message: message,
        async: true,
    },{
        success: function(httpResponse) {
            promise.resolve(httpResponse);
        },
        error: function(error) {
            promise.error(error);
        }
    });
    return promise;
}

Works as expected and I got an AWESOME email! Thanks guys for your input!

jsetting32
  • 1,632
  • 2
  • 20
  • 45