Sending email via mailgun
I am trying to add a mailgun contact form to my site using nodejs express and mailgun-js. But somehow I can't get it to work. My api key and domain are good as I tested them with sample code on mailgun-js official github page. So I am wondering if there is anything wrong with the following parts of the code. (routing and everything else works fine)
./models/mailer.js
var api_key = 'xxxxxxx';
var domain = 'xxxxxxxx';
var Mailgun = require('mailgun-js');
exports.sendOne = function (locals,callback) {
console.log(locals);
var mailgun = new Mailgun({apiKey: api_key,domain:domain});
var data = {
from: 'xxxxxx',
to: 'myemail@hotmail.com',
subject: 'Hello World',
text: 'Testing some Mailgun awesomness!'
};
mailgun.message().send(data,function (err,body) {
if(err) return callback(err);
console.log('message sent');
callback(null,body);
});
};
./controllers/contactCtrl.js
var mailer = require('../models/mailer');
exports.contact = function (req,res,next) {
res.render('contact');
};
exports.receiveMessage = function (req,res,next) {
mailer.sendOne(req.body,function (err,body) {
if(err) return next(err);
console.log(body);
res.send({message:'Your message has been successfully sent'});
});
};
Thank you so much:)
+3
source to share