email_util.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import 'dart:io';
  2. import 'package:archive/archive_io.dart';
  3. import 'package:email_validator/email_validator.dart';
  4. import 'package:ente_auth/core/configuration.dart';
  5. import 'package:ente_auth/core/logging/super_logging.dart';
  6. import 'package:ente_auth/ente_theme_data.dart';
  7. import 'package:ente_auth/l10n/l10n.dart';
  8. import 'package:ente_auth/ui/components/buttons/button_widget.dart';
  9. import 'package:ente_auth/ui/components/dialog_widget.dart';
  10. import 'package:ente_auth/ui/components/models/button_type.dart';
  11. import 'package:ente_auth/ui/tools/debug/log_file_viewer.dart';
  12. // import 'package:ente_auth/ui/tools/debug/log_file_viewer.dart';
  13. import 'package:ente_auth/utils/dialog_util.dart';
  14. import 'package:ente_auth/utils/toast_util.dart';
  15. import 'package:flutter/material.dart';
  16. import 'package:flutter/services.dart';
  17. import 'package:flutter_email_sender/flutter_email_sender.dart';
  18. import 'package:logging/logging.dart';
  19. // import 'package:open_mail_app/open_mail_app.dart';
  20. import 'package:package_info_plus/package_info_plus.dart';
  21. import 'package:path_provider/path_provider.dart';
  22. import 'package:share_plus/share_plus.dart';
  23. import 'package:url_launcher/url_launcher.dart';
  24. final Logger _logger = Logger('email_util');
  25. bool isValidEmail(String email) {
  26. return EmailValidator.validate(email);
  27. }
  28. Future<void> sendLogs(
  29. BuildContext context,
  30. String title,
  31. String toEmail, {
  32. Function? postShare,
  33. String? subject,
  34. String? body,
  35. }) async {
  36. final l10n = context.l10n;
  37. final List<Widget> actions = [
  38. TextButton(
  39. child: Row(
  40. mainAxisAlignment: MainAxisAlignment.start,
  41. children: [
  42. Icon(
  43. Icons.feed_outlined,
  44. color: Theme.of(context).iconTheme.color?.withOpacity(0.85),
  45. ),
  46. const Padding(padding: EdgeInsets.all(4)),
  47. Text(
  48. l10n.viewLogsAction,
  49. style: TextStyle(
  50. color: Theme.of(context)
  51. .colorScheme
  52. .defaultTextColor
  53. .withOpacity(0.85),
  54. ),
  55. ),
  56. ],
  57. ),
  58. onPressed: () async {
  59. showDialog(
  60. context: context,
  61. builder: (BuildContext context) {
  62. return LogFileViewer(SuperLogging.logFile!);
  63. },
  64. barrierColor: Colors.black87,
  65. barrierDismissible: false,
  66. );
  67. },
  68. ),
  69. TextButton(
  70. child: Text(
  71. title,
  72. style: TextStyle(
  73. color: Theme.of(context).colorScheme.alternativeColor,
  74. ),
  75. ),
  76. onPressed: () async {
  77. Navigator.of(context, rootNavigator: true).pop('dialog');
  78. await _sendLogs(context, toEmail, subject, body);
  79. if (postShare != null) {
  80. postShare();
  81. }
  82. },
  83. ),
  84. ];
  85. final List<Widget> content = [];
  86. content.addAll(
  87. [
  88. Text(
  89. l10n.sendLogsDescription,
  90. style: const TextStyle(
  91. height: 1.5,
  92. fontSize: 16,
  93. ),
  94. ),
  95. const Padding(padding: EdgeInsets.all(12)),
  96. Row(
  97. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  98. children: actions,
  99. ),
  100. ],
  101. );
  102. final confirmation = AlertDialog(
  103. title: Text(
  104. title,
  105. style: const TextStyle(
  106. fontSize: 18,
  107. ),
  108. ),
  109. content: SingleChildScrollView(
  110. child: ListBody(
  111. children: content,
  112. ),
  113. ),
  114. );
  115. showDialog(
  116. context: context,
  117. builder: (_) {
  118. return confirmation;
  119. },
  120. );
  121. }
  122. Future<void> _sendLogs(
  123. BuildContext context,
  124. String toEmail,
  125. String? subject,
  126. String? body,
  127. ) async {
  128. final String zipFilePath = await getZippedLogsFile(context);
  129. final Email email = Email(
  130. recipients: [toEmail],
  131. subject: subject ?? '',
  132. body: body ?? '',
  133. attachmentPaths: [zipFilePath],
  134. isHTML: false,
  135. );
  136. try {
  137. await FlutterEmailSender.send(email);
  138. } catch (e, s) {
  139. _logger.severe('email sender failed', e, s);
  140. await shareLogs(context, toEmail, zipFilePath);
  141. }
  142. }
  143. Future<String> getZippedLogsFile(BuildContext context) async {
  144. final l10n = context.l10n;
  145. final dialog = createProgressDialog(context, l10n.preparingLogsTitle);
  146. await dialog.show();
  147. final logsPath = (await getApplicationSupportDirectory()).path;
  148. final logsDirectory = Directory(logsPath + "/logs");
  149. final tempPath = (await getTemporaryDirectory()).path;
  150. final zipFilePath =
  151. tempPath + "/logs-${Configuration.instance.getUserID() ?? 0}.zip";
  152. final encoder = ZipFileEncoder();
  153. encoder.create(zipFilePath);
  154. encoder.addDirectory(logsDirectory);
  155. encoder.close();
  156. await dialog.hide();
  157. return zipFilePath;
  158. }
  159. Future<void> shareLogs(
  160. BuildContext context,
  161. String toEmail,
  162. String zipFilePath,
  163. ) async {
  164. final result = await showDialogWidget(
  165. context: context,
  166. title: context.l10n.emailYourLogs,
  167. body: context.l10n.pleaseSendTheLogsTo(toEmail),
  168. buttons: [
  169. ButtonWidget(
  170. buttonType: ButtonType.neutral,
  171. labelText: context.l10n.copyEmailAddress,
  172. isInAlert: true,
  173. buttonAction: ButtonAction.first,
  174. onTap: () async {
  175. await Clipboard.setData(ClipboardData(text: toEmail));
  176. },
  177. shouldShowSuccessConfirmation: true,
  178. ),
  179. ButtonWidget(
  180. buttonType: ButtonType.neutral,
  181. labelText: context.l10n.exportLogs,
  182. isInAlert: true,
  183. buttonAction: ButtonAction.second,
  184. ),
  185. ButtonWidget(
  186. buttonType: ButtonType.secondary,
  187. labelText: context.l10n.cancel,
  188. isInAlert: true,
  189. buttonAction: ButtonAction.cancel,
  190. ),
  191. ],
  192. );
  193. if (result?.action != null && result!.action == ButtonAction.second) {
  194. final Size size = MediaQuery.of(context).size;
  195. await Share.shareFiles(
  196. [zipFilePath],
  197. sharePositionOrigin: Rect.fromLTWH(0, 0, size.width, size.height / 2),
  198. );
  199. }
  200. }
  201. Future<void> sendEmail(
  202. BuildContext context, {
  203. required String to,
  204. String? subject,
  205. String? body,
  206. }) async {
  207. try {
  208. final String clientDebugInfo = await _clientInfo();
  209. final String _subject = subject ?? '[Support]';
  210. final String _body = (body ?? '') + clientDebugInfo;
  211. // final EmailContent email = EmailContent(
  212. // to: [
  213. // to,
  214. // ],
  215. // subject: subject ?? '[Support]',
  216. // body: (body ?? '') + clientDebugInfo,
  217. // );
  218. if (Platform.isAndroid) {
  219. // Special handling due to issue in proton mail android client
  220. // https://github.com/ente-io/frame/pull/253
  221. final Uri params = Uri(
  222. scheme: 'mailto',
  223. path: to,
  224. query: 'subject=$_subject&body=$_body',
  225. );
  226. if (await canLaunchUrl(params)) {
  227. await launchUrl(params);
  228. } else {
  229. // this will trigger _showNoMailAppsDialog
  230. throw Exception('Could not launch ${params.toString()}');
  231. }
  232. } else {
  233. _showNoMailAppsDialog(context, to);
  234. }
  235. } catch (e) {
  236. _logger.severe("Failed to send email to $to", e);
  237. _showNoMailAppsDialog(context, to);
  238. }
  239. }
  240. Future<String> _clientInfo() async {
  241. final packageInfo = await PackageInfo.fromPlatform();
  242. final String debugInfo =
  243. '\n\n\n\n ------------------- \nFollowing information can '
  244. 'help us in debugging if you are facing any issue '
  245. '\nRegistered email: ${Configuration.instance.getEmail()}'
  246. '\nClient: ${packageInfo.packageName}'
  247. '\nVersion : ${packageInfo.version}';
  248. return debugInfo;
  249. }
  250. void _showNoMailAppsDialog(BuildContext context, String toEmail) {
  251. final l10n = context.l10n;
  252. showDialog(
  253. context: context,
  254. builder: (context) {
  255. return AlertDialog(
  256. title: Text(l10n.emailUsMessage(toEmail)),
  257. actions: <Widget>[
  258. TextButton(
  259. child: Text(l10n.copyEmailAction),
  260. onPressed: () async {
  261. await Clipboard.setData(ClipboardData(text: toEmail));
  262. showShortToast(context, l10n.copied);
  263. },
  264. ),
  265. TextButton(
  266. child: Text(l10n.ok),
  267. onPressed: () {
  268. Navigator.pop(context);
  269. },
  270. )
  271. ],
  272. );
  273. },
  274. );
  275. }