Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Email in React Native

I am working on a React Native application. I need to send an email using an Email service, like an API. Can anyone please guide me through the process?

like image 579
Manish Avatar asked Jul 06 '26 07:07

Manish


1 Answers

In your scenario, you can use Linking in react native.

Import this first

import { Linking } from 'react-native'

React Native Open Mail Function :

<Button onPress={() => Linking.openURL('mailto:[email protected]') }
  title="[email protected]" />

React Native Open Mail Function With Subject and Body

<Button onPress={() => Linking.openURL('mailto:[email protected]?subject=SendMail&body=Description') }
      title="[email protected]" />

React Native Open URL

<Button onPress={() => Linking.openURL('https://www.google.co.in/') }
      title="www.google.co.in" />

More Details :

mailto:?subject=&body=&cc=

<receiver_email> Important value. The email address to send email.

<subject> default email subject that will attach to new email (option)

<body> default email text (option)

<emails_to_copy> list of emails to copy (option)

Example Full Code

// send-email.js

// We can use react-native Linking to send email
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);
}


// example.js

import { sendEmail } from './send-email';

sendEmail(
    '[email protected]',
    'Greeting!',
    'I think you are fucked up how many letters you get.'
).then(() => {
    console.log('Our email successful provided to device mail ');
});

For sending emails as background service :

I think you need a e-mail server or atleast an email service to send an email with your app. I don't think you can send e-mails directly from the client side.

But there are various email services on the Internet that you can use for free (like Mailgun or SendPulse. There you can just use a simple POST method from the code in your app against their APIs.

If you wan to send email using SMTP protocol then you can use the react native smtp module or react-native-smtp-mailer. You can read about it from here.

like image 72
Akila Devinda Avatar answered Jul 11 '26 11:07

Akila Devinda