2026-04-16 07:35:56 +02:00
|
|
|
import 'package:enough_mail/enough_mail.dart';
|
|
|
|
|
|
|
|
|
|
import '../../core/models/account.dart';
|
|
|
|
|
|
2026-04-16 07:51:52 +02:00
|
|
|
/// Opens an authenticated IMAP client for [account].
|
2026-04-16 07:35:56 +02:00
|
|
|
Future<ImapClient> connectImap(Account account, String password) async {
|
|
|
|
|
final client = ImapClient(isLogEnabled: false);
|
|
|
|
|
await client.connectToServer(
|
|
|
|
|
account.imapHost,
|
|
|
|
|
account.imapPort,
|
|
|
|
|
isSecure: account.imapSsl,
|
|
|
|
|
);
|
|
|
|
|
await client.login(account.email, password);
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 07:51:52 +02:00
|
|
|
/// Opens an authenticated SMTP client for [account].
|
|
|
|
|
///
|
|
|
|
|
/// Caller is responsible for calling [SmtpClient.quit] when done.
|
2026-04-16 07:35:56 +02:00
|
|
|
Future<SmtpClient> connectSmtp(Account account, String password) async {
|
2026-04-16 07:51:52 +02:00
|
|
|
// clientDomain is the sending domain advertised in EHLO — use the host part
|
|
|
|
|
// of the sender email, falling back to the SMTP host.
|
|
|
|
|
final atIndex = account.email.lastIndexOf('@');
|
|
|
|
|
final clientDomain =
|
|
|
|
|
atIndex != -1 ? account.email.substring(atIndex + 1) : account.smtpHost;
|
|
|
|
|
|
|
|
|
|
final client = SmtpClient(clientDomain, isLogEnabled: false);
|
2026-04-16 07:35:56 +02:00
|
|
|
await client.connectToServer(
|
|
|
|
|
account.smtpHost,
|
|
|
|
|
account.smtpPort,
|
|
|
|
|
isSecure: account.smtpSsl,
|
|
|
|
|
);
|
|
|
|
|
await client.ehlo();
|
|
|
|
|
if (!account.smtpSsl) {
|
2026-04-16 07:51:52 +02:00
|
|
|
// Opportunistic TLS on submission port (587)
|
|
|
|
|
try {
|
|
|
|
|
await client.startTls();
|
|
|
|
|
} catch (_) {
|
|
|
|
|
// Server doesn't support STARTTLS — proceed without it.
|
|
|
|
|
}
|
2026-04-16 07:35:56 +02:00
|
|
|
}
|
2026-04-16 07:51:52 +02:00
|
|
|
await client.authenticate(account.email, password);
|
2026-04-16 07:35:56 +02:00
|
|
|
return client;
|
|
|
|
|
}
|