skip to Main Content

Hi there I am trying to send server less email through react native I have researched about it and came to know that it is possible to send in Reactjs using EMAILJS but in react-native I have not found any solution yet. Can anyone help in this scenario ? or guide me about any available library. Thanks in advance

3

Answers


  1. EmailJS supports REST APIs. You can directly call them from your React Native app without any 3rd party library.

    const sendMail = async () => {
      const url = 'https://api.emailjs.com/api/v1.0/email/send';
    
      const data = {
        service_id: 'YOUR_SERVICE_ID',
        // other data .....
      };
      
      const params = {
        method: 'POST',
        body: JSON.stringify(data),
        headers: {
          'Content-Type': 'application/json'
        }
      };
      
      try {
        await fetch(url, params);
        console.log('Your mail is sent!');
      } catch (error) {
        console.error(error);
      }
    };
    
    Login or Signup to reply.
  2. I was not receiving the email either, but make sure to check out Account/Security to make sure "Allow EmailJS API for non-browser applications" is checked. Also I included my private key per EmailJS’s recommendations.

    Here is the initial code I got to work using axios:

    const sendEmail = async () => {
      try {
          const payload = {
              service_id: 'SERVICE_ID',
              template_id: 'TEMPLATE_ID',
              user_id: 'PUBLIC_KEY', //this includes a dash that is part of my key
              accessToken: 'PRIVATE_KEY',
              template_params: {
                  from_name: 'James',
                  to_name: 'David',
                  message: 'Test 123'
              }
          };
          const headers = { "Content-Type": "application/json" };
          const { status } = await axios.post('https://api.emailjs.com/api/v1.0/email/send', payload, { headers });
          if(status === 200) {
              console.log('success');
          }
      } catch (error) {
          console.log('error in axios Post-->', error);
      }
    }
    

    I hope this helps.

    Login or Signup to reply.
  3. A better solution is to open the default email client with prepopulated data from your application using https://github.com/chirag04/react-native-mail for example.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search