Adriana Giancarla Pope
Adriana Giancarla Pope

Reputation: 143

How to get url launcher to send an email?

I would like for someone to be able to click on a button "Contact me" and for this button to open up the email client with my email in there. I am currently just using the dummy data they give me on the package url_launcher 5.4.11.

This work properly when my url is 'https://flutter.dev'; but doesn't work when I try to enter a custom Supported URL scheme for sending an email.

The error I am getting is "Unhandled Exception: Could not launch [email protected]?subject=Terra%20Tarot%20Question%20or%20Feedback #0 _launchURL (package:tarotcards/screens/support.dart:11:5) "

I read somewhere that it's not possible to call mail app on the iOS emulator. Is this true?

The issue is with my url in the _launchURL() method. If you can help me fix this, that would be much appreciated. Thanks so much!

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

_launchURL() async {
  const url = 'mailto:[email protected]?subject=News&body=New%20plugin';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

class Support extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Image.asset('images/skull.png'),
            Container(
              padding: EdgeInsets.fromLTRB(0, 20.0, 0, 10.0),
              child: Text(
                'Need some help?',
                style: TextStyle(
                    color: Colors.white,
                    fontFamily: 'Opensans',
                    fontSize: 25.0,
                    fontWeight: FontWeight.bold),
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 0.0),
              child: Text(
                'Experiencing bugs? Want to request a feature?'
                'Feel free to get in touch with me.'
                '🔮 ~Adriana',
                style: TextStyle(
                    color: Colors.white,
                    letterSpacing: 0.5,
                    height: 1.5,
                    fontFamily: 'Opensans',
                    fontSize: 16.0,
                    fontWeight: FontWeight.normal),
              ),
            ),
            SizedBox(
              height: 40.0,
            ),
            ButtonTheme(
              //TODO:// make button #1 work!
              minWidth: 300.0,
              child: RaisedButton(
                child: Text('CONTACT ME'),
                padding: EdgeInsets.all(10.0),
                textColor: Colors.white,
                color: Color(0XFFeb1555),
                onPressed: _launchURL,
              ),
            ),
            SizedBox(
              height: 40.0,
            ),
            ButtonTheme(
              //TODO:// make button #2 work!
              minWidth: 300.0,
              child: RaisedButton(
                child: Text('FREQUENTLY ASKED QUESTIONS'),
                padding: EdgeInsets.all(10.0),
                textColor: Colors.black,
                color: Colors.white,
                onPressed: () {
                  print('second button tapped');
                },
              ),
            ),
            SizedBox(
              height: 150.0,
            ),
            FlatButton(
              child: Text(
                'Terms of Service & Privacy Policy →',
                style: TextStyle(
                  color: Color(0XFFeb1555),
                ),
              ),
              onPressed: () {
                print('privacy policy tapped');
              },
            ),
          ],
        ),
      ),
    );
  }
}

Upvotes: 4

Views: 6003

Answers (2)

StuckInPhDNoMore
StuckInPhDNoMore

Reputation: 2689

I would like to add that for API >= 30 the following is needed to be added to your AndroidManifest.xml file for email sending to work.

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="*/*" />
    </intent>
</queries>

source

Upvotes: 3

Ahmed Erabti
Ahmed Erabti

Reputation: 336

I couldn't reproduce the error on my machine for real device!

Are you using the simulator? Remember that the simulator cannot handle mailto because the mail application is not available.

Try:

_launchURL() async {
  final url =
      Uri.encodeFull('mailto:[email protected]?subject=News&body=New plugin');
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

You can try this package instead of on a real device: flutter_email_sender

Upvotes: 8

Related Questions