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
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
Post a Comment