Advertisement
Guest User

Untitled

a guest
Oct 7th, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. Server.js code:
  2.  
  3. const express = require('express');
  4. const app = express();
  5. const nodemailer = require('nodemailer');
  6.  
  7. // Middleware to parse JSON and URL-encoded form data
  8. app.use(express.json());
  9. app.use(express.urlencoded({ extended: true }));
  10.  
  11. // Create a nodemailer transporter (configure with your email service)
  12. const transporter = nodemailer.createTransport({
  13. service: 'gmail',
  14. auth: {
  15. user: 'example@gmail.com',
  16. pass: 'app-password' // Generate an App Password for security
  17. }
  18. });
  19.  
  20. // Define a route to handle form submissions
  21. app.post('/sendReminder', (req, res) => {
  22. const email = req.body.email;
  23. const reminderTime = new Date(req.body['reminder-time']);
  24.  
  25. // Schedule sending of email reminder
  26. setTimeout(() => {
  27. const mailOptions = {
  28. from: 'example@gmail.com',
  29. to: email,
  30. subject: 'Reminder',
  31. text: 'This is your reminder!'
  32. };
  33.  
  34. transporter.sendMail(mailOptions, (error, info) => {
  35. if (error) {
  36. console.error('Error sending email:', error);
  37. res.status(500).json({ error: 'Error sending email' }); // Respond with JSON error
  38. } else {
  39. console.log('Email sent: ' + info.response);
  40. res.json({ message: 'Reminder email sent successfully!' }); // Respond with JSON success message
  41. }
  42. });
  43. }, reminderTime - Date.now()); // Corrected the closing parenthesis for setTimeout
  44. });
  45.  
  46. // Start the server
  47. app.listen(3000, () => {
  48. console.log('Server is running on port 3000');
  49. });
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement