From d28346ad7376fa425afd8893cbd6861193536e74 Mon Sep 17 00:00:00 2001 From: Mike McCann Date: Mon, 13 Apr 2026 15:20:51 -0700 Subject: [PATCH] Base64 encode the image for the email attachment(s). --- src/data/lrauv_deployment_plots.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/data/lrauv_deployment_plots.py b/src/data/lrauv_deployment_plots.py index e182922..8bbcd45 100755 --- a/src/data/lrauv_deployment_plots.py +++ b/src/data/lrauv_deployment_plots.py @@ -16,6 +16,7 @@ __copyright__ = "Copyright 2026, Monterey Bay Aquarium Research Institute" import argparse # noqa: I001 +import base64 import http import logging import os @@ -364,6 +365,7 @@ def _build_and_write_html( # noqa: PLR0913 stoqs_url, nc_files, auv_name=dlist.split("/")[0], + png_file_path=Path(png_path), ) self.logger.info("Per-PNG HTML written to %s", per_png_html) archiver = Archiver(add_handlers=True, clobber=True) @@ -564,11 +566,13 @@ def _write_per_png_html( # noqa: C901, PLR0913 stoqs_url: str | None, nc_files: list[str], auv_name: str = "", + png_file_path: Path | None = None, ) -> None: """Write a plain HTML page for one deployment PNG. - Embeds the full-size PNG (via fully-qualified *png_url*), links to the - STOQS database, then lists per-log image / data / STOQS links — no CSS. + Embeds the full-size PNG as a base64 data URI when *png_file_path* is + supplied (so the image works in email clients that block external URLs). + Falls back to the fully-qualified *png_url* otherwise. """ # Group nc_files by log directory (second-to-last path component) grouped: dict[str, list[str]] = {} @@ -608,6 +612,12 @@ def _write_per_png_html( # noqa: C901, PLR0913 db_label = after_scheme.split("/")[1] if "/" in after_scheme else after_scheme stoqs_line = f'

View these data in {db_label}

\n' + if png_file_path is not None and png_file_path.exists(): + b64 = base64.b64encode(png_file_path.read_bytes()).decode("ascii") + img_src = f"data:image/png;base64,{b64}" + else: + img_src = png_url + html_title_single = title.replace("\n", " \u2014 ") script_github_url = get_script_github_url("src/data/lrauv_deployment_plots.py") created_ts = datetime.now(tz=UTC).strftime("%Y-%m-%d %H:%M:%S UTC") @@ -626,7 +636,7 @@ def _write_per_png_html( # noqa: C901, PLR0913 "\n" "\n" f"

{html_title_single}

\n" - f' {png_name}\n' + f' {png_name}\n' f" {stoqs_line}" "

Log files

\n" "