Guest User

Untitled

a guest
Jul 16th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. sendMessage = () => {
  2. const {fName, lName, email, message} = this.state;
  3.  
  4. if (email === '' || fName === '' || lName === '' || message === '') {
  5. this.setState({error: 'field'});
  6. } else if (!this.validateEmail(email)) {
  7. this.setState({error: 'email'});
  8. } else {
  9. axios.post('http://localhost:8079/contact', {
  10. fName: fName,
  11. lName: lName,
  12. email: email,
  13. messsage: message
  14. }).then(response => {
  15. console.log('Then is being fired')
  16. if (response.data.msg === 'success') {
  17. this.setState({fName: '', lName: '', email: '', message: ''});
  18. this.setState({error: null});
  19. this.setState({success: true});
  20. } else if (response.data.msg === 'fail') {
  21. this.setState({error: 'response'});
  22. } else {
  23. console.log('This is the else function');
  24. }
  25. });
  26. }
  27. }
  28.  
  29. let nodemailer = require('nodemailer');
  30. const creds = require('./config.js');
  31.  
  32. const express = require('express');
  33. const app = express();
  34.  
  35. const bodyParser = require('body-parser');
  36. const PORT = process.env.PORT || 8079;
  37.  
  38. // parse application/x-www-form-urlencoded
  39. app.use(bodyParser.urlencoded({ extended: false }))
  40. app.use(bodyParser.json());
  41.  
  42. app.listen(PORT, () => {
  43. console.log('Hey its running on port: ' + PORT);
  44. });
  45.  
  46. // POST route from contact form
  47. app.post('/contact', (req, res) => {
  48. console.log('Contact request has been sent');
  49. let mailOpts, smtpTrans;
  50. smtpTrans = nodemailer.createTransport({
  51. host: 'smtp.gmail.com',
  52. port: 465,
  53. secure: true,
  54. auth: {
  55. user: creds.USER,
  56. pass: creds.PASS
  57. }
  58. });
  59.  
  60. mailOpts = {
  61. from: `${req.body.fName} ${req.body.lName}: ${req.body.email}`,
  62. to: creds.USER,
  63. subject: 'New message from contact form at apperbuild.com!',
  64. text: `${req.body.fName} ${req.body.lName} (${req.body.email}) says: ${req.body.message}`
  65. };
  66.  
  67. console.log('Before Send Mail')
  68. smtpTrans.sendMail(mailOpts, (error, res) => {
  69. console.log('After Send Mail')
  70. if (error) {
  71. console.log('No error');
  72. res.send('fail');
  73. }
  74. else {
  75. console.log('Everything worked')
  76. res.send('success');
  77. }
  78. });
  79.  
  80. });
Add Comment
Please, Sign In to add comment