How to send the content of a form as email in React Native?

Viewed 37

With react, there have been methods like EmailJs but I don't know any services that takes the content from a form and sends it as email in React Native. Can anyone provide some pointers to help with this issue thank you

1 Answers

create a function that receives input reshapes it to an email content and redirects you to gmail only to send..install qs from npm

import qs from 'qs';
import { Linking } from 'react-native';


export async function sendEmail(to, subject, body, options = {}) {
    const { cc, bcc } = options;

    let url = `mailto:${to}`;

    // Create email link query
    const query = qs.stringify({
        subject: subject,
        body: body,
        cc: cc,
        bcc: bcc
    });

    if (query.length) {
        url += `?${query}`;
    }

    // check if we can use this link
    const canOpen = await Linking.canOpenURL(url);

    if (!canOpen) {
        throw new Error('Provided URL can not be handled');
    }

    return Linking.openURL(url);
}

//then import the above function and use it as

 sendEmail(
                'destinationemail@gmail.com',
                your emailsubject,
                your emailmessage,
                { cc: 'test@gmail.com; test2@gmail.com;' }
            ).then(() => {
                console.log('Your message was successfully sent!');
            });
Related