I'm try to get an email every time I create a contact .When Create a contact I have to include the email address ,then I should receive an email ,but I don't .It was working before for me but now I don't receive any email every time I create a contact This is the code I wrote :
Index.js
'use strict';
var _ = require('lodash'); var Contact = require('./contact.model');
// Get list of contacts exports.index = function(req, res) {
// Connect to the db Contact.find(function (err, contacts) {
if(err) { return handleError(res, err); }
return res.json(200, contacts); });
} ;
// Creates a new contact in datastore. exports.create = function(req, res) { Contact.create(req.body, function(err, contact) {
if(err) { return handleError(res, err); }
return res.json(201, contact);
contactEvent.publish('create_contact_event', contact); }); };
function handleError(res, err) { return res.send(500, err); };
Mail_app.js
var api_key = 'key-83224fdd930ba495d8ee3776e92e8e62';
var domain = 'mg.mydomain95.com';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
var contactEvent = require('./events');
var messageHandler = function(m) {
// The Payload
var data = {
from: 'WIT BSc IT <me@wit.ie>',
to: JSON.parse(m).email,
subject: 'Welcome',
text: 'Welcome to the company!!!'
};
mailgun.messages().send(data, function (error, body) {
console.log(body);
});
}
contactEvent.subscribe('create_contact_event', messageHandler)
Events.js
'use strict';
// Pubnub service configuration
// ===========================
var PubNub = require('pubnub');
var pubnub = new PubNub({
publishKey : 'pub-c-90209b1f-dd41-42a1-b64c-ab143ae25a56',
subscribeKey : 'sub-c-6eed774c-0281-11e7-8437-0619f8945a4f',
secretKey: "sec-c-NWY5ZjAzYWMtMWIxZi00ZTM2LTg3NGEtMDRjMGExZjI5YzNl",
ssl: true
});
module.exports = {
publish: function(channel, message){
pubnub.publish({
channel: channel,
message: JSON.stringify(message)},
function(status, response) {
if (status.error) {
console.log(status)
} else {
console.log("message Published w/ timetoken", response.timetoken)
}
});
},
subscribe: function(channel, callback){
pubnub.addListener({
message: function(m) {
// handle message
var msg = m.message; // The Payload
callback(msg);
}
});
// Subscribe to the demo_tutorial channel
pubnub.subscribe({
channels: [channel]
});
}
}
via Russkiy