Iram Imran
Iram Imran

Reputation: 5

Send Email with user Form using Nodemailer

I have my website working live I have a form for user if user or visitors who want ask any question can send me his her request by filling that form and when user click on submit button all data should receive on my Gmail email account. My form has input fields of user name, user contact detail ,user email address and question in text box which user want to ask me . Please provide me complete step by step code with node js express JavaScript CSS and html.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Form</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Contact Form</h1>
    <form id="contact-form" action="/send-email" method="POST">

        <label for="username">Name:</label>
        <input type="text" id="username" name="username" required>
        <label for="contact">Contact:</label>
        <input type="text" id="contact" name="contact" required>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
        <label for="question">Question:</label>
        <textarea id="question" name="question" required></textarea>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

Here is my Html and server file. I have installed nodemailer express and upgraded version of node js. I have a public folder in which index.html is running successfully but when user or I click on submit button to send my request I got the error, This page isn’t workingIf the problem continues, contact the site owner. HTTP ERROR 405 Here is my index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Form</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Contact Form</h1>
    <form id="contact-form" action="/send-email" method="POST">

        <label for="username">Name:</label>
        <input type="text" id="username" name="username" required>
        <label for="contact">Contact:</label>
        <input type="text" id="contact" name="contact" required>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
        <label for="question">Question:</label>
        <textarea id="question" name="question" required></textarea>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

//here is my app.js file:

const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Serve static files from the 'public' directory
app.use(express.static('public'));

// Define a route for handling form submissions
app.post('/send-email', (req, res) => {
    const { username, contact, email, question } = req.body;

    // Create a nodemailer transporter using your email service provider
    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: '[email protected]',
            pass: 'rtindiyqirtodnvt'
        }
    });

    // Define email content
    let mailOptions = {
        from: '[email protected]',
        to: '[email protected]',
        subject: 'New Question from Website',
        text: `
            User Name: ${username}
            Contact: ${contact}
            Email: ${email}
            Question: ${question}
        `
    };

    // Send email
    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            console.log(error);
            res.status(500).send('Internal Server Error');
        } else {
            console.log('Email sent: ' + info.response);
            res.send('Email sent successfully');
        }
    });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});

I want when I or user click on submit button then email should receive on my [email protected] account. Other gmail account I configured and get access key after two factor authentication.

Upvotes: 0

Views: 89

Answers (1)

Sourabh Singh
Sourabh Singh

Reputation: 16

let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
     user: '',
     pass: ''
 },
    tls: {
        rejectUnauthorized: false
    }
});

Please use this code and close two factor authentication because we are using third party

Upvotes: 0

Related Questions