Files
breakpilot-compliance/backend-compliance/compliance/services/smtp_sender.py
T
Benjamin Admin c2c8783fee refactor(agent-check): split routes file (2692→347 LOC) + wire B1/B3/A1 [guardrail-change]
Phase-5 split of agent_compliance_check_routes.py — the 2700-line
monolith was decomposed into 19 modules in compliance/api/agent_check/:

  - Phase A-F: resolve / profile+check / banner+TCF / vendors raw+finalize /
    HTML blocks top+mid+bot / email / persist
  - Helpers: _constants, _helpers, _fetch, _discovery, _single_check
  - Schemas + State + thin _orchestrator

A1 ZIP-Anhang nativ in _phase_e_email: evidence_zip_builder.py bundles
slices + manifest.json + audit_metadata.json (SHA256 per slice +
build_sha + source_url). smtp_sender.py erweitert um attachments-Parameter.

B1 COOKIE-CONSENT-UX-001 (Mobile Reachability): consent_reachability_check.py
parses footer anchors, classifies intent (reopen_cmp / info_only /
browser_deflect) + target (same_page_cmp / new_tab / external).
_b1_wiring.py fetches homepage with iPhone-UA + renders Art-7-Abs-3
severity-coloured block.

B3 TH-RETENTION (Cross-Doc Speicherdauer): retention_comparator.py
compares DSI claim ↔ cookie-table duration ↔ actual Max-Age/expires
with 5% tolerance + severity hierarchy (dsi_under_actual HIGH,
table_under_actual HIGH, dsi_vs_table MEDIUM, actual_under_table LOW
Safari-ITP-Hint). _b3_wiring.py + Top-10 mismatches table in mail.

Side-effects:
- Fixed silent UnboundLocalError in original Step 5 (gf_one_pager used
  audit_quality_findings before declaration, caught by surrounding
  except → block never rendered). New _phase_d3_blocks_bot.py runs
  audit-quality FIRST.
- agent_compliance_check_routes.py removed from loc-exceptions.txt
  ("Phase 5 split target" — done).

Tests: 55/55 grün (B1 22 + B3 27 + saving_scan 6).
E2E: smoke against Elli DSE+Cookie produced HIGH/missing B1 finding,
TH-RETENTION table (17 cookies / 3 ✓ / 3 ✗ / 11 ?), evidence-zip
with 2 slices + manifest + audit_metadata (12089B, SHA256-chained,
source verified), email sent (attachments=1).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-06 14:47:25 +02:00

86 lines
2.9 KiB
Python

"""
SMTP Sender — sends real emails via SMTP (e.g., to Mailpit for dev).
Uses standard smtplib. Configuration via environment variables:
SMTP_HOST (default: localhost)
SMTP_PORT (default: 1025)
SMTP_FROM_NAME (default: BreakPilot Compliance)
SMTP_FROM_ADDR (default: compliance@breakpilot.local)
"""
from __future__ import annotations
import logging
import os
import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
logger = logging.getLogger(__name__)
SMTP_HOST = os.environ.get("SMTP_HOST", "localhost")
SMTP_PORT = int(os.environ.get("SMTP_PORT", "1025"))
SMTP_FROM_NAME = os.environ.get("SMTP_FROM_NAME", "BreakPilot Compliance")
SMTP_FROM_ADDR = os.environ.get("SMTP_FROM_ADDR", "compliance@breakpilot.local")
def send_email(
recipient: str,
subject: str,
body_html: str,
from_addr: str | None = None,
from_name: str | None = None,
attachments: list[dict] | None = None,
) -> dict:
"""Send an email via SMTP. Returns dict with status and message_id.
attachments: optional list of dicts:
[{"filename": "evidence.zip", "data": <bytes>,
"mime": "application/zip"}, ...]
"""
sender_addr = from_addr or SMTP_FROM_ADDR
sender_name = from_name or SMTP_FROM_NAME
if attachments:
msg = MIMEMultipart("mixed")
body = MIMEMultipart("alternative")
body.attach(MIMEText(body_html, "html", "utf-8"))
msg.attach(body)
for att in attachments:
mime = att.get("mime", "application/octet-stream")
maintype, _, subtype = mime.partition("/")
part = MIMEBase(maintype or "application", subtype or "octet-stream")
part.set_payload(att.get("data", b""))
encoders.encode_base64(part)
fname = att.get("filename", "attachment.bin")
part.add_header(
"Content-Disposition",
f'attachment; filename="{fname}"',
)
msg.attach(part)
else:
msg = MIMEMultipart("alternative")
msg.attach(MIMEText(body_html, "html", "utf-8"))
msg["From"] = f"{sender_name} <{sender_addr}>"
msg["To"] = recipient
msg["Subject"] = subject
try:
with smtplib.SMTP(SMTP_HOST, SMTP_PORT, timeout=30) as server:
server.sendmail(sender_addr, [recipient], msg.as_string())
att_count = len(attachments or [])
logger.info(
"Email sent to %s: %s (attachments=%d)",
recipient, subject, att_count,
)
return {
"status": "sent", "recipient": recipient, "subject": subject,
"attachments": att_count,
}
except Exception as e:
logger.error("Failed to send email to %s: %s", recipient, e)
return {"status": "failed", "recipient": recipient, "error": str(e)}