Created
March 8, 2018 23:31
-
-
Save koldovsky/cf5795aeab7105b86562d3219362a421 to your computer and use it in GitHub Desktop.
Formspree alternative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
const nodemailer = require('nodemailer'); | |
const bodyParser = require('body-parser'); | |
const path = require('path'); | |
const transporter = nodemailer.createTransport({ | |
service: 'gmail', | |
auth: { | |
user: 'YOUR_GMAIL', | |
pass: 'YOUR_PASS' | |
} | |
}); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(express.static(path.resolve(__dirname, 'public'))); | |
app.get('/', function(req, res) { | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
app.post('/email/:address', (req, res) => { | |
const mailOptions = { | |
from: 'infolve.team@gmail.com', | |
to: req.params.address, | |
subject: 'Email from form-sender', | |
html: JSON.stringify(req.body) | |
}; | |
transporter.sendMail(mailOptions, function(error, info) { | |
if (error) { | |
console.error(error); | |
res.status(500).send('Server error'); | |
} else { | |
console.log('Email sent: ' + info.response); | |
res.send(`Adress: ${req.params.address}, Data sent: ${JSON.stringify(req.body)}`); | |
} | |
}); | |
}); | |
app.listen(process.env.PORT || 3000, process.env.IP || '0.0.0.0' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment