TechBuzzPro

Flutter URL Launcher Email: Step-by-Step Guide

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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_launcher package added to your pubspec.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/await and BuildContext.

Step-by-Step

  1. Add the dependency. In your project folder, run flutter pub add url_launcher. This updates pubspec.yaml and fetches the package. If you prefer manual editing, add url_launcher: ^6.0.0 (or the current version) under dependencies and run flutter pub get.
  2. Import the package. At the top of the Dart file where you will trigger the email, add import 'package:url_launcher/url_launcher.dart';.
  3. Build the mailto URI. Use Uri with the scheme: 'mailto', path: 'someone@example.com', and a queryParameters map for subject and body. Example:
    final Uri emailLaunchUri = Uri(
      scheme: 'mailto',
      path: 'support@example.com',
      queryParameters: {
        'subject': 'App feedback',
        'body': 'Hello, I would like to report...',
      },
    );
    The Uri class handles URL-encoding of spaces and special characters, so you do not need to encode manually.
  4. Launch the URI. Call launchUrl(emailLaunchUri) inside an async function. Check the returned bool: if it is false, the launch failed and you should show a message. Example:
    Future<void> sendEmail() async {
      if (!await launchUrl(emailLaunchUri)) {
        throw Exception('Could not launch email app');
      }
    }
    Wrap the call in a try/catch to handle exceptions gracefully.
  5. Call it from a button. Attach sendEmail to an onPressed handler. Because it uses BuildContext indirectly through the platform channel, ensure the widget is still mounted before showing any snackbar or dialog after the await.
  6. Configure platform requirements. On Android 11+ (API 30+), add a <queries> block to android/app/src/main/AndroidManifest.xml so the app can resolve email handlers:
    <queries>
      <intent>
        <action android:name="android.intent.action.SENDTO" />
        <data android:scheme="mailto" />
      </intent>
    </queries>
    On iOS, no manifest change is required for mailto:, but if you later add tel: or sms: you must declare those schemes in Info.plist under LSApplicationQueriesSchemes. For mailto: specifically, iOS handles it without extra configuration.
  7. Test on a real device. Emulators often lack a configured email account, so the launch may return false or 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 of launchUrl(). The older launch function is deprecated; use launchUrl.
  • 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 queryParameters double-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 false return. If no email app is installed, launchUrl returns false; without a check the user sees nothing happen.

Tips

  • Keep the body short. Very long bodies can be truncated by some email clients.
  • Use Uri.encodeComponent only if you are building the URI string manually; with queryParameters it is unnecessary.
  • Provide a fallback: if launchUrl returns false, 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.