Add mailer listener
This commit is contained in:
parent
ab32d8f471
commit
1351130f83
4 changed files with 184 additions and 0 deletions
53
lib/utils/mailer.dart
Normal file
53
lib/utils/mailer.dart
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
import 'package:desktopapp/utils/logger.dart';
|
||||||
|
import 'package:desktopapp/utils/requests.dart';
|
||||||
|
import 'package:enough_mail/enough_mail.dart';
|
||||||
|
|
||||||
|
class Mailer {
|
||||||
|
/// High level mail API example
|
||||||
|
static Future<void> mailExample() async {
|
||||||
|
const email = 'trexricher1997@gmail.com';
|
||||||
|
const password = 'rugnxbafhcwsykzt';
|
||||||
|
Logger.log(LoggerType.info, Mailer, 'Discovering settings for $email');
|
||||||
|
final config = await Discover.discover(email);
|
||||||
|
if (config == null) {
|
||||||
|
Logger.log(LoggerType.info, Mailer,
|
||||||
|
'Unable to autodiscover settings for $email');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Logger.log(LoggerType.info, Mailer, 'Settings for $email : $config');
|
||||||
|
final account = MailAccount.fromDiscoveredSettings(
|
||||||
|
'my account', email, password, config);
|
||||||
|
final mailClient = MailClient(account, isLogEnabled: true);
|
||||||
|
try {
|
||||||
|
await mailClient.connect();
|
||||||
|
Logger.log(LoggerType.info, Mailer, 'Connected to $email');
|
||||||
|
await mailClient.selectInbox();
|
||||||
|
mailClient.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
|
||||||
|
if (event.eventType == ImapEventType.exists) {
|
||||||
|
final evt = event as ImapMessagesExistEvent;
|
||||||
|
if (evt.newMessagesExists <= evt.oldMessagesExists) return;
|
||||||
|
final sequence = MessageSequence();
|
||||||
|
if (evt.newMessagesExists - evt.oldMessagesExists > 1) {
|
||||||
|
sequence.addRange(evt.oldMessagesExists, evt.newMessagesExists);
|
||||||
|
} else {
|
||||||
|
sequence.add(evt.newMessagesExists);
|
||||||
|
}
|
||||||
|
final messages = await mailClient.fetchMessageSequence(sequence,
|
||||||
|
fetchPreference: FetchPreference.full);
|
||||||
|
for (final message in messages) {
|
||||||
|
var mailEvt = MailLoadEvent(message, mailClient);
|
||||||
|
var content = mailEvt.message.decodeSubject();
|
||||||
|
var mess = mailEvt.message.decodeTextPlainPart();
|
||||||
|
Logger.log(LoggerType.info, Mailer, 'New message $content : $mess');
|
||||||
|
Requests.sendAlert('*$content* $mess');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Logger.log(LoggerType.info, Mailer, 'Start polling mail for $email');
|
||||||
|
await mailClient.startPolling(const Duration(seconds: 30));
|
||||||
|
Logger.log(LoggerType.info, Mailer, 'Finish polling mail for $email');
|
||||||
|
} on MailException catch (e) {
|
||||||
|
Logger.log(LoggerType.error, Mailer, 'High level API failed with $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import 'package:desktopapp/classes/routes.dart';
|
import 'package:desktopapp/classes/routes.dart';
|
||||||
|
import 'package:desktopapp/utils/mailer.dart';
|
||||||
import 'package:desktopapp/utils/requests.dart';
|
import 'package:desktopapp/utils/requests.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
@ -44,6 +45,9 @@ class HomePage extends StatelessWidget {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: const Text('Envoyer une alerte de test')),
|
child: const Text('Envoyer une alerte de test')),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Mailer.mailExample(),
|
||||||
|
child: const Text('Test email')),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
126
pubspec.lock
126
pubspec.lock
|
@ -1,6 +1,20 @@
|
||||||
# Generated by pub
|
# Generated by pub
|
||||||
# See https://dart.dev/tools/pub/glossary#lockfile
|
# See https://dart.dev/tools/pub/glossary#lockfile
|
||||||
packages:
|
packages:
|
||||||
|
args:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: args
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "2.3.0"
|
||||||
|
asn1lib:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: asn1lib
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.1.0"
|
||||||
async:
|
async:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -8,6 +22,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.8.2"
|
version: "2.8.2"
|
||||||
|
basic_utils:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: basic_utils
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "3.9.4"
|
||||||
bloc:
|
bloc:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -50,6 +71,20 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.15.0"
|
version: "1.15.0"
|
||||||
|
convert:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: convert
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "3.0.1"
|
||||||
|
crypto:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: crypto
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "3.0.1"
|
||||||
cupertino_icons:
|
cupertino_icons:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
@ -64,6 +99,41 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.0.6"
|
version: "4.0.6"
|
||||||
|
encrypt:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: encrypt
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "5.0.1"
|
||||||
|
enough_convert:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: enough_convert
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.4.0"
|
||||||
|
enough_mail:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: enough_mail
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.3.6"
|
||||||
|
enough_serialization:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: enough_serialization
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.4.0"
|
||||||
|
event_bus:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: event_bus
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.0"
|
||||||
fake_async:
|
fake_async:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -121,6 +191,13 @@ packages:
|
||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
http:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: http
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.13.4"
|
||||||
http_parser:
|
http_parser:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -128,6 +205,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.0.0"
|
version: "4.0.0"
|
||||||
|
intl:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: intl
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.17.0"
|
||||||
js:
|
js:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -135,6 +219,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.6.3"
|
version: "0.6.3"
|
||||||
|
json_annotation:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: json_annotation
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "4.4.0"
|
||||||
lints:
|
lints:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -142,6 +233,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.1"
|
version: "1.0.1"
|
||||||
|
logging:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: logging
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.2"
|
||||||
matcher:
|
matcher:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -198,6 +296,20 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.5"
|
version: "2.0.5"
|
||||||
|
pedantic:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: pedantic
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.11.1"
|
||||||
|
petitparser:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: petitparser
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "4.4.0"
|
||||||
platform:
|
platform:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -212,6 +324,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.2"
|
version: "2.1.2"
|
||||||
|
pointycastle:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: pointycastle
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "3.5.2"
|
||||||
process:
|
process:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -413,6 +532,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.2.0+1"
|
version: "0.2.0+1"
|
||||||
|
xml:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: xml
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "5.3.1"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=2.16.2 <3.0.0"
|
dart: ">=2.16.2 <3.0.0"
|
||||||
flutter: ">=2.10.0"
|
flutter: ">=2.10.0"
|
||||||
|
|
|
@ -39,6 +39,7 @@ dependencies:
|
||||||
shared_preferences: ^2.0.13
|
shared_preferences: ^2.0.13
|
||||||
flash: ^2.0.3+2
|
flash: ^2.0.3+2
|
||||||
dio: ^4.0.6
|
dio: ^4.0.6
|
||||||
|
enough_mail: ^1.3.4
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
Loading…
Reference in a new issue