flutter send email with url_launcher uri
2,280
Solution 1
Try using queryParameters
in Uri
. You can achieve this in below shown way:
void launchEmailSubmission() async {
final Uri params = Uri(
scheme: 'mailto',
path: '[email protected]',
queryParameters: {
'subject': 'Default Subject',
'body': 'Default body'
}
);
String url = params.toString();
if (await canLaunch(url)) {
await launch(url);
} else {
print('Could not launch $url');
}
}
It will open will default body and subject.
Solution 2
As @tsitixe pointed out you can use Piyushs answer and change queryParameters to query like this to avoid "+" symbols between the words in your E-Mail:
void launchEmailSubmission() async {
final Uri params = Uri(
scheme: 'mailto',
path: '[email protected]',
query: 'subject=Default Subject&body=Default body'
);
String url = params.toString();
if (await canLaunch(url)) {
await launch(url);
} else {
print('Could not launch $url');
}
}
Solution 3
Try this!
void _launchURL() async {
final Uri params = Uri(
scheme: 'mailto',
path: '[email protected]',
);
String url = params.toString();
if (await canLaunch(url)) {
await launch(url);
} else {
print( 'Could not launch $url');
}
}
Comments
-
tsitixe over 1 year
I'm using url_launcher to send email with system email in my app. I'm using code below and this guy is doing so well.
void launchEmailSubmission() async { final Uri params = Uri( scheme: 'mailto', path: '[email protected]', ); String url = params.toString(); if (await canLaunch(url)) { await launch(url); } else { print('Could not launch $url'); } }
But now I want to give it 'default' subject and hintText inside the mail body box(if hintText not possible, then normal text).
Is there any way to do this?
-
tsitixe over 3 yearsThanks! It is working super well but one thing left unsolved as expected.. The space between 'Default' and 'Subject' or 'Default' and 'body' are translated into '+' sign when the email app appears. Is there any cool way to make this '+' sign get back to normal space in the email app out of Flutter code?!
-
tsitixe over 3 yearsbro.. this one is exactly the original SO code i've used before. I recommend the code above by @Maurya to make additional 'default subject and body' options!
-
evgen shein over 3 years@tsitixe Replace
queryParameters: { 'subject': 'Default Subject', 'body': 'Default body'}
withquery: 'subject=Default Subject&body=Default body'
to remove '+' -
martinseal1987 over 2 yearsi believe an equals sign in your 'Default Subject' or 'Default Body' will also break this