Enough-Software / enough_mail

IMAP, POP3 and SMTP clients for Dart developers. Contains both low level as well as a high level API.
Mozilla Public License 2.0
106 stars 55 forks source link

searchMessages with custom searchCriteria not working #244

Closed sayedmoataz closed 3 months ago

sayedmoataz commented 3 months ago

Dear All, I'm working on an application that require a basic functionality of email like fetch emails and send emails only so I used imap and smtp but I have a bit of issues that if I put a custom search criteria it's not working and return

[log] IMAP failed with BAD Could not parse command``` 

my method to do this 
```Future<List<Email>> fetchEmails({
    required String domain,
    required String userName,
    required String password,
    String? fromAddress,
    DateTime? sinceDate,
    DateTime? beforeDate,
    DateTime? specificTimeStart,
    DateTime? specificTimeEnd,
    int maxMessageCount = 2,
  }) async {
    final imapServerHost = 'imap.$domain';
    const imapServerPort = 993;
    const isImapServerSecure = true;
    final client = ImapClient(isLogEnabled: false);
    List<Email> emails = [];
    try {
      await _connectToServer(
          client, imapServerHost, imapServerPort, isImapServerSecure);
      await client.login(userName, password);
      await client.selectInbox();
      String searchCriteria = 'UNSEEN';
      if (sinceDate != null) {
        final sinceStr = sinceDate.toUtc().toIso8601String().split('T').first;
        searchCriteria = '$searchCriteria SINCE "$sinceStr"';
      }
      if (beforeDate != null) {
        final beforeStr = beforeDate.toUtc().toIso8601String().split('T').first;
        searchCriteria = '$searchCriteria BEFORE "$beforeStr"';
      }
      if (specificTimeStart != null && specificTimeEnd != null) {
        final startStr =
            specificTimeStart.toUtc().toIso8601String().split('T').last;
        final endStr =
            specificTimeEnd.toUtc().toIso8601String().split('T').last;
        searchCriteria =
            '$searchCriteria SENTSINCE "$startStr" SENTBEFORE "$endStr"';
      }
      log('searchCriteria is: $searchCriteria');
final fetchResult =
          await client.searchMessages(searchCriteria: searchCriteria);
      log('fetchResult_1 is: $fetchResult');
      var messageIds = fetchResult.matchingSequence?.toList() ?? [];
      if (messageIds.isEmpty) {
        log('No messages found');
        return emails;
      }
      messageIds = messageIds.take(maxMessageCount).toList();
      for (var messageId in fetchResult.matchingSequence!.toList()) {
        final fetchResult = await client.fetchMessage(messageId, 'BODY[]');
        log('fetchResult is: $fetchResult');
        emails.add(_parseMessage(fetchResult));
      }
      await client.logout();
    } on ImapException catch (e) {
      log('IMAP failed with $e');
    }
    return emails;
  }```

I tried many formats like 

searchCriteria is: UNSEEN SINCE "2024-08-03" BEFORE "2024-08-04" SENTSINCE "12:00:00.000Z" SENTBEFORE "14:00:00.000Z" searchCriteria is: UNSEEN SINCE "2024-08-03" BEFORE "2024-08-04" SENTSINCE "12:00:00.000Z" SENTBEFORE "14:00:00.000Z" searchCriteria is: UNSEEN SINCE 2024-08-03 BEFORE 2024-08-04 SENTSINCE 12:00:00.000Z SENTBEFORE 14:00:00.000Z searchCriteria is: UNSEEN SINCE 2024-08-03 BEFORE 2024-08-04 SENTSINCE 12:00 SENTBEFORE 22:00 searchCriteria is: UNSEEN SINCE "2024-08-03" BEFORE "2024-08-04" SENTSINCE "12:00" SENTBEFORE "22:00" searchCriteria is: UNSEEN SINCE "2024-08-03" searchCriteria is: SINCE "2024-08-03"



but all returns same error ? could anyone help me!
sayedmoataz commented 3 months ago

it's working when search criteria like as UNSEEN SINCE 10-Aug-2024 BEFORE 11-Aug-2024