Cloud Function Mailgun

345 views
Skip to first unread message

adr...@gmail.com

unread,
Aug 8, 2016, 1:01:12 PM8/8/16
to back{4}app
Hi there - I am trying to send an email in cloud code using mailgun, but am struggling to get it working!

I have added the function to my index.js

var client = require('cloud/myMailModule-1.0.0');
client.initialize('xxxxx', 'xxx');

Parse.Cloud.define("sendEmailToUser", function(request, response) {
  client.sendEmail({
    from: "MyM...@CloudCode.com",
    subject: "Hello from Cloud Code!",
    text: "Using Parse and My Mail Module is great!"
  }).then(function(httpResponse) {
    response.success("Email sent!");
  }, function(httpResponse) {
    console.error(httpResponse);
    response.error("Uh oh, something went wrong");
  });
});

And added the module in myMailModule-1.0.0

(function() {

  var url = 'api.mailgun.net/v2';
  var domain = 'xxxxxxx';
  var key = 'xxxxxxxxx';

  module.exports = {

    initialize: function(domainName, apiKey) {
      domain = domainName;
      key = apiKey;
      return this;
    },

    sendEmail: function(params, options) {
      return Parse.Cloud.httpRequest({
        method: "POST",
        url: "https://api:" + key + "@" + url + "/" + domain + "/messages",
        body: params,
      }).then(function(httpResponse) {
        if (options && options.success) {
          options.success(httpResponse);
        }
      }, function(httpResponse) {
        if (options && options.error) {
          options.error(httpResponse);
        }
      });
    }

  }
}());

HOWEVER, I am getting the following error message: Error: Cannot find module 'cloud/myMailModule-1.0.0'] code: 'MODULE_NOT_FOUND' }


Any suggestions would be much appreciated!! Thanks

Ricardo Seiji

unread,
Aug 8, 2016, 1:56:02 PM8/8/16
to back{4}app
Hey, Archie!

How's it going?

So, on parse-server (and back4app), we need to change some stuff on cloud-code:

Let's change "var client = require('cloud/myMailModule-1.0.0');"
to "var client = require('./myMailModule-1.0.0');"

Tell me if it's working properly.

Regards,
Ricardo Seiji.

May your coffee be strong and your Monday be short. – Unknown

adr...@gmail.com

unread,
Aug 9, 2016, 6:36:59 AM8/9/16
to back{4}app
Hi Ricardo - 

This works great! Thank you very much for your help. 

Archie
Reply all
Reply to author
Forward
0 new messages