I can’t seem to get flutter to send an email
this is from my main.dart
Future<bool> isRunningOnEmulator() async {
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
final AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
return androidInfo.isPhysicalDevice == false;
} else if (Platform.isIOS) {
final IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
return iosInfo.isPhysicalDevice == false;
}
return false;
}
Future<void> _sendEmail() async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
String site = prefs.getString('site') ?? '';
String inspectedBy = prefs.getString('inspectedBy') ?? '';
String date = prefs.getString('date') ?? '';
String comments = prefs.getString('comments') ?? '';
String furtherAction = prefs.getString('furtherAction') ?? '';
String nextInspection = prefs.getString('nextInspection') ?? '';
String partsRequired = prefs.getString('partsRequired') ?? '';
String emailBody = '''
Track Maintenance Monthly Inspection Record
Inspected By: $inspectedBy
Date: $date
Inspection Details:
''';
for (String question in questions) {
emailBody += 'n$question: ${prefs.getString(question) ?? 'No'}';
emailBody +=
'nComments: ${prefs.getString('$question-comment') ?? ''}n';
}
emailBody += '''
Comments: $comments
Further Action: $furtherAction
Next Inspection: $nextInspection
Parts Required: $partsRequired
''';
String emailRecipient = widget.determineRecipient(site);
final Email email = Email(
body: emailBody,
subject: 'Track Maintenance Monthly Inspection Record',
recipients: [emailRecipient, '[email protected]'],
attachmentPaths: _images.map((file) => file.path).toList(),
isHTML: false,
);
print('Sending email...');
if (await isRunningOnEmulator()) {
throw Exception("Email functionality is not available on emulators.");
}
await FlutterEmailSender.send(email);
print('Email sent successfully.');
Navigator.push(
context, MaterialPageRoute(builder: (context) => ConfirmationPage()));
} catch (e) {
print('Failed to send email: $e');
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text("Error"),
content: Text(e.toString().contains("not_available") ||
e.toString().contains(
"Email functionality is not available on emulators.")
? "No email clients installed or email functionality is not available on emulators. Please ensure an email client is installed and configured."
: "Failed to send email. Please try again later."),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(ctx).pop();
},
child: Text("OK"),
),
],
),
);
}
}
This is from androidmanifest.xml (appsrcmain)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mlp_app">
<!-- Required permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:label="mlp_app"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Email intent filter to handle sending emails -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="message/rfc822"/>
<data android:mimeType="application/pdf"/>
<data android:mimeType="image/*"/>
<data android:mimeType="*/*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="message/rfc822"/>
<data android:mimeType="application/pdf"/>
<data android:mimeType="image/*"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
</manifest>
No matter what i try i cant seem to get it to submit it just throws back “No email clients installed or email functionality is not available on emulators. Please ensure an email client is installed and configured” and i am using this on a android based phone
New contributor
Jack Prothero is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.