In this post, we are going to show you how to open the default Email Launcher in Flutter. Along with the receiver’s email address, subject, and body text in Flutter. This example will help you to make the “email us” button on your contact page. After that which will make it very easy for users to email you.
![Email Launcher in Flutter](https://mobikul.com/wp-content/uploads/2023/12/email-launcher-in-flutter.png)
Introduction
The url_launcher package in Flutter provides an easy way to launch URLs from your app. In other words, it can be used to open web pages, send emails, make phone calls, send text messages, and more. With just a few lines of code, After that you can easily launch URLs in Flutter. In this case, the “url_launcher” plugin can be used to launch the mail in a mobile application.
The steps for adding the plugin to the Flutter app
- Open “pubspec.yaml” file from the project folder.
- In the pubspec.yaml file, type “url_launcher:” under dependencies.
- After that now click “Pub Get” button at the top of the application (Android Studio).
- The console’s “Process finished with exit code 0” indicates that the dependency was successfully added.
- Now import the plugin or package by adding the “import ‘package:url_launcher/url_launcher.dart’;” code to the top of the “main.dart” file.
1 2 3 4 |
dependencies: flutter: sdk: flutter url_launcher: |
You may also check our Flutter app development services
Email Launcher in Flutter
Let’s now construct a function that will be triggered each time a user clicks a button associated with a specific Mail ID, allowing them to send a message to that ID.
1 2 3 4 5 6 7 8 |
_sendingMails() async { var url = Uri.parse("mailto:testcode@webkul.in"); if (await canLaunchUrl(url)) { await launchUrl(url); } else { throw 'Could not launch $url'; } } |
- Here, the function is called “_sendingMails,” and in order for it to return a promise, it is declared as “async.”
- The necessary Mail ID is assigned as a string to the “url” variable. The app is instructed to launch the phone’s default mailing app by the syntax “mailto:”. After that to enter the Mail ID specified in the “url” variable in the “To” section. To ensure that the variable is never altered, it is declared as a “const.”
- The URL is only launched if it is possible to do so, in which case it is called by passing the URL variable. Similarly an argument to the launch() function.
- Else, it will throw/print a text with the url value, as an error message.
Calling the Email Launcher in Flutter:
This function is called whenever needed in code, by calling the name of the functions as such. The examples are as follows:
1 2 3 4 5 6 7 8 9 10 11 |
ElevatedButton( onPressed: _sendingMails(), style: ButtonStyle( padding: MaterialStateProperty.all(constEdgeInsets.all(5.0)), textStyle: MaterialStateProperty.all( constTextStyle(color: Colors.black), ), ), child: constText('Send Mail'), ), // ElevatedButton |
Conclusion:
The URL Launcher plugin is incredibly handy when you need to interface with other applications from your application. We will learn what the URL Launcher plugin’s use cases are and how to implement the plugin to fit each use case in conclusion.
For more understanding please can go through this Link.
For more interesting blogs check here.
Thanks for reading this article.