Obtaining Mails

The emil.Access trait defines primitive MailOps for retrieving mails and working with folders.

Mails can retrieved by searching a folder. So at first a folder must be obtained. When searching mails, only MailHeaders are returned – the most common headers of an e-mail. More cane be loaded separately using the loadMail operation.

A SearchQuery must be provided to the search operation. Using import emil.SearchQuery._ allows for conveniently defining these:

import emil._, emil.SearchQuery._
import java.time._

val q = (ReceivedDate >= Instant.now.minusSeconds(60)) && (Subject =*= "test") && !Flagged
// q: And = And(
//   qs = List(
//     Not(c = Flagged),
//     ReceivedDate(date = 2023-11-18T11:55:18.043514104Z, rel = Ge),
//     Subject(text = "test")
//   )
// )

The subject test is a simple substring test. Look at SearchQuery to find out what is possible.

Searching inbox may look like this:

import cats.effect._

def searchInbox[C](a: Access[IO, C], q: SearchQuery) =
  for {
    inbox  <- a.getInbox
    mails  <- a.search(inbox, 20)(q)
  } yield mails

The 20 defines a maximum count. Just use Int.MaxValue to return all.

Returning mail headers only from a search saves bandwidth and is fast. If you really want to load all mails found, use the searchAndLoad operation.

import cats.effect._

def searchLoadInbox[C](a: Access[IO, C], q: SearchQuery) =
  for {
    inbox  <- a.getInbox
    mails  <- a.searchAndLoad(inbox, 20)(q)
  } yield mails