Repository Public
1#!/usr/bin/env python32"""SP Mail — envoi réel par JMAP (Email/set + EmailSubmission/set) et lecture de l'état de livraison.3Usage : JMAP_URL=http://10.67.0.1:8080 JMAP_USER=... JMAP_PASS=... jmap_send.py <destinataire> <sujet> [corps]4Aucun secret en argument ni en sortie."""5import base64, json, os, sys, time, urllib.request67URL, USER, PW = os.environ["JMAP_URL"].rstrip("/"), os.environ["JMAP_USER"], os.environ["JMAP_PASS"]8AUTH = "Basic " + base64.b64encode(f"{USER}:{PW}".encode()).decode()910def http(path, body=None):11 req = urllib.request.Request(URL + path, data=json.dumps(body).encode() if body else None,12 headers={"Authorization": AUTH, "Content-Type": "application/json"})13 with urllib.request.urlopen(req, timeout=30) as r:14 return json.load(r)1516def call(calls):17 using = ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail", "urn:ietf:params:jmap:submission"]18 return http("/jmap/", {"using": using, "methodCalls": calls})["methodResponses"]1920def main():21 to, subject = sys.argv[1], sys.argv[2]22 body = sys.argv[3] if len(sys.argv) > 3 else "Message de recette SP Mail — accents : é à ç ô — 日本語 — ✓"23 acct = http("/jmap/session")["primaryAccounts"]["urn:ietf:params:jmap:mail"]24 r = call([["Mailbox/get", {"accountId": acct, "properties": ["role", "name"]}, "m"],25 ["Identity/get", {"accountId": acct}, "i"]])26 boxes = {m.get("role"): m["id"] for m in r[0][1]["list"]}27 idents = r[1][1]["list"]28 ident = next((i for i in idents if i["email"].lower() == USER.lower()), idents[0] if idents else None)29 if ident is None:30 cr = call([["Identity/set", {"accountId": acct, "create": {"n": {"name": "SP Mail", "email": USER}}}, "c"]])31 ident = {"id": cr[0][1]["created"]["n"]["id"], "email": USER}32 r = call([33 ["Email/set", {"accountId": acct, "create": {"e": {34 "mailboxIds": {boxes.get("drafts") or boxes["inbox"]: True}, "keywords": {"$draft": True},35 "from": [{"name": "SP Mail recette", "email": USER}], "to": [{"email": to}], "subject": subject,36 "bodyValues": {"b": {"value": body}}, "textBody": [{"partId": "b", "type": "text/plain"}]}}}, "e"],37 ["EmailSubmission/set", {"accountId": acct, "create": {"s": {"identityId": ident["id"], "emailId": "#e"}},38 "onSuccessUpdateEmail": {"#s": {f"mailboxIds/{boxes.get('drafts') or boxes['inbox']}": None,39 f"mailboxIds/{boxes.get('sent') or boxes['inbox']}": True,40 "keywords/$draft": None}}}, "s"]])41 for name, res, _ in r:42 if res.get("notCreated"):43 print("ECHEC", name, json.dumps(res["notCreated"])); sys.exit(1)44 sub = r[1][1]["created"]["s"]["id"]45 print("accepté pour livraison, soumission", sub)46 for _ in range(12):47 time.sleep(5)48 g = call([["EmailSubmission/get", {"accountId": acct, "ids": [sub]}, "g"]])[0][1]["list"]49 if g:50 print("undoStatus:", g[0].get("undoStatus"), "| deliveryStatus:", json.dumps(g[0].get("deliveryStatus")))51 ds = g[0].get("deliveryStatus") or {}52 if ds and all(v.get("delivered") != "queued" for v in ds.values()):53 break5455main()56