email_util.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import 'dart:io';
  2. import 'package:archive/archive_io.dart';
  3. import 'package:email_validator/email_validator.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/services.dart';
  7. import 'package:flutter_email_sender/flutter_email_sender.dart';
  8. import 'package:logging/logging.dart';
  9. import 'package:open_mail_app/open_mail_app.dart';
  10. import 'package:package_info_plus/package_info_plus.dart';
  11. import 'package:path_provider/path_provider.dart';
  12. import 'package:photos/core/configuration.dart';
  13. import 'package:photos/core/error-reporting/super_logging.dart';
  14. import 'package:photos/ui/components/buttons/button_widget.dart';
  15. import 'package:photos/ui/components/dialog_widget.dart';
  16. import 'package:photos/ui/components/models/button_type.dart';
  17. import 'package:photos/ui/tools/debug/log_file_viewer.dart';
  18. import 'package:photos/utils/dialog_util.dart';
  19. import 'package:photos/utils/toast_util.dart';
  20. import 'package:share_plus/share_plus.dart';
  21. import 'package:url_launcher/url_launcher.dart';
  22. final Logger _logger = Logger('email_util');
  23. bool isValidEmail(String? email) {
  24. if (email == null) {
  25. return false;
  26. }
  27. return EmailValidator.validate(email);
  28. }
  29. Future<void> sendLogs(
  30. BuildContext context,
  31. String title,
  32. String toEmail, {
  33. Function? postShare,
  34. String? subject,
  35. String? body,
  36. }) async {
  37. showDialogWidget(
  38. context: context,
  39. title: "Report a bug",
  40. icon: Icons.bug_report_outlined,
  41. body:
  42. "This will send across logs to help us debug your issue. Please note that file names will be included to help track issues with specific files.",
  43. buttons: [
  44. ButtonWidget(
  45. isInAlert: true,
  46. buttonType: ButtonType.neutral,
  47. labelText: "Report a bug",
  48. buttonAction: ButtonAction.first,
  49. shouldSurfaceExecutionStates: false,
  50. onTap: () async {
  51. await _sendLogs(context, toEmail, subject, body);
  52. if (postShare != null) {
  53. postShare();
  54. }
  55. },
  56. ),
  57. //isInAlert is false here as we don't want to the dialog to dismiss
  58. //on pressing this button
  59. ButtonWidget(
  60. buttonType: ButtonType.secondary,
  61. labelText: "View logs",
  62. buttonAction: ButtonAction.second,
  63. onTap: () async {
  64. showDialog(
  65. context: context,
  66. builder: (BuildContext context) {
  67. return LogFileViewer(SuperLogging.logFile!);
  68. },
  69. barrierColor: Colors.black87,
  70. barrierDismissible: false,
  71. );
  72. },
  73. ),
  74. const ButtonWidget(
  75. isInAlert: true,
  76. buttonType: ButtonType.secondary,
  77. labelText: "Cancel",
  78. buttonAction: ButtonAction.cancel,
  79. ),
  80. ],
  81. );
  82. }
  83. Future<void> _sendLogs(
  84. BuildContext context,
  85. String toEmail,
  86. String? subject,
  87. String? body,
  88. ) async {
  89. final String zipFilePath = await getZippedLogsFile(context);
  90. final Email email = Email(
  91. recipients: [toEmail],
  92. subject: subject ?? '',
  93. body: body ?? '',
  94. attachmentPaths: [zipFilePath],
  95. isHTML: false,
  96. );
  97. try {
  98. await FlutterEmailSender.send(email);
  99. } catch (e, s) {
  100. _logger.severe('email sender failed', e, s);
  101. Navigator.of(context, rootNavigator: true).pop();
  102. await shareLogs(context, toEmail, zipFilePath);
  103. }
  104. }
  105. Future<String> getZippedLogsFile(BuildContext context) async {
  106. final dialog = createProgressDialog(context, "Preparing logs...");
  107. await dialog.show();
  108. final logsPath = (await getApplicationSupportDirectory()).path;
  109. final logsDirectory = Directory(logsPath + "/logs");
  110. final tempPath = (await getTemporaryDirectory()).path;
  111. final zipFilePath =
  112. tempPath + "/logs-${Configuration.instance.getUserID() ?? 0}.zip";
  113. final encoder = ZipFileEncoder();
  114. encoder.create(zipFilePath);
  115. encoder.addDirectory(logsDirectory);
  116. encoder.close();
  117. await dialog.hide();
  118. return zipFilePath;
  119. }
  120. Future<void> shareLogs(
  121. BuildContext context,
  122. String toEmail,
  123. String zipFilePath,
  124. ) async {
  125. final result = await showDialogWidget(
  126. context: context,
  127. title: "Email your logs",
  128. body: "Please send the logs to \n$toEmail",
  129. buttons: [
  130. ButtonWidget(
  131. buttonType: ButtonType.neutral,
  132. labelText: "Copy email address",
  133. isInAlert: true,
  134. buttonAction: ButtonAction.first,
  135. onTap: () async {
  136. await Clipboard.setData(ClipboardData(text: toEmail));
  137. },
  138. shouldShowSuccessConfirmation: true,
  139. ),
  140. const ButtonWidget(
  141. buttonType: ButtonType.neutral,
  142. labelText: "Export logs",
  143. isInAlert: true,
  144. buttonAction: ButtonAction.second,
  145. ),
  146. const ButtonWidget(
  147. buttonType: ButtonType.secondary,
  148. labelText: "Cancel",
  149. isInAlert: true,
  150. buttonAction: ButtonAction.cancel,
  151. ),
  152. ],
  153. );
  154. if (result?.action != null && result!.action == ButtonAction.second) {
  155. final Size size = MediaQuery.of(context).size;
  156. await Share.shareFiles(
  157. [zipFilePath],
  158. sharePositionOrigin: Rect.fromLTWH(0, 0, size.width, size.height / 2),
  159. );
  160. }
  161. }
  162. Future<void> sendEmail(
  163. BuildContext context, {
  164. required String to,
  165. String? subject,
  166. String? body,
  167. }) async {
  168. try {
  169. final String clientDebugInfo = await _clientInfo();
  170. final EmailContent emailContent = EmailContent(
  171. to: [
  172. to,
  173. ],
  174. subject: subject ?? '[Support]',
  175. body: (body ?? '') + clientDebugInfo,
  176. );
  177. if (Platform.isAndroid) {
  178. // Special handling due to issue in proton mail android client
  179. // https://github.com/ente-io/photos-app/pull/253
  180. final Uri params = Uri(
  181. scheme: 'mailto',
  182. path: to,
  183. query: 'subject=${emailContent.subject}&body=${emailContent.body}',
  184. );
  185. if (await canLaunchUrl(params)) {
  186. await launchUrl(params);
  187. } else {
  188. // this will trigger _showNoMailAppsDialog
  189. throw Exception('Could not launch ${params.toString()}');
  190. }
  191. } else {
  192. final OpenMailAppResult result =
  193. await OpenMailApp.composeNewEmailInMailApp(
  194. nativePickerTitle: 'Select emailContent app',
  195. emailContent: emailContent,
  196. );
  197. if (!result.didOpen && !result.canOpen) {
  198. _showNoMailAppsDialog(context, to);
  199. } else if (!result.didOpen && result.canOpen) {
  200. await showCupertinoModalPopup(
  201. context: context,
  202. builder: (_) => CupertinoActionSheet(
  203. title: Text("Select mail app \n $to"),
  204. actions: [
  205. for (var app in result.options)
  206. CupertinoActionSheetAction(
  207. child: Text(app.name),
  208. onPressed: () {
  209. final content = emailContent;
  210. OpenMailApp.composeNewEmailInSpecificMailApp(
  211. mailApp: app,
  212. emailContent: content,
  213. );
  214. Navigator.of(context, rootNavigator: true).pop();
  215. },
  216. ),
  217. ],
  218. cancelButton: CupertinoActionSheetAction(
  219. child: const Text("Cancel"),
  220. onPressed: () {
  221. Navigator.of(context, rootNavigator: true).pop();
  222. },
  223. ),
  224. ),
  225. );
  226. }
  227. }
  228. } catch (e) {
  229. _logger.severe("Failed to send emailContent to $to", e);
  230. _showNoMailAppsDialog(context, to);
  231. }
  232. }
  233. Future<String> _clientInfo() async {
  234. final packageInfo = await PackageInfo.fromPlatform();
  235. final String debugInfo =
  236. '\n\n\n\n ------------------- \nFollowing information can '
  237. 'help us in debugging if you are facing any issue '
  238. '\nRegistered email: ${Configuration.instance.getEmail()}'
  239. '\nClient: ${packageInfo.packageName}'
  240. '\nVersion : ${packageInfo.version}';
  241. return debugInfo;
  242. }
  243. void _showNoMailAppsDialog(BuildContext context, String toEmail) {
  244. showChoiceDialog(
  245. context,
  246. icon: Icons.email_outlined,
  247. title: 'Please email us at $toEmail',
  248. firstButtonLabel: "Copy email address",
  249. secondButtonLabel: "Dismiss",
  250. firstButtonOnTap: () async {
  251. await Clipboard.setData(ClipboardData(text: toEmail));
  252. showShortToast(context, 'Copied');
  253. },
  254. );
  255. }