this repo has no description
1(* Examples from the tutorial *) 2 3open Lwt.Syntax 4open Jmap 5open Jmap_mail 6 7(* Example: Authentication *) 8let auth_example () = 9 (* Using a Fastmail API token *) 10 let token = Sys.getenv_opt "JMAP_API_TOKEN" in 11 match token with 12 | None -> 13 Printf.eprintf "Error: JMAP_API_TOKEN environment variable not set\n"; 14 Lwt.return_none 15 | Some token -> 16 let+ result = Jmap_mail.login_with_token 17 ~uri:"https://api.fastmail.com/jmap/session" 18 ~api_token:token 19 in 20 21 (* Handle the result *) 22 match result with 23 | Ok conn -> 24 (* Get the primary account ID *) 25 let account_id = 26 let mail_capability = Jmap_mail.Capability.to_string Jmap_mail.Capability.Mail in 27 match List.assoc_opt mail_capability conn.session.primary_accounts with 28 | Some id -> id 29 | None -> 30 match conn.session.accounts with 31 | (id, _) :: _ -> id 32 | [] -> failwith "No accounts found" 33 in 34 Printf.printf "Authenticated successfully with account ID: %s\n" account_id; 35 Some (conn, account_id) 36 | Error e -> 37 Printf.eprintf "Authentication error: %s\n" 38 (match e with 39 | Api.Connection_error msg -> "Connection error: " ^ msg 40 | Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body 41 | Api.Parse_error msg -> "Parse error: " ^ msg 42 | Api.Authentication_error -> "Authentication error"); 43 None 44 45(* Example: Working with Mailboxes *) 46let mailbox_example (conn, account_id) = 47 (* Get all mailboxes *) 48 let+ mailboxes_result = Jmap_mail.get_mailboxes conn ~account_id in 49 50 match mailboxes_result with 51 | Ok mailboxes -> 52 Printf.printf "Found %d mailboxes\n" (List.length mailboxes); 53 54 (* Find inbox - for simplicity, just use the first mailbox *) 55 let inbox = match mailboxes with 56 | first :: _ -> Some first 57 | [] -> None 58 in 59 60 (match inbox with 61 | Some m -> 62 Printf.printf "Inbox ID: %s, Name: %s\n" 63 m.Types.id 64 m.Types.name; 65 Some (conn, account_id, m.Types.id) 66 | None -> 67 Printf.printf "No inbox found\n"; 68 None) 69 | Error e -> 70 Printf.eprintf "Error getting mailboxes: %s\n" 71 (match e with 72 | Api.Connection_error msg -> "Connection error: " ^ msg 73 | Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body 74 | Api.Parse_error msg -> "Parse error: " ^ msg 75 | Api.Authentication_error -> "Authentication error"); 76 None 77 78(* Example: Working with Emails *) 79let email_example (conn, account_id, mailbox_id) = 80 (* Get emails from mailbox *) 81 let+ emails_result = Jmap_mail.get_messages_in_mailbox 82 conn 83 ~account_id 84 ~mailbox_id 85 ~limit:5 86 () 87 in 88 89 match emails_result with 90 | Ok emails -> begin 91 Printf.printf "Found %d emails\n" (List.length emails); 92 93 (* Display emails *) 94 List.iter (fun (email:Jmap_mail.Types.email) -> 95 (* Using explicit module path for Types to avoid ambiguity *) 96 let module Mail = Jmap_mail.Types in 97 98 (* Get sender info *) 99 let from = match email.Mail.from with 100 | None -> "Unknown" 101 | Some addrs -> 102 match addrs with 103 | [] -> "Unknown" 104 | addr :: _ -> 105 match addr.Mail.name with 106 | None -> addr.Mail.email 107 | Some name -> 108 Printf.sprintf "%s <%s>" name addr.Mail.email 109 in 110 111 (* Check for unread status *) 112 let is_unread = 113 List.exists (fun (kw, active) -> 114 match kw with 115 | Mail.Unread -> active 116 | Mail.Custom s when s = "$unread" -> active 117 | _ -> false 118 ) email.Mail.keywords 119 in 120 121 (* Display email info *) 122 Printf.printf "[%s] %s - %s\n" 123 (if is_unread then "UNREAD" else "READ") 124 from 125 (Option.value ~default:"(No Subject)" email.Mail.subject) 126 ) emails; 127 128 match emails with 129 | [] -> None 130 | hd::_ -> Some (conn, account_id, hd.Jmap_mail.Types.id) 131 end 132 | Error e -> 133 Printf.eprintf "Error getting emails: %s\n" 134 (match e with 135 | Api.Connection_error msg -> "Connection error: " ^ msg 136 | Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body 137 | Api.Parse_error msg -> "Parse error: " ^ msg 138 | Api.Authentication_error -> "Authentication error"); 139 None 140 141(* Run examples with Lwt *) 142let () = 143 (* Set up logging *) 144 Jmap.init_logging ~level:2 ~enable_logs:true ~redact_sensitive:true (); 145 146 (* Run the examples in sequence *) 147 let result = Lwt_main.run ( 148 let* auth_result = auth_example () in 149 match auth_result with 150 | None -> Lwt.return 1 151 | Some conn_account -> 152 let* mailbox_result = mailbox_example conn_account in 153 match mailbox_result with 154 | None -> Lwt.return 1 155 | Some conn_account_mailbox -> 156 let* email_result = email_example conn_account_mailbox in 157 match email_result with 158 | None -> Lwt.return 1 159 | Some _ -> 160 Printf.printf "All examples completed successfully\n"; 161 Lwt.return 0 162 ) in 163 164 exit result