I’m having trouble getting my contact form to send emails using Node.js Express and the mailgun-js library. The API credentials work fine when I test them with the official examples, but my implementation doesn’t seem to work. Can someone help me figure out what’s wrong?
Email service module:
const API_KEY = 'my-secret-key';
const MAIL_DOMAIN = 'my-domain.com';
const MailgunAPI = require('mailgun-js');
exports.deliverEmail = function (formData, done) {
console.log(formData);
const mailService = new MailgunAPI({apiKey: API_KEY, domain: MAIL_DOMAIN});
const emailData = {
from: '[email protected]',
to: '[email protected]',
subject: 'New Contact Form',
text: 'Someone filled out the contact form!'
};
mailService.messages().send(emailData, function (error, response) {
if(error) return done(error);
console.log('email delivered');
done(null, response);
});
};
Route handler:
const emailService = require('../services/email');
exports.showContactPage = function (req, res, next) {
res.render('contact-form');
};
exports.handleFormSubmit = function (req, res, next) {
emailService.deliverEmail(req.body, function (error, result) {
if(error) return next(error);
console.log(result);
res.json({status: 'Message sent successfully'});
});
};
Any ideas what might be going wrong here?