Open the default email app on iPhone using Flutter
In this article
Yes, you can open the iPhone's default Mail app from a Flutter project by using the url_launcher package and a “mailto:” URL scheme.
What You'll Need
- Flutter SDK (latest stable version)
- Xcode installed on macOS (required for iOS builds)
- An iPhone or iOS simulator running iOS 13 or later
- The
url_launcherpackage added topubspec.yaml - Basic knowledge of Dart and Flutter widget structure
Step-by-Step
-
Create or open a Flutter project. Open a terminal and run
flutter create my_email_appor navigate to an existing project folder. -
Add the
url_launcherdependency. Openpubspec.yamland underdependenciesadd:
Then runurl_launcher: ^6.1.7flutter pub getto fetch the package. -
Import the package in the Dart file where you want to trigger the email client.
import 'package:url_launcher/url_launcher.dart'; -
Write a helper function that builds a proper
mailto:URI. The URI can include optional fields such assubject,body, andcc. Example:FutureopenDefaultMail({ required String toEmail, String? subject, String? body, }) async { final Uri emailLaunchUri = Uri( scheme: 'mailto', path: toEmail, queryParameters: { if (subject != null) 'subject': subject, if (body != null) 'body': body, }, ); if (!await launchUrl(emailLaunchUri)) { // If launchUrl returns false, the device could not handle the URL. throw 'Could not launch email client'; } } -
Call the function from a UI element. For instance, attach it to a button:
ElevatedButton( onPressed: () => openDefaultMail( toEmail: 'example@domain.com', subject: 'Hello from Flutter', body: 'This email was opened via the default Mail app.', ), child: Text('Send Email'), ); -
Run the app on an iPhone or iOS simulator. Use
flutter runor launch from VS Code/Android Studio. Tap the button – the system will switch to the native Mail app with the fields pre‑filled. -
Handle errors gracefully. Wrap the call in a
try/catchblock and show aSnackBaror dialog if the launch fails. This covers cases where the user has removed the Mail app or disabled it.
Common Mistakes
- Using
launch()instead oflaunchUrl(). The newer API provides better URI handling and returns a boolean indicating success. - Omitting the
mailto:scheme. Without the scheme iOS treats the string as a web URL and opens Safari instead of Mail. - Including spaces or special characters in the query parameters without proper encoding. Using the
Uriconstructor automatically percent‑encodes values. - Testing on an Android emulator only. The default Mail app is iOS‑specific; on Android you would need a different approach (e.g., intent filters).
- Assuming the function works when the device has no email accounts configured. iOS still opens the Mail app, but the compose screen will show an error after the user tries to send.
Tips
- Check
await canLaunchUrl(emailLaunchUri)before callinglaunchUrlto provide a clearer error message if the scheme is unsupported. - Keep the subject and body short; very long bodies may be truncated by the system UI.
- If you need to support attachments, the native Mail app cannot be launched directly with files via a URL scheme. You would need to use a plugin such as
flutter_email_senderinstead. - Use
awaitonlaunchUrlso the UI does not continue before the system switch completes. - Test on a physical device as the simulator sometimes shows a placeholder Mail app that behaves slightly differently.
Troubleshooting
Launch fails and returns false: First, confirm that the device actually has the Mail app installed (it is built‑in on iOS, but can be removed on newer iOS versions). Open Settings > General > iPhone Storage and verify Mail appears. If it is missing, instruct the user to reinstall it from the App Store.
App crashes with an uncaught exception: This usually means the Uri was malformed. Print the generated URI to the console and ensure it looks like mailto:example@domain.com?subject=Hello&body=Test. Avoid manually concatenating strings; always use the Uri constructor.
No email client opens, but no error is thrown: Some iOS configurations disable third‑party URL schemes for privacy. Go to Settings > Privacy > Tracking and ensure your app is allowed to open external links. Also, verify that Info.plist includes the LSApplicationQueriesSchemes entry for mailto if you are using canLaunchUrl.
The compose screen appears but the fields are empty: Ensure you are passing the query parameters correctly. The Uri query map must use string values; passing null will omit the parameter entirely.
After returning from Mail, the Flutter UI is frozen: This can happen if you called launchUrl without await inside an async callback that also updates state. Always mark the onPressed handler as async and await the launch before calling setState or navigating.
Frequently Asked Questions
Can I open the default Mail app without pre‑filling any fields?
Yes. Use a simple mailto: URI without query parameters, e.g. Uri(scheme: 'mailto'). iOS will open the compose screen with empty fields.
What happens on iOS versions that no longer include the Mail app?
If the user has removed Mail, the launchUrl call returns false. Your app should catch this and optionally fall back to a web‑based mailto link or display a friendly message.
Is it possible to add attachments through the mailto: scheme?
No. The mailto: URL scheme does not support file attachments. For attachments you need a dedicated plugin that uses native APIs, such as flutter_email_sender or a platform channel.
Do I need to request any permissions to use url_launcher?
No additional permissions are required for the mailto: scheme. The package works with the default iOS security model.
Can I open other email clients like Gmail or Outlook instead of the default Mail app?
Yes, but you must use the specific URL scheme for each client (e.g., googlegmail:// for Gmail). You would first check canLaunchUrl for that scheme and fall back to mailto: if it is unavailable.
Why does the compose screen sometimes show a warning about ‘No email account configured’?
The warning is generated by iOS when the user has not added any email account to the Mail app. Your Flutter code cannot bypass this; the user must configure an account in Settings > Mail > Accounts.