Skip to main content

React + Nodemailer: the email gets sent but it opens a new window and as well, how can I implement it on live website

I am using react + nodemailer to make a contact form for my portfolio website, however I'm having some issues.

First issue is that the email does get sent, but as soon as it sends it opens a new window with the message as part of the url, which is not supposed to happen.

Before submitting info for email

After submition of info

Second, I want to use it in a live website instead in only my localhost:3000 -> https://example.com, but I havent found anything in the docs of nodemailer.

Here is the code of the server and the react code:

import React from 'react';
import { useState } from 'react';
import './contactForm.css';
import { Footer } from '../../containers';

import axios from 'axios';

const ContactForm = () => {
     //const [status, setStatus] = useState("Submit");
     const [recipient_email, setEmail] = useState("");
     const [name, setName] = useState("");
     const [message, setMessage] = useState("");

     function sendMail(){
        if(recipient_email && name && message){
            axios
                .post('http://localhost:5000/send_email', {
                    recipient_email,
                    name,
                    message,
                })
                .then(() => alert('Message sent succesfuly'))
                .catch(() => alert('Oops something went wrong'));
            return;
        }
        return alert('Fill in all the fields to continue');
     };

  return (
    <div className='RO__ContactForm' id='contactForm'>
        <div className='RO__ContactForm-title'>
            <h3>Contact</h3>
            <h1>I'm here to help you level up</h1>
        </div>
        <div className='RO__ContactForm-content'>
            <div className='RO__ContactForm-content_description'>
                <p>I'm just on click away to help you take your company 
                    to the next level. Fill in the form to share more 
                    details about the project or your favorite movie. 
                    Either way, I'd love to talk.</p>
                <p></p>
            </div>
            <form
                className='RO__ContactForm-content_form'
                target='_blank'
                >
                <div className='RO__ContactForm-content_form_name'>
                    <div className='RO__ContactForm-content_form_nameTitle'>
                        <h5>What's your name?</h5>
                    </div>
                    <input 
                        className='RO_ContactForm-content_form_nameInput'
                        type= 'text'
                        id='name'
                        onChange={ (e) => setName(e.target.value) }
                        name='name' required 
                    />
                </div>
                <div className='RO__ContactForm-content_form_email'>
                    <div className='RO__ContactForm-content_form_emailTitle'>
                        <h5>Your email</h5>
                    </div>
                    <input
                        className='RO__ContactForm-content_form_emailInput'
                        type='email'
                        id='email'
                        onChange={ (e) => setEmail(e.target.value) }
                        name='email' required
                    />
                </div>
                <div className='RO__ContactForm-content_form_info'>
                    <div className='RO__ContactForm-content_form_infoTitle'>
                        <h5>What can I help you with?</h5>
                    </div>
                    <textarea
                        className='RO__ContactForm-content_form_infoContent'
                        id='message'
                        onChange={ (e) => setMessage(e.target.value) }
                        name='message' required
                    />
                </div>
                <div className='RO__ContactForm-content_form_button'>
                    <button 
                        onClick = {() => sendMail()} 
                        type='submit'
                    >
                        Submit

                    </button>
                </div>

            </form>
        </div>
        <div className='RO__ContactForm-footer'>
            <Footer  />
        </div>
        
    </div>
  )
}

export default ContactForm

server code:

const { response } = require('express');
const express = require('express');
const nodemailer = require('nodemailer');
const cors = require('cors');
const app = express();
const port = 5000;

app.use(cors());
app.use(express.json({ limit: '25mb' }));
app.use(express.urlencoded({ limit: '25mb' }));
app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    next();
});

function sendEmail({ recipient_email, name, message }){
    return new Promise((resolve, reject) => {
        var transporter = nodemailer.createTransport({
            service: 'Hotmail',
            auth: {
                user: '***********@hotmail.com',
                pass: '**********',
            },
        });

        const mail_configs = {
            from: 'darkknight-3096@hotmail.com',
            to: 'irvin.rafael.3096@gmail.com',
            subject: 'Test',
            text: `Name: ${name} \n Email: ${recipient_email} \n Message: ${message}`,
        };
        transporter.sendMail(mail_configs, function(error, info){
            if(error){
                console.log(error);
                return reject({ message: 'An error has occured' });
            }
            return resolve({ message: 'Email has been sent succesfuly' });
        });
    });
}

app.get('/contactForm', (req, res) => {
    sendEmail()
        .then((response) => res.send(response.message), console.log(response.message))
        .catch((error) => res.status(500).send(error.message));
});

app.post("/send_email", (req, res) => {
    sendEmail(req.body)
        .then((response) => res.send(response.message))
        .catch((error) => res.status(500).send(error.message));
});

app.listen(port, () => {
    console.log(`nodemailerProject is listening at localhost:${port}`);
}); 
Via Active questions tagged javascript - Stack Overflow https://ift.tt/gwQANlT

Comments

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data