contact_control.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var secure = require('../config/secure');
  2. var nodemailer = require("nodemailer");
  3. var transporter = nodemailer.createTransport({
  4. service: 'Mandrill',
  5. auth: {
  6. user: secure.mandrill.user,
  7. pass: secure.mandrill.password
  8. }
  9. });
  10. /********** GET / Contact **************/
  11. exports.getContact = function(req, res) {
  12. res.render('contact', {
  13. title: 'Contact'
  14. });
  15. };
  16. /********** POST / Contact **************/
  17. exports.postContact = function(req, res) {
  18. req.assert('name', 'Name cannot be blank').notEmpty();
  19. req.assert('email', 'Email is not valid').isEmail();
  20. req.assert('message', 'Message cannot be blank').notEmpty();
  21. var errors = req.validationErrors();
  22. if (errors) {
  23. req.flash('errors', errors);
  24. return res.redirect('/contact');
  25. }
  26. var from = req.body.email;
  27. var name = req.body.name;
  28. var body = req.body.message;
  29. var to = 'admin@juryd.com';
  30. var subject = 'Contact Form | Juryd';
  31. var mailOptions = {
  32. to: to,
  33. from: from,
  34. subject: subject,
  35. text: body
  36. };
  37. transporter.sendMail(mailOptions, function(err) {
  38. if (err) {
  39. req.flash('errors', { msg: err.message });
  40. return res.redirect('/contact');
  41. }
  42. req.flash('success', { msg: 'Email has been sent successfully!' });
  43. res.redirect('/contact');
  44. });
  45. };