How to send a mail using react js

To send an email using React JS, you'll need to use a library or service that provides email sending functionality. Here are a few options:

  1. EmailJS: EmailJS is a popular library that allows you to send emails from your React application. You can install it using npm or yarn:
    npm install emailjs

    Then, you can use the following code to send an email:

    
    import emailjs from 'emailjs';

const sendEmail = () => { const serviceID = 'your-service-id'; const templateID = 'your-template-id'; const userId = 'your-user-id';

const templateParams = { from_name: 'Your Name', from_email: '[email protected]', to_name: 'Recipient Name', to_email: '[email protected]', subject: 'Test Email', message: 'This is a test email sent from React!' };

emailjs.send(serviceID, templateID, templateParams, userId) .then((response) => { console.log('Email sent successfully!', response); }) .catch((err) => { console.log('Error sending email:', err); }); };

2. **Nodemailer**: Nodemailer is another popular library that allows you to send emails from your React application. You'll need to set up a Node.js server to use Nodemailer. Here's an example:

import nodemailer from 'nodemailer';

const sendEmail = () => { const transporter = nodemailer.createTransport({ host: 'your-smtp-host', port: 587, secure: false, // or 'STARTTLS' auth: { user: '[email protected]', pass: 'your-password' } });

const mailOptions = { from: '[email protected]', to: '[email protected]', subject: 'Test Email', text: 'This is a test email sent from React!' };

transporter.sendMail(mailOptions, (error, info) => { if (error) { console.log('Error sending email:', error); } else { console.log('Email sent successfully!', info); } }); };

3. **SendGrid**: SendGrid is a cloud-based email service that provides a simple API for sending emails. You can use the SendGrid API to send emails from your React application. Here's an example:

import axios from 'axios';

const sendEmail = () => { const apiKey = 'your-sendgrid-api-key'; const fromEmail = '[email protected]'; const toEmail = '[email protected]'; const subject = 'Test Email'; const text = 'This is a test email sent from React!';

axios.post(https://api.sendgrid.com/v3/mail/send, { personalizations: [ { to: [{ email: toEmail, name: '' }], subject } ], from: { email: fromEmail }, content: [ { type: 'text/plain', value: text } ] }, { headers: { 'Authorization': Bearer ${apiKey}, 'Content-Type': 'application/json' } }) .then((response) => { console.log('Email sent successfully!', response); }) .catch((error) => { console.log('Error sending email:', error); }); };


Make sure to replace the placeholders with your own values (e.g., service ID, template ID, user ID, SMTP host, email address, password, etc.).

In each example, you'll need to modify the code to fit your specific use case and requirements.