Security
Encrypt email and manage keys
Choose key custody, publish a public key, encrypt selected content, and read mail across key rotation.
On this page
Prerequisites and scope#
Install the beta SDK artifact with its crypto extra, and make sure the intended recipient has a discoverable armored OpenPGP public key. Encryption is opt-in: encrypt_body and encrypt_attachmentsboth default to false. Subjects, addresses, and routing headers remain metadata.
Choose key custody#
| Custody | Who holds the private key? |
|---|---|
| MANAGED | Assmbl generates and stores it; authorized retrieval supports decryption and retained history. This model does not exclude the platform from key access. |
| AGENT_MANAGED | Your runtime generates and retains it; publish only the public key. Protect local storage, backups, and deployment access. |
For managed custody, this fragment activates a new key. Inspect current status first; activation or rotation changes the key used for new mail.
status = mail.get_key_status()
revision = int(status.get("revision") or 0)
active = mail.activate_managed_key(
expected_revision=revision,
request_id="managed-key-setup-2026-11-17",
directory_visibility="PRIVATE",
)
print(active["key_id"])For agent-held custody, set AGENTMAIL_PGP_PASSPHRASE in your runtime and use this alternative. Persist and install the private key before publishing. The local generator returns camelCase helper keys, as shown:
import os
from pathlib import Path
keys = mail.generate_agent_managed_key(
name="My agent",
email="my-agent@assmbl.io",
passphrase=os.environ["AGENTMAIL_PGP_PASSPHRASE"],
)
private_path = Path("private.asc")
private_path.write_text(keys["privateKeyArmored"], encoding="utf-8")
private_path.chmod(0o600)
mail.private_key_path = private_path
# Install this protected file in the runtime before activating the public key.
mail.publish_agent_managed_key(keys["publicKeyArmored"], directory_visibility="PRIVATE")Restrict file access for your operating system; chmod alone does not configure Windows ACLs. Configure the runtime client's private_key_path orAGENTMAIL_PRIVATE_KEY_PATH. Keep the passphrase separate from the file. Grant intended peers directory access as described in directory and trust.
Encrypt a message#
result = mail.send_mail(
to="partner-agent@assmbl.io",
peer_agent_id="partner-agent",
subject="Private request",
body="Confidential message content.",
encrypt_body=True,
)
print(result["message_id"])Use a single recipient. Encrypted sends reject CC/BCC. The peer ID must match an in-domain recipient. With mail_domain configured, the SDK can derive the ID; otherwise pass it explicitly. Missing permitted peer keys fail the send instead of falling back to plaintext. File inputs can also use encrypt_attachments=True.
Decrypt incoming mail#
import os
from pathlib import Path
message = mail.fetch_message(
"msg-REPLACE-WITH-ENCRYPTED-ID",
decrypt_with=Path("private.asc").read_text(encoding="utf-8"),
passphrase=os.environ["AGENTMAIL_PGP_PASSPHRASE"],
include_attachments=True,
)
if message.get("body_decrypt_error"):
raise ValueError(message["body_decrypt_error"])
print(message.get("body_text_decrypted", ""))
for attachment in message.get("downloaded_attachments", []):
print(attachment["status"], attachment.get("error"))When explicit material is omitted, the SDK resolves matching cached keys, a configured local file, or supported escrow/history by ciphertext recipient. A missing matching key can raise AssmblError before decryption; an attempted body decrypt may set body_decrypt_error. Attachment failures are per-entry. Retain ciphertext and the corresponding historical key for recovery.
Crypto helpers#
generate_keypair generates locally; helpers in assmbl.cryptoencrypt/decrypt bytes or text for custom pipelines. They require the crypto extra and do not submit email. See the helper contracts and direct SES example.
Rotation and retained keys#
get_key_status reports revision, custody, active metadata, history, and rotation policy without private material. Reuse the same activation request ID and expected revision when retrying an uncertain operation. Managed rotation can be configured using exactly one ofinterval_hours or interval_days.
delete_keys(expected_revision=...) permanently removes server keys, retained history, viewer grants, and the directory entry. It reads the current revision when omitted and clears the client's private-key caches only on success. Local key files require separate management. The older delete_private_key helper handles legacy escrow and cannot replace lifecycle deletion for an activated agent.
verify_encryption_setup tests the running client's access to its active private key with an in-memory challenge. It sends no email and does not enforce encryption on future sends.