Guest User

Untitled

a guest
Mar 14th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. export default function ajax() {
  2. const form = document.forms[0];
  3.  
  4. const xhr = new XMLHttpRequest();
  5.  
  6. form.addEventListener('submit', (e) => {
  7. e.preventDefault();
  8.  
  9. // FORMDATA TO JSON OBJECT
  10. const fields = document.querySelectorAll('form > div');
  11. let formData = {};
  12. fields.forEach((container) => {
  13. formData[container.childNodes[0].name] = container.childNodes[0].value;
  14. })
  15. const formDataJSON = JSON.stringify(formData);
  16.  
  17. xhr.open('POST', '/send', true);
  18. xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  19.  
  20. xhr.onreadystatechange = function() {
  21. if(this.readyState === this.DONE && this.status === 200)
  22. console.log('success');
  23.  
  24. else if(this.readyState === this.UNSET)
  25. console.log('failed');
  26.  
  27. else if(this.readyState === this.LOADING)
  28. console.log('loading');
  29. }
  30.  
  31. xhr.send(formDataJSON);
  32. })
  33. }
  34.  
  35. module.exports = (data) => {
  36. const _auth = require('./_auth');
  37. const nodemailer = require('nodemailer');
  38.  
  39. const transporter = nodemailer.createTransport({
  40. service: 'gmail',
  41. auth: {
  42. user: _auth.user,
  43. pass: _auth.pass
  44. }
  45. });
  46.  
  47. const html = `
  48. <div style="background-color: #F3F4F7; border-radius: 10px; padding: 50px;">
  49. <h2>Sie haben eine neue Nachricht</h2>
  50. <p>${data.nachricht}</p>
  51. <h3>Absender</h3>
  52. <ul>
  53. <li>Name: ${data.name}</li>
  54. <li>Email: ${data.email}</li>
  55. <li>Tel: ${data.tel}</li>
  56. </ul>
  57. </div>
  58. `;
  59.  
  60. const mailOptions = {
  61. from: data.email,
  62. to: _auth.user,
  63. subject: '20° - Nachricht',
  64. text: data.nachricht,
  65. html
  66. };
  67.  
  68. transporter.sendMail(mailOptions, function(error, info){
  69. if (error)
  70. console.log(error);
  71. else
  72. console.log('Email sent: ' + info.response);
  73.  
  74. transporter.close();
  75. });
  76. }
Add Comment
Please, Sign In to add comment