1
0
Fork 0

Add mailer listener

This commit is contained in:
Florian RICHER (MrDev023) 2022-04-12 22:28:30 +02:00
parent ab32d8f471
commit 1351130f83
4 changed files with 184 additions and 0 deletions

53
lib/utils/mailer.dart Normal file
View 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');
}
}
}

View file

@ -1,4 +1,5 @@
import 'package:desktopapp/classes/routes.dart';
import 'package:desktopapp/utils/mailer.dart';
import 'package:desktopapp/utils/requests.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -44,6 +45,9 @@ class HomePage extends StatelessWidget {
}
},
child: const Text('Envoyer une alerte de test')),
TextButton(
onPressed: () => Mailer.mailExample(),
child: const Text('Test email')),
]);
}

View file

@ -1,6 +1,20 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
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:
dependency: transitive
description:
@ -8,6 +22,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.2"
basic_utils:
dependency: transitive
description:
name: basic_utils
url: "https://pub.dartlang.org"
source: hosted
version: "3.9.4"
bloc:
dependency: transitive
description:
@ -50,6 +71,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
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:
dependency: "direct main"
description:
@ -64,6 +99,41 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
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:
dependency: transitive
description:
@ -121,6 +191,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: transitive
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.4"
http_parser:
dependency: transitive
description:
@ -128,6 +205,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
intl:
dependency: transitive
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.0"
js:
dependency: transitive
description:
@ -135,6 +219,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "4.4.0"
lints:
dependency: transitive
description:
@ -142,6 +233,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
logging:
dependency: transitive
description:
name: logging
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
matcher:
dependency: transitive
description:
@ -198,6 +296,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
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:
dependency: transitive
description:
@ -212,6 +324,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
pointycastle:
dependency: transitive
description:
name: pointycastle
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.2"
process:
dependency: transitive
description:
@ -413,6 +532,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0+1"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "5.3.1"
sdks:
dart: ">=2.16.2 <3.0.0"
flutter: ">=2.10.0"

View file

@ -39,6 +39,7 @@ dependencies:
shared_preferences: ^2.0.13
flash: ^2.0.3+2
dio: ^4.0.6
enough_mail: ^1.3.4
dev_dependencies:
flutter_test: