How to send mail in react js

There are several ways to send mail in React JS, depending on your specific requirements and the type of mail you want to send. Here are a few options:

  1. Using a third-party email service: You can use a third-party email service like Mailgun, Sendgrid, or Mailjet to send emails from your React application. These services provide APIs that you can use to send emails programmatically.

Here's an example of how you can use Mailgun to send an email in React:

import axios from 'axios';

const mailgun = axios.create({
  baseURL: 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages',
  headers: {
    'Authorization': 'Basic YOUR_API_KEY',
    'Content-Type': 'application/x-www-form-urlencoded'
  }
});

const sendEmail = () => {
  const data = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Hello from React!',
    text: 'This is a test email sent from React!'
  };

  mailgun.post('', data)
   .then(response => console.log(response.data))
   .catch(error => console.error(error));
};
  1. Using a library like Nodemailer: Nodemailer is a popular library for sending emails in Node.js, and it can be used in React applications as well. You can install Nodemailer using npm or yarn:
    npm install nodemailer

    Here's an example of how you can use Nodemailer to send an email in React:

    
    import nodemailer from 'nodemailer';

const transporter = nodemailer.createTransport({ host: 'smtp.example.com', port: 587, secure: false, // or 'STARTTLS' auth: { user: '[email protected]', pass: 'your_password' } });

const sendEmail = () => { const mailOptions = { from: '[email protected]', to: '[email protected]', subject: 'Hello from React!', text: 'This is a test email sent from React!' };

transporter.sendMail(mailOptions, (error, info) => { if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); } }); };

3. **Using the `fetch` API**: You can also use the `fetch` API to send an email in React. This method is simpler than the previous two, but it may not be as reliable or scalable.

Here's an example of how you can use the `fetch` API to send an email in React:
```jsx
const sendEmail = () => {
  const data = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Hello from React!',
    text: 'This is a test email sent from React!'
  };

  fetch('https://your-email-service.com/send-email', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: JSON.stringify(data)
  })
   .then(response => response.json())
   .then(data => console.log(data))
   .catch(error => console.error(error));
};

In all cases, you'll need to replace the placeholders (e.g. YOUR_DOMAIN_NAME, YOUR_API_KEY, [email protected], etc.) with your actual email service credentials and settings.

Remember to handle errors and exceptions properly, and to test your email sending functionality thoroughly before deploying it to production.