1
0
Fork 0
mrtiboute/lib/utils/mailer.dart

77 lines
3 KiB
Dart
Raw Normal View History

2022-04-14 21:57:32 +02:00
import 'dart:async';
import 'package:desktopapp/classes/stream_element.dart';
2022-04-12 22:28:30 +02:00
import 'package:desktopapp/utils/logger.dart';
import 'package:enough_mail/enough_mail.dart';
class Mailer {
2022-04-14 21:57:32 +02:00
static MailClient? _mailClient;
2022-04-12 22:28:30 +02:00
/// High level mail API example
2022-04-14 21:57:32 +02:00
static Future<bool> connect({
required String email,
required String password,
required StreamController<StreamElement> streamController,
}) async {
2022-04-12 22:28:30 +02:00
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');
2022-04-14 21:57:32 +02:00
return false;
2022-04-12 22:28:30 +02:00
}
Logger.log(LoggerType.info, Mailer, 'Settings for $email : $config');
final account = MailAccount.fromDiscoveredSettings(
'my account', email, password, config);
2022-04-14 21:57:32 +02:00
_mailClient = MailClient(account, isLogEnabled: true);
2022-04-12 22:28:30 +02:00
try {
2022-04-14 21:57:32 +02:00
await _mailClient!.connect();
2022-04-12 22:28:30 +02:00
Logger.log(LoggerType.info, Mailer, 'Connected to $email');
2022-04-14 21:57:32 +02:00
streamController
.add(StreamElement(StreamElementType.log, 'Connected to $email'));
await _mailClient!.selectInbox();
streamController.add(
StreamElement(StreamElementType.log, 'INBOX selected for $email'));
_mailClient!.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
2022-04-12 22:28:30 +02:00
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);
}
2022-04-14 21:57:32 +02:00
final messages = await _mailClient!.fetchMessageSequence(sequence,
fetchPreference: FetchPreference.envelope);
2022-04-12 22:28:30 +02:00
for (final message in messages) {
2022-04-14 21:57:32 +02:00
var mailEvt = MailLoadEvent(message, _mailClient!);
streamController.add(StreamElement(StreamElementType.alert,
mailEvt.message.decodeSubject() ?? ''));
// var content = mailEvt.message.decodeSubject();
// var mess = mailEvt.message.decodeTextPlainPart();
// Logger.log(LoggerType.info, Mailer, 'New message $content : $mess');
// Requests.sendAlert('*$content* $mess');
2022-04-12 22:28:30 +02:00
}
}
});
2022-04-14 21:57:32 +02:00
await _mailClient!.startPolling();
return true;
2022-04-12 22:28:30 +02:00
} on MailException catch (e) {
2022-04-14 21:57:32 +02:00
streamController.add(StreamElement(StreamElementType.log, e.toString()));
2022-04-12 22:28:30 +02:00
Logger.log(LoggerType.error, Mailer, 'High level API failed with $e');
2022-04-14 21:57:32 +02:00
return false;
2022-04-12 22:28:30 +02:00
}
}
2022-04-14 21:57:32 +02:00
static Future<void> disconnect() async {
if (_mailClient == null) return;
Logger.log(LoggerType.info, Mailer, 'Disconnecting from mail');
await _mailClient!.stopPolling();
await _mailClient!.disconnect();
Logger.log(LoggerType.info, Mailer, 'Disconnected from mail');
_mailClient = null;
}
2022-04-12 22:28:30 +02:00
}