forked from larvalabs/pullup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact.js
57 lines (48 loc) · 1.34 KB
/
contact.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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) {
res.render('contact', {
title: 'Contact'
});
};
/**
* POST /contact
* Send a contact form via SendGrid.
* @param {string} email
* @param {string} name
* @param {string} message
*/
exports.postContact = function(req, res) {
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');
}
var from = req.body.email;
var name = req.body.name;
var body = req.body.message;
var to = '[email protected]';
var subject = 'API Example | Contact Form';
var email = new sendgrid.Email({
to: to,
from: from,
subject: subject,
text: body + '\n\n' + name
});
sendgrid.send(email, function(err) {
if (err) {
req.flash('errors', { msg: err.message });
return res.redirect('/contact');
}
req.flash('success', { msg: 'Email has been sent successfully!' });
res.redirect('/contact');
});
};