Flutter URL Launcher Email: Step-by-Step Guide
In this article
To send an email from a Flutter app using url_launcher, you open a mailto: URI with the launchUrl function. The device's default email app opens with the recipient, subject and body pre-filled, and the user taps Send. This is the standard, cross-platform way to hand off email composition without embedding a mail client or asking for SMTP credentials.
What You'll Need
- Flutter SDK installed and a project you can run on a device or emulator.
- The
url_launcherpackage added to yourpubspec.yaml(use the latest version shown on pub.dev). - An email app installed on the test device (Gmail, Apple Mail, Outlook, etc.).
- Basic familiarity with
async/awaitandBuildContext.
Step-by-Step
- Add the dependency. In your project folder, run
flutter pub add url_launcher. This updatespubspec.yamland fetches the package. If you prefer manual editing, addurl_launcher: ^6.0.0(or the current version) underdependenciesand runflutter pub get. - Import the package. At the top of the Dart file where you will trigger the email, add
import 'package:url_launcher/url_launcher.dart';. - Build the mailto URI. Use
Uriwith thescheme: 'mailto',path: 'someone@example.com', and aqueryParametersmap forsubjectandbody. Example:
Thefinal Uri emailLaunchUri = Uri( scheme: 'mailto', path: 'support@example.com', queryParameters: { 'subject': 'App feedback', 'body': 'Hello, I would like to report...', }, );Uriclass handles URL-encoding of spaces and special characters, so you do not need to encode manually. - Launch the URI. Call
launchUrl(emailLaunchUri)inside an async function. Check the returnedbool: if it isfalse, the launch failed and you should show a message. Example:
Wrap the call in aFuture<void> sendEmail() async { if (!await launchUrl(emailLaunchUri)) { throw Exception('Could not launch email app'); } }try/catchto handle exceptions gracefully. - Call it from a button. Attach
sendEmailto anonPressedhandler. Because it usesBuildContextindirectly through the platform channel, ensure the widget is still mounted before showing any snackbar or dialog after the await. - Configure platform requirements. On Android 11+ (API 30+), add a
<queries>block toandroid/app/src/main/AndroidManifest.xmlso the app can resolve email handlers:
On iOS, no manifest change is required for<queries> <intent> <action android:name="android.intent.action.SENDTO" /> <data android:scheme="mailto" /> </intent> </queries>mailto:, but if you later addtel:orsms:you must declare those schemes inInfo.plistunderLSApplicationQueriesSchemes. Formailto:specifically, iOS handles it without extra configuration. - Test on a real device. Emulators often lack a configured email account, so the launch may return
falseor open nothing. Install a mail app, sign in, and test again. Also test with no mail app installed to confirm your error message appears.
Common Mistakes
- Using
launch()instead oflaunchUrl(). The olderlaunchfunction is deprecated; uselaunchUrl. - Forgetting
await. Without it, you cannot check the result and errors are swallowed. - Manually encoding the subject or body. Passing a pre-encoded string to
queryParametersdouble-encodes it, producing garbled text in the email app. - Missing the Android
<queries>block. On Android 11+, the launch silently fails or throws because the package visibility rules block the intent resolution. - Assuming
mailto:works on all platforms. It works on Android, iOS, macOS, Windows and Linux, but not on Flutter web in all browsers; on web, the browser's mail handler takes over and behaviour varies. - Not handling the
falsereturn. If no email app is installed,launchUrlreturnsfalse; without a check the user sees nothing happen.
Tips
- Keep the
bodyshort. Very long bodies can be truncated by some email clients. - Use
Uri.encodeComponentonly if you are building the URI string manually; withqueryParametersit is unnecessary. - Provide a fallback: if
launchUrlreturnsfalse, show a dialog with the email address so the user can copy it. - For multiple recipients, pass a comma-separated list in
path(e.g.,'a@example.com,b@example.com') — most clients accept this. - Test on both iOS and Android; the same
mailto:URI behaves slightly differently in Gmail vs Apple Mail.
Troubleshooting
If nothing opens, first confirm an email app is installed and signed in. On Android, check the <queries> block is present and that you rebuilt the app after editing the manifest. On iOS, test on a real device — the simulator may not have Mail configured. If the app crashes, ensure you are not calling launchUrl after the widget is disposed; guard with if (!mounted) return;. If the subject or body appears garbled, remove any manual encoding and let Uri handle it. If launchUrl returns false on Android 11+, the <queries> entry is the most likely cause. Finally, run flutter clean and rebuild to rule out stale build artifacts.
Frequently Asked Questions
Can I send an email directly without opening the mail app?
Not with url_launcher. It only hands off to the device's email client. To send silently you need a backend service or an SMTP package, which requires server credentials and is not recommended for client-side apps.
Does url_launcher work on Flutter web?
It can open a mailto: link, but the result depends on the browser and the user's configured mail handler. Many browsers open a blank tab or do nothing if no handler is set, so always check the return value and provide a fallback.
Why does the email app open with an empty body?
Some email clients ignore the body parameter or truncate it. Keep the body plain text and avoid special characters. Also ensure you are not double-encoding the string.
Do I need permissions in AndroidManifest.xml?
No runtime permissions are needed for mailto:. You only need the <queries> block for Android 11+ package visibility. No INTERNET permission is required because the email app handles the network request.
Can I attach a file?
mailto: does not support attachments. You would need to use a platform-specific share sheet or a third-party plugin that supports attachments, but those are outside the scope of url_launcher.
What happens if the user has no email app?
launchUrl returns false. Your code should catch that and show a message with the email address so the user can copy it manually.