2013-12-20 01:31:16 -05:00
|
|
|
var secrets = require('../config/secrets');
|
|
|
|
var sendgrid = require('sendgrid')(secrets.sendgrid.user, secrets.sendgrid.password);
|
2013-11-30 13:14:29 -05:00
|
|
|
|
2013-12-20 13:48:33 -05:00
|
|
|
/**
|
|
|
|
* GET /contact
|
2014-01-07 18:15:14 -05:00
|
|
|
* Contact form page.
|
2013-12-20 13:48:33 -05:00
|
|
|
*/
|
2014-01-13 04:34:54 -05:00
|
|
|
|
2013-11-19 14:33:11 -05:00
|
|
|
exports.getContact = function(req, res) {
|
2013-11-19 23:19:53 -05:00
|
|
|
res.render('contact', {
|
|
|
|
title: 'Contact',
|
2013-11-30 13:27:43 -05:00
|
|
|
success: req.flash('success'),
|
2014-01-23 22:19:18 -05:00
|
|
|
errors: req.flash('errors')
|
2013-11-19 23:19:53 -05:00
|
|
|
});
|
2013-11-19 14:33:11 -05:00
|
|
|
};
|
|
|
|
|
2013-12-20 13:48:33 -05:00
|
|
|
/**
|
|
|
|
* POST /contact
|
2014-01-13 04:24:31 -05:00
|
|
|
* Send a contact form via SendGrid.
|
|
|
|
* @param {string} email
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string} message
|
2013-12-20 13:48:33 -05:00
|
|
|
*/
|
2014-01-13 04:24:31 -05:00
|
|
|
|
2013-11-19 14:33:11 -05:00
|
|
|
exports.postContact = function(req, res) {
|
2014-01-23 22:19:18 -05:00
|
|
|
req.assert('name', 'Name cannot be blank').notEmpty();
|
|
|
|
req.assert('email', 'Email cannot be blank').notEmpty();
|
|
|
|
req.assert('email', 'Email is not valid').isEmail();
|
|
|
|
req.assert('message', 'Message cannot be blank').notEmpty();
|
|
|
|
|
|
|
|
var errors = req.validationErrors();
|
|
|
|
|
|
|
|
if (errors) {
|
|
|
|
req.flash('errors', errors);
|
|
|
|
return res.redirect('/contact');
|
|
|
|
}
|
|
|
|
|
2013-11-30 13:34:28 -05:00
|
|
|
var from = req.body.email;
|
2013-12-04 20:55:01 -05:00
|
|
|
var name = req.body.name;
|
2014-01-13 04:24:31 -05:00
|
|
|
var body = req.body.message;
|
2014-01-23 22:25:13 -05:00
|
|
|
var to = 'you@email.com';
|
2013-11-30 13:27:43 -05:00
|
|
|
var subject = 'API Example | Contact Form';
|
|
|
|
|
2013-11-30 13:34:28 -05:00
|
|
|
var email = new sendgrid.Email({
|
2014-01-13 04:34:54 -05:00
|
|
|
to: to,
|
2013-12-20 13:48:33 -05:00
|
|
|
from: from,
|
|
|
|
subject: subject,
|
|
|
|
text: body + '\n\n' + name
|
2013-11-30 13:34:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
sendgrid.send(email, function(err) {
|
2013-11-30 13:27:43 -05:00
|
|
|
if (err) {
|
2014-01-23 22:25:13 -05:00
|
|
|
req.flash('errors', err.message);
|
2013-11-30 13:27:43 -05:00
|
|
|
return res.redirect('/contact');
|
|
|
|
}
|
|
|
|
req.flash('success', 'Email has been sent successfully!');
|
|
|
|
res.redirect('/contact');
|
2013-11-30 13:14:29 -05:00
|
|
|
});
|
2013-12-20 13:48:33 -05:00
|
|
|
};
|