freeCodeCamp/controllers/contact.js

44 lines
1015 B
JavaScript
Raw Normal View History

var secrets = require('../config/secrets');
var sendgrid = require('sendgrid')(secrets.sendgrid.user, secrets.sendgrid.password);
/**
* GET /contact
* Contact form page
*/
exports.getContact = function(req, res) {
2013-11-20 04:19:53 +00:00
res.render('contact', {
title: 'Contact',
2013-11-20 13:03:10 +00:00
user: req.user,
success: req.flash('success'),
error: req.flash('error')
2013-11-20 04:19:53 +00:00
});
};
/**
* POST /contact
* Send a contact form message via SendGrid
*/
exports.postContact = function(req, res) {
var from = req.body.email;
2013-12-05 01:55:01 +00:00
var name = req.body.name;
var body = req.body.contactBody;
var sendTo = 'sakhat@gmail.com';
var subject = 'API Example | Contact Form';
var email = new sendgrid.Email({
to: sendTo,
from: from,
subject: subject,
text: body + '\n\n' + name
});
sendgrid.send(email, function(err) {
if (err) {
req.flash('error', err.message);
return res.redirect('/contact');
}
req.flash('success', 'Email has been sent successfully!');
res.redirect('/contact');
});
};