I do not want to compose an email. I just want to be able to launch the main email app on a user's device (iOS& Android) from a react-native app.
Scenario: I will send a verification email to the user upon signup.
I do not want to compose an email. I just want to be able to launch the main email app on a user's device (iOS& Android) from a react-native app.
Scenario: I will send a verification email to the user upon signup.
<Button onPress={() => Linking.openURL('mailto:support@example.com') }
title="support@example.com" />
<Button onPress={() => Linking.openURL('mailto:support@example.com?subject=SendMail&body=Description') }
title="support@example.com" />
<Button onPress={() => Linking.openURL('https://www.google.co.in/') }
title="www.google.co.in" />
##Don't forget to import
import { Linking } from 'react-native'
Note: Not supported in iOS simulator, so you must test on a device.
Unfortunately, none of the answers after are correct.
I do not want to compose an email. I just want to be able to launch the main email app
I would like to have the same behavior:
Open Email App
More or less the same as the Slack Onboarding with the magic link.
I found a solution with the library react-native-email-link. You can open an email client from React Native (for 'magic link' type feature).
mail.app
on iOS Simulator.Expo.io pure js/typescript solution:
import * as IntentLauncher from 'expo-intent-launcher';
// ...
public openMailClientIOS() {
Linking.canOpenURL('message:0')
.then(supported => {
if (!supported) {
console.log('Cant handle url')
} else {
return Linking.openURL('message:0')
.catch(this.handleOpenMailClientErrors)
}
})
.catch(this.handleOpenMailClientErrors)
}
public openMailClientAndroid() {
const activityAction = 'android.intent.action.MAIN'; // Intent.ACTION_MAIN
const intentParams: IntentLauncher.IntentLauncherParams = {
flags: 268435456, // Intent.FLAG_ACTIVITY_NEW_TASK
category: 'android.intent.category.APP_EMAIL' // Intent.CATEGORY_APP_EMAIL
};
IntentLauncher.startActivityAsync(activityAction, intentParams)
.catch(this.handleOpenMailClientErrors);
}
Works in iOS with Mail, works in Android
Android Intent docs: https://developer.android.com/reference/android/content/Intent#ACTION_MAIN
Expo IntentLauncher doc: https://docs.expo.io/versions/latest/sdk/intent-launcher/
To open email app on iOS:
Linking.canOpenURL('message:')
.then(supported => {
if (!supported) {
console.log('Cant handle url')
} else {
return Linking.openURL('message:')
}
})
.catch(err => {
console.error('An error occurred', err)
})
You can use this method to send open any email client and send an email with some data.
export const sendEmailViaEmailApp = (toMailId, subject, body) => {
if (!isUndefined(toMailId)) {
let link = `mailto:${toMailId}`;
if (!isUndefined(subject)) {
link = `${link}?subject=${subject}`;
}
if (isUndefined(subject)) {
link = `${link}?body=${body}`;
} else {
link = `${link}&body=${body}`;
}
Linking.canOpenURL(link)
.then(supported => {
if (supported) {
// 'mailto:support@example.com?subject=Billing Query&body=Description'
Linking.openURL(link);
}
})
.catch(err => console.error('An error occurred', err));
} else {
console.log('sendEmailViaEmailApp -----> ', 'mail link is undefined');
}
};
Place this method inside a utils class and use this method where ever you want
import { View,Linking,Text, Image,TouchableOpacity } from 'react-native';
const emailId= 'care@flipkart.com'
const onPressEmailClick = (email) => {
Linking.openURL('mailto:'+email)
// Linking.openURL('mailto:Care@amazon.com')
}
<View style={{ flexDirection: "row", alignItems: "center", justifyContent: "center" }} >
<Text style={{ textAlign: "center", marginTop: 15, color: "black" }} >
{"For any query mail us "}
</Text>
<TouchableOpacity
onPress={() => onPressEmailClick(emailId)} >
<Text style={{ textAlign: "center", marginTop: 15, color: "black", textDecorationLine: 'underline' }} >
{emailId}
</Text>
</TouchableOpacity>
You can open the gmail app using this as your URL googlegmail://
, its gmail's scheme. The first time your app opens gmail it will open an alert saying " wants to open gmail", the user can then tap ok or cancel. This behaviour only happens once.
This won't open the email composer but just straight to the user's primary inbox.
I have found a way to open mail account, not compose mail. Since you need physcial iOS device, I have tested only on Android (Samsung S9+) and it works.
import { openInbox } from "react-native-email-link";
import * as IntentLauncher from 'expo-intent-launcher';
const openMail = async () => {
if (Platform.OS === "ios") {
try {
await openInbox({ title: "Open mail app" });
} catch (error) {
console.error(`OpenEmailbox > iOS Error > ${error}`);
}
}
if (Platform.OS === "android") {
const activityAction = "android.intent.action.MAIN";
const intentParams = {
category: "android.intent.category.APP_EMAIL",
};
IntentLauncher.startActivityAsync(activityAction, intentParams);
}
}
I think the following npm module should have what you're looking for. Unfortunately it uses native libraries so you'll have to run some react-native links.
Use react-native-mail for launch email client. It will automatically open email client. https://www.npmjs.com/package/react-native-mail
If you want a wrapper that works with Android and iOS. https://www.npmjs.com/package/react-native-email-action
import { Linking } from 'react-native'
React Native Open Mail
<TouchableOpacity onPress={() => Linking.openURL('mailto:support@example.com')}>
<Text>support@example.com</Text>
</TouchableOpacity>
React Native Open Mail With Subject & Body
<TouchableOpacity onPress={() => Linking.openURL('mailto:support@example.com?subject=sendmail&body=details')}>
<Text>support@example.com</Text>
</TouchableOpacity>
this will only work in real device. not working in iOS simulator.
<TouchableOpacity onPress={()=>{
Linking.openURL('mailto:support@domain.com?subject=mailsubject&body=mailbody');
}}>
<View><Text>Contact Us</Text></View>
</TouchableOpacity>
This work for me.!