Sonia
Sonia

Reputation: 123

Mailto in ReactNative

I try to add a button to send an email from the mail provider of the user, like so

_OnPressMailto(emails){
  console.log(emails[0].email)
  const address = emails[0].email
  Linking.openURL('mailto:address');
}

Linking.openURL works perfectly for my other links to fb and twitter. But the mailto returns an error=> Unhandled Promise.

An idea ?

Upvotes: 0

Views: 4364

Answers (3)

Pir Shukarullah Shah
Pir Shukarullah Shah

Reputation: 4232

You are not concatenating address properly with mailto. Either use + operator or template as follow Linking.openURL(`mailto:${address}`).

_onMailto(emails) {
  console.log(emails[0].email);
  const address = emails[0].email;
  this.launchURL(`mailto:${address}`);
}


launchURL(url) {
  Linking.canOpenURL(url).then(supported => {
    if(!supported) {
            console.log('Can\'t handle url: ' + url);
        } else {
            Linking.openURL(url)
            .catch(err => {
        console.warn('openURL error', err);
            });
        }
    }).catch(err => console.warn('An unexpected error happened', err));
}

Upvotes: 1

Waqas Ahmed
Waqas Ahmed

Reputation: 1411

Use https://github.com/anarchicknight/react-native-communications it is a simple package to open different urls

Upvotes: 1

monssef
monssef

Reputation: 1004

from the react native docs

openURL()
returns a Promise object. If the user confirms the open dialog or the url automatically opens, the promise is resolved. If the user cancels the open dialog or there are no registered applications for the url, the promise is rejected.

NOTE: This method will fail if the system doesn't know how to open the specified URL.

you should probably use canOpenURL to determine whether or not an installed app can handle a given URL, or at least handle the returned promise rejections.

Upvotes: 1

Related Questions