Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fill out form and then process it when sending an email

  • Loading branch information...
commit b2c25e2b61004a221e759df2876fea74796fc59f 1 parent 0446fe4
@cbrake authored
View
32 app.js
@@ -1,10 +1,14 @@
-var express = require('express')
-var fs = require('fs')
-var nodemailer = require('nodemailer')
-var config = require('./config').config
+var express = require('express'),
+ fs = require('fs'),
+ nodemailer = require('nodemailer'),
+ config = require('./config').config,
+ sys = require('sys')
var app = express()
+// this is required to parse the body of HTML POSTs, etc
+app.use(express.bodyParser())
+
var transport = nodemailer.createTransport(config.email.transport.type, config.email.transport.options)
app.configure(function() {
@@ -16,28 +20,34 @@ app.get('/', function(req, res) {
res.sendfile(__dirname + '/public/' + 'index.html');
})
-app.get('/send-mail', function(req, res) {
+app.post('/send-mail', function(req, res) {
+
+ // the following can be used to dump the req data structure
+ // to see where your data located -- useful for debugging
+ // console.log(sys.inspect(req))
+ console.log("message = " + req.body.message)
+
var mailOptions = {
from: config.email.from,
to: config.email.to,
- subject: "test message"
+ subject: "test message",
+ text: req.body.message
}
-
transport.sendMail(mailOptions, function(error, response) {
if (error) {
console.log(error)
- res.send('Error sending mail: ' + error)
+ // res.send('Error sending mail: ' + error)
+ res.redirect("/error-page.html")
} else {
console.log("Message sent: " + response.message)
- res.send('Message sent: ' + response.message)
+ //res.send('Message sent: ' + response.message)
+ res.redirect("/message-sent.html")
}
})
})
-
-
//app.listen(process.env.VMC_APP_PORT || 1377, null)
console.log("listen on port 3030")
app.listen(3030)
View
11 public/error-page.html
@@ -0,0 +1,11 @@
+<!DOCTYPE HTML>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title></title>
+</head>
+<body>
+ Sorry, error sending message.<br>
+ <a href="/">go back to main page</a>
+</body>
+</html>
View
14 public/index.html
@@ -1 +1,13 @@
-hello world
+<!DOCTYPE HTML>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title></title>
+</head>
+<body>
+ <form action="/send-mail" method="post">
+ Message: <input type="text" name="message">
+ <input type="submit" value="Send">
+ </form>
+</body>
+</html>
View
11 public/message-sent.html
@@ -0,0 +1,11 @@
+<!DOCTYPE HTML>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title></title>
+</head>
+<body>
+ Message sent, thank you.<br>
+ <a href="/">go back to main page</a>
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.