this repo has no description
1(** 2 * fastmail_list - Lists emails from a Fastmail account using JMAP API 3 * 4 * This binary connects to the Fastmail JMAP API using an authentication token 5 * from the JMAP_API_TOKEN environment variable and lists the most recent 100 6 * emails with their subjects and sender details. 7 * 8 * Usage: 9 * JMAP_API_TOKEN=your_api_token ./fastmail_list 10 *) 11 12open Lwt.Syntax 13open Jmap 14open Jmap_mail 15module Mail = Jmap_mail.Types 16 17(** Prints the email details *) 18let print_email (email : Mail.email) = 19 let sender = 20 match email.from with 21 | Some (addr :: _) -> 22 (match addr.name with 23 | Some name -> Printf.sprintf "%s <%s>" name addr.email 24 | None -> addr.email) 25 | _ -> "<unknown>" 26 in 27 let subject = 28 match email.subject with 29 | Some s -> s 30 | None -> "<no subject>" 31 in 32 let date = email.received_at in 33 Printf.printf "%s | %s | %s\n" date sender subject 34 35(** Main function *) 36let main () = 37 match Sys.getenv_opt "JMAP_API_TOKEN" with 38 | None -> 39 Printf.eprintf "Error: JMAP_API_TOKEN environment variable not set\n"; 40 Printf.eprintf "Usage: JMAP_API_TOKEN=your_token ./fastmail_list\n"; 41 exit 1 42 | Some token -> 43 (* Connect to Fastmail JMAP API *) 44 let* result = login_with_token 45 ~uri:"https://api.fastmail.com/jmap/session" 46 ~api_token:token 47 in 48 match result with 49 | Error err -> 50 (match err with 51 | Api.Connection_error msg -> 52 Printf.eprintf "Connection error: %s\n" msg 53 | Api.HTTP_error (code, body) -> 54 Printf.eprintf "HTTP error %d: %s\n" code body 55 | Api.Parse_error msg -> 56 Printf.eprintf "Parse error: %s\n" msg 57 | Api.Authentication_error -> 58 Printf.eprintf "Authentication error. Check your API token.\n"); 59 Lwt.return 1 60 | Ok conn -> 61 (* Get the primary account ID *) 62 let primary_account_id = 63 match List.assoc_opt "urn:ietf:params:jmap:mail" conn.session.primary_accounts with 64 | Some id -> id 65 | None -> 66 match conn.session.accounts with 67 | (id, _) :: _ -> id 68 | [] -> 69 Printf.eprintf "No accounts found\n"; 70 exit 1 71 in 72 73 (* Get the Inbox mailbox *) 74 let* mailboxes_result = get_mailboxes conn ~account_id:primary_account_id in 75 match mailboxes_result with 76 | Error err -> 77 Printf.eprintf "Failed to get mailboxes: %s\n" 78 (match err with 79 | Api.Connection_error msg -> "Connection error: " ^ msg 80 | Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body 81 | Api.Parse_error msg -> "Parse error: " ^ msg 82 | Api.Authentication_error -> "Authentication error"); 83 Lwt.return 1 84 | Ok mailboxes -> 85 (* If there's a mailbox list, just use the first one for this example *) 86 let inbox_id = 87 match mailboxes with 88 | mailbox :: _ -> mailbox.Mail.id 89 | [] -> 90 Printf.eprintf "No mailboxes found\n"; 91 exit 1 92 in 93 94 (* Get messages from inbox *) 95 let* emails_result = get_messages_in_mailbox 96 conn 97 ~account_id:primary_account_id 98 ~mailbox_id:inbox_id 99 ~limit:100 100 () 101 in 102 match emails_result with 103 | Error err -> 104 Printf.eprintf "Failed to get emails: %s\n" 105 (match err with 106 | Api.Connection_error msg -> "Connection error: " ^ msg 107 | Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body 108 | Api.Parse_error msg -> "Parse error: " ^ msg 109 | Api.Authentication_error -> "Authentication error"); 110 Lwt.return 1 111 | Ok emails -> 112 Printf.printf "Listing the most recent %d emails in your inbox:\n" (List.length emails); 113 Printf.printf "--------------------------------------------\n"; 114 List.iter print_email emails; 115 Lwt.return 0 116 117(** Program entry point *) 118let () = 119 let exit_code = Lwt_main.run (main ()) in 120 exit exit_code