React Native course · Module 13: Native APIs
expo-sms & expo-mail-composer - SMS and Email
In this lesson3
Send SMS messages and emails!
expo-sms
npx expo install expo-smsimport * as SMS from 'expo-sms';
async function sendSMS(phoneNumber: string, message: string) {
const isAvailable = await SMS.isAvailableAsync();
if (!isAvailable) {
Alert.alert('SMS not available');
return;
}
const { result } = await SMS.sendSMSAsync(
[phoneNumber], // Can provide multiple numbers
message
);
// result: 'sent', 'cancelled', or 'unknown'
return result;
}
// Example usage
await sendSMS('+48123456789', 'Greetings from Atlantis!');expo-mail-composer
npx expo install expo-mail-composerimport * as MailComposer from 'expo-mail-composer';
async function sendEmail() {
const isAvailable = await MailComposer.isAvailableAsync();
if (!isAvailable) {
Alert.alert('Email not available');
return;
}
const result = await MailComposer.composeAsync({
recipients: ['contact@atlantis.app'],
ccRecipients: ['copy@atlantis.app'],
bccRecipients: ['hidden@atlantis.app'],
subject: 'Message from Atlantis',
body: 'Hello! This is a message sent from the Atlantis app.',
isHtml: false,
attachments: ['/path/to/file.pdf'], // Optional attachments
});
// result.status: 'sent', 'saved', 'cancelled', 'undetermined'
return result;
}Communication service
const CommunicationService = {
async sendSMS(phone: string, message: string) {
if (await SMS.isAvailableAsync()) {
return SMS.sendSMSAsync([phone], message);
}
throw new Error('SMS not available');
},
async sendEmail(to: string, subject: string, body: string) {
if (await MailComposer.isAvailableAsync()) {
return MailComposer.composeAsync({
recipients: [to],
subject,
body,
});
}
throw new Error('Email not available');
},
async call(phone: string) {
const url = `tel:${phone}`;
if (await Linking.canOpenURL(url)) {
await Linking.openURL(url);
}
},
};Code for this lesson: App.tsx
import React from 'react';
import { View, Text, Button, StyleSheet, Alert } from 'react-native';
import * as SMS from 'expo-sms';
import * as MailComposer from 'expo-mail-composer';
import * as Linking from 'expo-linking';
// Communication service using native phone features
const CommunicationService = {
async sendSMS(phone: string, message: string) {
// TODO: check availability: if (await SMS.isAvailableAsync()) { ... }
// TODO: inside, send the SMS: return SMS.sendSMSAsync([phone], message);
// TODO: otherwise: throw new Error('SMS unavailable');
},
async sendEmail(to: string, subject: string, body: string) {
// TODO: check availability: if (await MailComposer.isAvailableAsync()) { ... }
// TODO: inside, send the email:
// return MailComposer.composeAsync({ recipients: [to], subject, body });
// TODO: otherwise: throw new Error('Email unavailable');
},
async call(phone: string) {
const url = `tel:${phone}`;
// TODO: check if the URL can be opened: if (await Linking.canOpenURL(url)) { ... }
// TODO: inside, open it: await Linking.openURL(url);
},
};
export default function App() {
return (
<View style={styles.screen}>
<Text style={styles.title}>Atlantis Communication</Text>
<Button
title="Send SMS"
onPress={() => CommunicationService.sendSMS('+48123456789', 'Hello from Atlantis!')}
/>
<Button
title="Send email"
onPress={() => CommunicationService.sendEmail('contact@atlantyda.app', 'Hi', 'Content')}
/>
<Button
title="Call"
onPress={() => CommunicationService.call('+48123456789')}
/>
</View>
);
}
const styles = StyleSheet.create({
screen: {
flex: 1,
justifyContent: 'center',
padding: 24,
gap: 12,
backgroundColor: '#1a5276',
},
title: {
fontSize: 22,
color: '#fff',
fontWeight: '600',
marginBottom: 16,
},
});Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. How do you send an SMS?
2. How do you check SMS availability?
Hands-on tasks in the game
- Click in order
Click the elements for sending email:
- Code editor
Complete the CommunicationService code.