Giter VIP home page Giter VIP logo

Comments (14)

ollyde avatar ollyde commented on August 10, 2024 3

For anyone else stuck on this, you'll have to write a native module as such:

public class NativeModuleSendEmail extends ReactContextBaseJavaModule {

    public NativeModuleSendEmail(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "SendEmail";
    }

    @ReactMethod
    public void sendEmail(final String emailAddress, final String subject) {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:"));
        intent.putExtra(Intent.EXTRA_EMAIL  , new String[] { emailAddress });
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);

        getCurrentActivity().startActivity(Intent.createChooser(intent, ""));
    }

    public static class ReactPackage implements com.facebook.react.ReactPackage {
        @Override
        public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
            return Collections.emptyList();
        }

        @Override
        public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
            List<NativeModule> modules = new ArrayList<>();
            modules.add(new NativeModuleSendEmail(reactContext));
            return modules;
        }
    }
}

Then register it:

return Arrays.<ReactPackage>asList(
          new NativeModuleSendEmail.ReactPackage()
    );

Create the JS file

/**
 * @providesModule send-email
 */

// @flow

import { NativeModules } from 'react-native';
module.exports = NativeModules.SendEmail;

Use it:

import SendMail from 'send-email';
SendMail.sendEmail('some@email', subject);

from react-native-mail.

jasan-s avatar jasan-s commented on August 10, 2024 1

I also posted a stackoverflow question:

from react-native-mail.

chirag04 avatar chirag04 commented on August 10, 2024

use Linking module from react native for that.

from react-native-mail.

dwilt avatar dwilt commented on August 10, 2024

@chirag04 I can't find an example of this anywhere using Linking. Do you have one?

from react-native-mail.

chirag04 avatar chirag04 commented on August 10, 2024

http://facebook.github.io/react-native/releases/0.42/docs/linking.html#openurl

from react-native-mail.

dwilt avatar dwilt commented on August 10, 2024

Right, but do you know the value that should be passed in to openURL to open the Mail app?

from react-native-mail.

dwilt avatar dwilt commented on August 10, 2024

I'm looking into this more and seeing the idea here. For iOS, we're supposed to pass the URL scheme for iOS. This works well to detect if the user has different mail clients installed as iOS doesn't allow you to change the default Mail app. So, if the user had Inbox for iOS installed, we can detect if that's there first, and then open that:

var Mailer = require('NativeModules').RNMail;
if (Platform.OS === `ios`) {
    const inbox = await Linking.canOpenURL(`inbox-gmail:`);

    // if inbox is installed, open that
    if (inbox) {
        Linking.openURL(`inbox-gmail:`);
    } else {
    // else default to iOS Mail app
        Linking.openURL(`message:`);
    }
} else if (Platform.OS === `android`) {
    // open default mail app via exposed method for Android via logic like: http://stackoverflow.com/a/31594815/1681896
   // Mailer.openDefaultClient(); 
}

For Android, we could use the intent url, but defaulting to the default mail app would be pretty easy via this made-up openDefaultClient method that would use a solution similar to http://stackoverflow.com/a/31594815/1681896

from react-native-mail.

dwilt avatar dwilt commented on August 10, 2024

For the record, as I mentioned above, the iOS conditional I have works perfect. However, besides mailto: (which opens a new message and that's the opposite of this feature request), I can't figure out a value to pass into openURL that will launch any kind of mail application on my Android device. I've tried com.google.android.gm per http://stackoverflow.com/a/3592060/1681896 to try and get it to launch Gmail but with no success

from react-native-mail.

jasan-s avatar jasan-s commented on August 10, 2024

@dwilt did you find a solution to opening the mail app for both ios and android? I want to launch mail app to guide user to verify email upon account creation. Linking.openURL(message:); or Linking.openURL(inbox-gmail:); both lead to unhandled exception errors.

from react-native-mail.

dwilt avatar dwilt commented on August 10, 2024

@jasan-s no, I wasn't able to find one. We ended up abandoning the idea of opening the mail client automatically since the email notification coming in from the top added to the user experience of them seeing the email come in just as we showed the success message of saying we sent one.

from react-native-mail.

jasan-s avatar jasan-s commented on August 10, 2024

from react-native-mail.

markokostovski avatar markokostovski commented on August 10, 2024

Would like to say that Linking.openURL('mailto:[email protected]?subject=abcdefg&body=body') worked for me, but only on iOS, when I was on simulator it didn't.. threw error

from react-native-mail.

roots-ai avatar roots-ai commented on August 10, 2024

@OllyDixon How to handle this for iOS?

from react-native-mail.

ollyde avatar ollyde commented on August 10, 2024

@roots-ai I no longer do React Native. We have migrated onto Flutter.

from react-native-mail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.