Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sendMessage = () => {
- const {fName, lName, email, message} = this.state;
- if (email === '' || fName === '' || lName === '' || message === '') {
- this.setState({error: 'field'});
- } else if (!this.validateEmail(email)) {
- this.setState({error: 'email'});
- } else {
- axios.post('http://localhost:8079/contact', {
- fName: fName,
- lName: lName,
- email: email,
- messsage: message
- }).then(response => {
- console.log('Then is being fired')
- if (response.data.msg === 'success') {
- this.setState({fName: '', lName: '', email: '', message: ''});
- this.setState({error: null});
- this.setState({success: true});
- } else if (response.data.msg === 'fail') {
- this.setState({error: 'response'});
- } else {
- console.log('This is the else function');
- }
- });
- }
- }
- let nodemailer = require('nodemailer');
- const creds = require('./config.js');
- const express = require('express');
- const app = express();
- const bodyParser = require('body-parser');
- const PORT = process.env.PORT || 8079;
- // parse application/x-www-form-urlencoded
- app.use(bodyParser.urlencoded({ extended: false }))
- app.use(bodyParser.json());
- app.listen(PORT, () => {
- console.log('Hey its running on port: ' + PORT);
- });
- // POST route from contact form
- app.post('/contact', (req, res) => {
- console.log('Contact request has been sent');
- let mailOpts, smtpTrans;
- smtpTrans = nodemailer.createTransport({
- host: 'smtp.gmail.com',
- port: 465,
- secure: true,
- auth: {
- user: creds.USER,
- pass: creds.PASS
- }
- });
- mailOpts = {
- from: `${req.body.fName} ${req.body.lName}: ${req.body.email}`,
- to: creds.USER,
- subject: 'New message from contact form at apperbuild.com!',
- text: `${req.body.fName} ${req.body.lName} (${req.body.email}) says: ${req.body.message}`
- };
- console.log('Before Send Mail')
- smtpTrans.sendMail(mailOpts, (error, res) => {
- console.log('After Send Mail')
- if (error) {
- console.log('No error');
- res.send('fail');
- }
- else {
- console.log('Everything worked')
- res.send('success');
- }
- });
- });
Add Comment
Please, Sign In to add comment