AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(blocks): email the account owner their own result through the platform's mail, with no sign-in (#15838)
Reinier van der Leerf3814783f8ba
diff --git a/autogpt_platform/backend/backend/blocks/email_me.py b/autogpt_platform/backend/backend/blocks/email_me.pynew file mode 100644index 0000000000..3af716f46e--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/email_me.py@@ -0,0 +1,111 @@+import asyncio++from backend.blocks._base import (+ Block,+ BlockCategory,+ BlockEffect,+ BlockOutput,+ BlockSchemaInput,+ BlockSchemaOutput,+)+from backend.data.execution import ExecutionContext+from backend.data.model import SchemaField+from backend.notifications.owner_email_body import (+ BodyFormat,+ InlineImage,+ image_sources,+)+from backend.notifications.owner_email_images import fetch_images+from backend.util.clients import get_notification_manager_client+++class SendEmailToMeBlock(Block):+ class Input(BlockSchemaInput):+ subject: str = SchemaField(+ description="Subject of the email", placeholder="Your weekly summary"+ )+ body: str = SchemaField(+ description=(+ "Body of the email, written in the chosen format. Images "+ "( or an https:// URL in Markdown, <img> "+ "in HTML) are attached to the email."+ ),+ placeholder="Here is what I found this week...",+ )+ body_format: BodyFormat = SchemaField(+ description=(+ "How the body is written: Markdown, plain text, or HTML (scripts, "+ "forms and styles are removed)"+ ),+ default=BodyFormat.MARKDOWN,+ )+ link: str = SchemaField(+ description="Optional web link to show as a button, e.g. to the full result",+ default="",+ advanced=True,+ )++ class Output(BlockSchemaOutput):+ status: str = SchemaField(description="Status of the email sending operation")++ def __init__(self):+ super().__init__(+ id="38997608-a57a-4e11-971c-b83a5332e80b",+ description=(+ "Email yourself: sends a message to your own inbox, at your account's "+ "email address, from the platform, with no email account to sign in "+ "to. Use it for "+ '"email me" or "send it to myself": a result, a summary or a '+ "recurring digest. It can't send to anyone else; to email other "+ "people, use Gmail or SMTP."+ ),+ categories={BlockCategory.OUTPUT, BlockCategory.COMMUNICATION},+ input_schema=SendEmailToMeBlock.Input,+ output_schema=SendEmailToMeBlock.Output,+ test_input={"subject": "Test Email", "body": "This is a test email."},+ test_output=[("status", "Email sent to your account address")],+ test_mock={"send_to_owner": lambda *args, **kwargs: None},+ # The only recipient is the account owner: a self-DM, not a send to+ # someone outside the platform.+ capability_kind="service",+ effect=BlockEffect.EXTERNAL_JUDGED,+ )++ async def run(+ self,+ input_data: Input,+ *,+ user_id: str,+ execution_context: ExecutionContext,+ **kwargs,+ ) -> BlockOutput:+ try:+ sources = await asyncio.to_thread(+ image_sources, input_data.body, input_data.body_format+ )+ images = await fetch_images(sources, execution_context)+ await self.send_to_owner(+ user_id,+ input_data.subject,+ input_data.body,+ input_data.link or None,+ input_data.body_format,+ images,+ )+ except (ValueError, RuntimeError, TimeoutError) as e:+ yield "error", str(e)+ return+ yield "status", "Email sent to your account address"++ @staticmethod+ async def send_to_owner(+ user_id: str,+ subject: str,+ body: str,+ link: str | None,+ body_format: BodyFormat,+ images: list[InlineImage],+ ) -> None:+ await get_notification_manager_client().send_owner_email(+ user_id, subject, body, link, body_format, images+ )diff --git a/autogpt_platform/backend/backend/blocks/email_me_test.py b/autogpt_platform/backend/backend/blocks/email_me_test.pynew file mode 100644index 0000000000..eeaa57909c--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/email_me_test.py@@ -0,0 +1,642 @@+"""SendEmailToMeBlock run the way Otto runs it, with Postmark as the only stub.++Everything from the copilot's block runner down to the notification service's+sender is real, except the RPC hop to that service, which is called in-process.+"""++import asyncio+import base64+import threading+from datetime import UTC, datetime+from types import SimpleNamespace+from typing import Self+from unittest.mock import AsyncMock, MagicMock, patch++import aiohttp+import pytest+import requests+from postmarker.exceptions import ClientError++from backend.blocks import email_me+from backend.blocks.email_me import SendEmailToMeBlock+from backend.copilot.tools._test_data import make_session+from backend.copilot.tools.models import BlockOutputResponse, SetupRequirementsResponse+from backend.copilot.tools.run_block import RunBlockTool+from backend.data.execution import ExecutionContext+from backend.data.workspace import WorkspaceFile+from backend.notifications import email as email_module+from backend.notifications import notifications, owner_email, owner_email_images+from backend.notifications.email import EmailSender+from backend.util import file as file_module+from backend.util import workspace as workspace_module+from backend.util.exceptions import BlockExecutionError++REFUSED = ClientError("[406] Recipient is marked inactive", error_code=406)+OWNER = "owner-user"+OWNER_ADDRESS = "[email protected]"+OTHER = "other-user"+OTHER_ADDRESS = "[email protected]"+BLOCK_ID = SendEmailToMeBlock().id+++def postmark():+ return MagicMock()+++def accounts():+ return {+ OWNER: SimpleNamespace(address=OWNER_ADDRESS, verified=True),+ OTHER: SimpleNamespace(address=OTHER_ADDRESS, verified=True),+ }+++def counter():+ return _FakeRedis()++[email protected](autouse=True)+def platform(monkeypatch, postmark, accounts, counter):+ """The mail service behind the block: real code, stubbed at Postmark, the+ account table and Redis."""+ sender = EmailSender.__new__(EmailSender)+ sender.postmark = postmark+ manager = notifications.NotificationManager.__new__(+ notifications.NotificationManager+ )+ manager.email_sender = sender+ monkeypatch.setattr(+ email_me,+ "get_notification_manager_client",+ lambda: SimpleNamespace(send_owner_email=manager.send_owner_email),+ )++ db = MagicMock()+ db.get_user_email_by_id = AsyncMock(+ side_effect=lambda uid: accounts[uid].address if uid in accounts else None+ )+ db.get_user_email_verification = AsyncMock(+ side_effect=lambda uid: accounts[uid].verified+ )+ monkeypatch.setattr(+ owner_email, "get_database_manager_async_client", lambda **_: db+ )+ monkeypatch.setattr(owner_email, "get_redis_async", AsyncMock(return_value=counter))+ # This mail is sent while prod has every platform notification switched off.+ monkeypatch.setattr(+ notifications.settings.config, "enable_user_notifications", False+ )++[email protected](autouse=True)+def copilot_runtime():+ reviews = MagicMock()+ reviews.get_pending_reviews_for_chat_session = AsyncMock(return_value=[])+ workspace = MagicMock()+ workspace.get_or_create_workspace = AsyncMock(return_value=MagicMock(id="ws"))+ users = MagicMock()+ users.get_user_by_id = AsyncMock(side_effect=ValueError)+ with (+ patch("backend.copilot.tools.helpers.review_db", return_value=reviews),+ patch("backend.copilot.tools.helpers.workspace_db", return_value=workspace),+ patch("backend.copilot.tools.helpers.user_db", return_value=users),+ # An account with nothing connected, so a block that wanted a+ # credential would get its sign-in card here rather than an RPC.+ patch(+ "backend.copilot.tools.utils.get_user_credentials",+ AsyncMock(return_value=[]),+ ),+ patch(+ "backend.copilot.tools.utils.selected_credentials",+ AsyncMock(return_value={}),+ ),+ ):+ yield+++async def run(input_data: dict, user_id: str = OWNER):+ return await RunBlockTool()._execute(+ user_id=user_id,+ session=make_session(user_id=user_id),+ block_id=BLOCK_ID,+ input_data=input_data,+ dry_run=False,+ )+++def sent_to(postmark) -> list[str]:+ return [call.kwargs["To"] for call in postmark.emails.send.call_args_list]+++async def test_an_ask_in_chat_emails_the_owner_with_no_sign_in_card(postmark):+ response = await run({"subject": "Your digest", "body": "Three things."})++ assert not isinstance(response, SetupRequirementsResponse)+ assert isinstance(response, BlockOutputResponse)+ assert response.outputs == {"status": ["Email sent to your account address"]}+ assert sent_to(postmark) == [OWNER_ADDRESS]+ assert postmark.emails.send.call_args.kwargs["Subject"] == "Your digest"+++ "extra",+ [+ {"to": OTHER_ADDRESS},+ {"to_email": OTHER_ADDRESS},+ {"recipient": OTHER_ADDRESS},+ {"email": OTHER_ADDRESS},+ {"user_id": OTHER},+ ],+)+async def test_no_input_sends_it_anywhere_but_the_owners_address(postmark, extra):+ await run({"subject": "s", "body": "b", **extra})++ assert set(sent_to(postmark)) <= {OWNER_ADDRESS}+++async def test_an_unverified_address_gets_nothing(postmark, accounts):+ accounts[OWNER].verified = False++ response = await run({"subject": "s", "body": "b"})++ assert "isn't verified" in _error(response)+ assert sent_to(postmark) == []+++async def test_the_daily_cap_stops_a_looping_run(monkeypatch, postmark):+ monkeypatch.setattr(owner_email.settings.config, "owner_email_daily_limit", 2)++ for _ in range(2):+ await run({"subject": "s", "body": "b"})+ response = await run({"subject": "s", "body": "b"})++ assert "the most allowed in a day" in _error(response)+ assert sent_to(postmark) == [OWNER_ADDRESS, OWNER_ADDRESS]+++async def test_a_send_postmark_refused_does_not_use_up_the_days_allowance(+ monkeypatch, postmark+):+ monkeypatch.setattr(owner_email.settings.config, "owner_email_daily_limit", 1)+ postmark.emails.send.side_effect = [REFUSED, None]++ refused = await run({"subject": "s", "body": "b"})+ response = await run({"subject": "s", "body": "b"})++ assert "The email service refused this email" in _error(refused)+ assert response.outputs == {"status": ["Email sent to your account address"]}+++async def test_a_send_that_may_have_arrived_keeps_its_slot(monkeypatch, postmark):+ monkeypatch.setattr(owner_email.settings.config, "owner_email_daily_limit", 1)+ postmark.emails.send.side_effect = [requests.Timeout("read timed out"), None]++ unknown = await run({"subject": "s", "body": "b"})+ response = await run({"subject": "s", "body": "b"})++ assert "it may still arrive" in _error(unknown)+ assert "the most allowed in a day" in _error(response)+++async def test_an_unreadable_daily_count_sends_nothing(postmark, counter):+ counter.broken = True++ response = await run({"subject": "s", "body": "b"})++ assert "Couldn't check today's email count" in _error(response)+ assert sent_to(postmark) == []+++async def test_without_postmark_the_run_reports_a_failure_not_a_send():+ sender = EmailSender.__new__(EmailSender)+ sender.postmark = None+ manager = notifications.NotificationManager.__new__(+ notifications.NotificationManager+ )+ manager.email_sender = sender+ with patch.object(+ email_me,+ "get_notification_manager_client",+ lambda: SimpleNamespace(send_owner_email=manager.send_owner_email),+ ):+ response = await run({"subject": "s", "body": "b"})++ assert "isn't set up" in _error(response)+++async def test_a_refused_send_does_not_lock_out_the_day_when_another_fails(+ monkeypatch, postmark+):+ """A refused at the cap while B is in flight, then B fails: the day's one+ slot must come back."""+ monkeypatch.setattr(owner_email.settings.config, "owner_email_daily_limit", 1)+ in_flight, release = threading.Event(), threading.Event()++ def postmark_send(**_):+ if not in_flight.is_set():+ in_flight.set()+ release.wait(5)+ raise REFUSED++ postmark.emails.send.side_effect = postmark_send+ first = asyncio.create_task(run({"subject": "s", "body": "b"}))+ await asyncio.to_thread(in_flight.wait, 5)+ refused = await run({"subject": "s", "body": "b"})+ release.set()+ await first+ retry = await run({"subject": "s", "body": "b"})++ assert "the most allowed in a day" in _error(refused)+ assert retry.outputs == {"status": ["Email sent to your account address"]}+++ "body_format, body, shown",+ [+ ("plain", "Total: **3** <b>new</b>", "Total: **3** <b>new</b>"),+ ("markdown", "Total: **3** new", "Total: <strong>3</strong> new"),+ (+ "html",+ "<p>Total: <strong>3</strong> new</p>",+ "Total: <strong>3</strong> new",+ ),+ ],+)+async def test_the_body_is_read_in_the_format_the_run_chose(+ postmark, body_format, body, shown+):+ await run({"subject": "s", "body": body, "body_format": body_format})++ assert shown in _sent(postmark)["HtmlBody"]+++HOSTILE = (+ '<p onclick="steal()" style="background:url(https://t.example/a.gif)">Hi</p>'+ "<script>alert(1)</script>"+ '<img src="https://203.0.113.9/t.example/pixel.gif" width="1" height="1">'+ '<form action="https://evil.example"><input name="password"><button>Go</button></form>'+ '<a href="javascript:alert(2)">bad</a> <a href="https://ok.example">ok</a>'+ '<a href="data:text/html;base64,PHNjcmlwdD4=">click</a>'+)++GONE = ("<script", "alert(", "steal()", "t.example", "<form", "<input", "javascript:")+GONE += ("evil.example", ">Go<", "data:text/html")++[email protected]("body_format", ["markdown", "html"])+async def test_scripts_forms_and_tracking_pixels_never_reach_the_mail(+ postmark, body_format+):+ subject = '<img src="https://headline.invalid/s.gif"> Digest'+ await run({"subject": subject, "body": HOSTILE, "body_format": body_format})++ sent = _sent(postmark)+ body = sent["HtmlBody"].split("FROM OTTO", 1)[1]+ assert '<img src="https://headline.invalid' not in sent["HtmlBody"]+ for gone in GONE:+ assert gone not in body, gone+ assert 'href="https://ok.example"' in body+ assert "alert(" not in sent["TextBody"] and "Hi" in sent["TextBody"]+++ "body_format, body",+ [+ ("plain", "Three things.\n\nFirst, the news."),+ ("markdown", "## Three things\n\n- First, the news."),+ ("html", "<h2>Three things</h2><ul><li>First, the news.</li></ul>"),+ ],+)+async def test_every_format_sends_a_plain_text_part(postmark, body_format, body):+ await run({"subject": "s", "body": body, "body_format": body_format})++ text = _sent(postmark)["TextBody"]+ assert "Three things" in text and "First, the news." in text+ assert "<" not in text+++async def test_a_link_keeps_its_attributes_and_gets_the_template_style(postmark):+ body = '<p><a href="https://ok.example" title="A -> B">the report</a></p>'+ await run({"subject": "s", "body": body, "body_format": "html"})++ assert (+ '<a href="https://ok.example" title="A -> B" '+ 'style="color:#4D36AD; text-decoration:underline;">the report</a>'+ ) in _sent(postmark)["HtmlBody"]+++ "subject, body, refusal",+ [(" \n ", "b", "subject"), ("s", " \n\n ", "The body is empty")],+)+async def test_a_blank_subject_or_body_is_refused(postmark, subject, body, refusal):+ response = await run({"subject": subject, "body": body})++ assert refusal in _error(response)+ assert sent_to(postmark) == []+++ "opening, closing",+ # Each of the last three hides the nesting from a naive count: a closing tag+ # in an attribute value, a self-closing div, an end tag for nothing open.+ [+ ("<div>", "</div>"),+ ("<div title='</div>'>", "</div>"),+ ("<div/>", ""),+ ("<div></b>", ""),+ ],+)+async def test_html_nested_too_deeply_is_refused(postmark, opening, closing):+ body = opening * 150 + "deep" + closing * 150++ response = await run({"subject": "s", "body": body, "body_format": "html"})++ assert "nested more than 100 levels deep" in _error(response)+ assert sent_to(postmark) == []+++async def test_an_unclosed_script_takes_the_rest_of_the_body_with_it(postmark):+ body = "<p>Before</p><script>steal(document.cookie)"++ await run({"subject": "s", "body": body, "body_format": "html"})++ html = _sent(postmark)["HtmlBody"]+ assert "Before" in html+ assert "steal(" not in html+++async def test_a_dropped_element_after_wide_unicode_is_cut_where_it_ends(postmark):+ # "İ" lowercases to two characters, which shifts any offset taken from .lower().+ body = "İstanbul " * 5 + "<script>steal()</script><p>after the script</p>"++ await run({"subject": "s", "body": body, "body_format": "html"})++ html = _sent(postmark)["HtmlBody"]+ assert "after the script" in html+ assert "steal(" not in html+++async def test_a_link_that_is_not_a_web_address_is_refused(postmark):+ response = await run({"subject": "s", "body": "b", "link": "javascript:alert(1)"})++ assert "must be a web address" in _error(response)+ assert sent_to(postmark) == []+++ "failure",+ [RuntimeError("Email sending isn't set up"), TimeoutError("it may still arrive")],+)+async def test_an_expected_failure_is_a_block_error_not_an_unknown_one(+ monkeypatch, failure+):+ """Uncaught, these would be filed as BlockUnknownError, the class kept for bugs."""+ block = SendEmailToMeBlock()+ monkeypatch.setattr(block, "send_to_owner", AsyncMock(side_effect=failure))+ context = ExecutionContext(+ user_id=OWNER, graph_id="g", graph_exec_id="ge", graph_version=1+ )++ with pytest.raises(BlockExecutionError, match=str(failure)):+ async for _ in block.execute(+ {"subject": "s", "body": "b"},+ execution_context=context,+ user_id=OWNER,+ graph_id="g",+ graph_exec_id="ge",+ graph_version=1,+ node_id="n",+ node_exec_id="ne",+ ):+ pass+++PNG = b"\x89PNG\r\n\x1a\n" + bytes(64)++[email protected](autouse=True)+def network(monkeypatch) -> list[str]:+ """Every URL that reached the HTTP transport; nothing does in these tests."""+ reached: list[str] = []++ async def no_network(self, method, url, *args, **kwargs):+ reached.append(str(url))+ raise aiohttp.ClientConnectionError("no network in tests")++ monkeypatch.setattr(aiohttp.ClientSession, "_request", no_network)+ return reached+++def workspace(monkeypatch, tmp_path) -> dict[str, tuple[str, bytes]]:+ """Files by id, as (owning workspace, content). The run's workspace is "ws";+ the lookups answer the way the database queries do."""+ files: dict[str, tuple[str, bytes]] = {}++ def record(file_id: str) -> WorkspaceFile:+ now = datetime.now(tz=UTC)+ return WorkspaceFile(+ id=file_id,+ workspace_id=files[file_id][0],+ created_at=now,+ updated_at=now,+ name=f"{file_id}.png",+ path=f"/{file_id}.png",+ storage_path=file_id,+ mime_type="image/png",+ size_bytes=len(files[file_id][1]),+ )++ async def scoped(file_id: str, workspace_id: str):+ owner = files.get(file_id, (None, b""))[0]+ return record(file_id) if owner == workspace_id else None++ async def unscoped(file_id: str):+ return record(file_id) if file_id in files else None++ async def retrieve(storage_path: str) -> bytes:+ return files[storage_path][1]++ db = SimpleNamespace(get_workspace_file=scoped, get_workspace_file_by_id=unscoped)+ storage = SimpleNamespace(retrieve=retrieve)+ monkeypatch.setattr(workspace_module, "workspace_db", lambda: db)+ monkeypatch.setattr(+ workspace_module, "get_workspace_storage", AsyncMock(return_value=storage)+ )+ monkeypatch.setattr(file_module, "scan_content_safe", AsyncMock())+ monkeypatch.setattr(file_module, "TEMP_DIR", tmp_path)+ return files+++async def test_a_workspace_image_is_attached_and_shown_inline(postmark, workspace):+ workspace["chart"] = ("ws", PNG)++ await run({"subject": "s", "body": "Results:\n\n"})++ sent = _sent(postmark)+ assert sent["Attachments"] == [+ {+ "Name": "image-1.png",+ "Content": base64.b64encode(PNG).decode(),+ "ContentType": "image/png",+ "ContentID": "cid:image-1",+ }+ ]+ assert '<img src="cid:image-1" alt="Chart"' in sent["HtmlBody"]+ assert "workspace://" not in sent["HtmlBody"]+++async def test_the_plain_text_part_shows_an_images_alt_text(postmark, workspace):+ workspace["chart"] = ("ws", PNG)++ await run(+ {"subject": "s", "body": "Results:\n\n"}+ )++ text = _sent(postmark)["TextBody"]+ assert "Weekly chart" in text+ assert "cid:" not in text and "workspace://" not in text+++async def test_an_image_in_another_users_workspace_is_never_read(postmark, workspace):+ workspace["theirs"] = ("ws-someone-else", PNG)++ await run({"subject": "s", "body": ""})++ sent = _sent(postmark)+ assert not sent["Attachments"]+ assert "Their chart" in sent["HtmlBody"]+ assert 'src="cid:' not in sent["HtmlBody"]+++async def test_an_image_on_an_internal_address_is_never_fetched(postmark, network):+ await run({"subject": "s", "body": ""})++ sent = _sent(postmark)+ assert not [url for url in network if "127.0.0.1" in url]+ assert not sent["Attachments"]+ assert "Internal" in sent["HtmlBody"]+++ "content",+ [b"<svg onload='steal()'></svg>", b"%PDF-1.7 not an image"],+)+async def test_a_file_that_is_not_an_image_is_left_out(postmark, workspace, content):+ workspace["fake"] = ("ws", content)++ await run({"subject": "s", "body": ""})++ sent = _sent(postmark)+ assert not sent["Attachments"]+ assert "Fake" in sent["HtmlBody"]+++async def test_an_oversized_image_is_left_out_and_its_alt_text_shown(+ monkeypatch, postmark, workspace+):+ monkeypatch.setattr(owner_email_images, "MAX_IMAGE_BYTES", len(PNG) - 1)+ workspace["big"] = ("ws", PNG)++ await run({"subject": "s", "body": ""})++ sent = _sent(postmark)+ assert not sent["Attachments"]+ assert "Big chart" in sent["HtmlBody"]+++ "limit",+ [("MAX_IMAGES", 1), ("MAX_TOTAL_BYTES", len(PNG) + 1)],+)+async def test_images_past_the_count_or_total_size_are_left_out(+ monkeypatch, postmark, workspace, limit+):+ monkeypatch.setattr(owner_email_images, *limit)+ workspace["one"] = ("ws", PNG)+ workspace["two"] = ("ws", PNG)++ await run(+ {"subject": "s", "body": " "}+ )++ sent = _sent(postmark)+ assert [a["ContentID"] for a in sent["Attachments"]] == ["cid:image-1"]+ assert "Two" in sent["HtmlBody"]+++async def test_an_owner_email_is_tagged_and_never_tracked_with_tracking_on(+ monkeypatch, postmark, workspace+):+ """Postmark tracking is on for notification mail, but this mail's links are+ the user's own and can carry their tokens."""+ monkeypatch.setattr(+ email_module.settings.config, "postmark_track_opens_and_clicks", True+ )+ workspace["chart"] = ("ws", PNG)+ body = "[The report](https://docs.example/d/abc?token=s3cret)\n\n"++ await run({"subject": "s", "body": body})++ sent = _sent(postmark)+ assert sent["Tag"] == "owner_email"+ assert sent["TrackOpens"] is False and sent["TrackLinks"] == "None"+ assert [a["ContentID"] for a in sent["Attachments"]] == ["cid:image-1"]+ assert 'href="https://docs.example/d/abc?token=s3cret"' in sent["HtmlBody"]+++def _sent(postmark) -> dict:+ assert len(postmark.emails.send.call_args_list) == 1+ return postmark.emails.send.call_args.kwargs+++def _error(response) -> str:+ if isinstance(response, BlockOutputResponse):+ return " ".join(response.outputs.get("error", []))+ return str(getattr(response, "error", None) or response.message)+++class _FakeRedis:+ def __init__(self):+ self.counts: dict[str, int] = {}+ self.broken = False++ async def incr(self, key: str) -> int:+ if self.broken:+ raise ConnectionError("redis down")+ self.counts[key] = self.counts.get(key, 0) + 1+ return self.counts[key]++ async def decr(self, key: str) -> int:+ self.counts[key] -= 1+ return self.counts[key]++ def pipeline(self, transaction: bool) -> "_FakePipeline":+ assert transaction+ return _FakePipeline(self)+++class _FakePipeline:+ """Only the counter's two commands; the live-Redis test covers their meaning."""++ def __init__(self, redis: _FakeRedis):+ self.redis = redis+ self.keys: list[str] = []++ async def __aenter__(self) -> Self:+ return self++ async def __aexit__(self, *_) -> None:+ pass++ def incr(self, key: str) -> None:+ self.keys.append(key)++ def expire(self, key: str, seconds: int) -> None:+ pass++ async def execute(self) -> list:+ return [await self.redis.incr(key) for key in self.keys] + [True]diff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.pyindex 3b50b452b0..31aa74e2fc 100644--- a/autogpt_platform/backend/backend/copilot/prompting.py+++ b/autogpt_platform/backend/backend/copilot/prompting.py@@ -220,6 +220,10 @@ user): it posts as the AutoGPT bot with no credential into a linked server'schannels or the user's own DMs. For anywhere else, or with nothing linked, usethe user's connected block, or tell them they can link the platform.+To email the user themselves ("email me", a digest to their inbox), use+`block:38997608-a57a-4e11-971c-b83a5332e80b` (SendEmailToMeBlock): it sends to+their account address with no sign-in. Use Gmail only to email someone else.+If `find_capability` without `kind` returns nothing for a named service,`web_search` for "<service> MCP server" and call `run_capability` with the server URL as `id`.Verify the hostname belongs to the vendor first; if several candidates exist,diff --git a/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md b/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.mdindex a1a211ac91..1286c6fd08 100644--- a/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md+++ b/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md@@ -226,6 +226,12 @@ descriptions; read and follow those when `find_capability` surfaces a match.when credentials are missing or invalid, then proceeds to execute onceconnected. Use `run_capability(id="tool:connect_integration", ...)` only for a standalone providersetup not tied to a specific run.+- **Emailing the user**: to deliver to the user's own inbox ("email me the+ results daily"), use `SendEmailToMeBlock` (ID:+ `38997608-a57a-4e11-971c-b83a5332e80b`). It needs no credentials and only+ ever sends to the account's address, so do not ask for one. Use Gmail or+ SMTP only to email someone else. To show an image, put+ `` in its Markdown body.- **Node spacing**: Position nodes with at least 800 X-units between them.- **Nested properties**: Use `parentField_#_childField` notation in linksink_name/source_name to access nested object fields.diff --git a/autogpt_platform/backend/backend/notifications/email.py b/autogpt_platform/backend/backend/notifications/email.pyindex 733345f0ae..9c03d55634 100644--- a/autogpt_platform/backend/backend/notifications/email.py+++ b/autogpt_platform/backend/backend/notifications/email.py@@ -6,12 +6,14 @@ changelog are MailerLite's). Every message carries a plain-text MIME part builtfrom the same data model. Product notifications carry one-clickList-Unsubscribe headers; billing, auth and internal Ops mail do not.-Each message is tagged with what it is (the notification type, or ``auth``).+Each message is tagged with what it is (the notification type, ``auth`` or+``owner_email``).Postmark echoes the tag in its delivery, open, click and bounce webhooks,which ``api/features/postmark`` turns into PostHog events. Every message alsosays whether Postmark tracks its opens and clicks, so the stream's own defaultnever decides: notification mail follows ``postmark_track_opens_and_clicks``,-and auth mail is never tracked, since its links carry sign-in tokens.+and auth mail is never tracked, since its links carry sign-in tokens, nor is+mail a user's run sends them, whose links are their own."""import asyncio@@ -39,6 +41,8 @@ POSTMARK_TIMEOUT_SECONDS = 30# Postmark tag for Better Auth's emails, which are not notifications.AUTH_EMAIL_TAG = "auth"+# Postmark tag for SendEmailToMeBlock's emails to the account owner.+OWNER_EMAIL_TAG = "owner_email"class TypedPostmarkClient(PostmarkClient):@@ -128,6 +132,30 @@ class EmailSender:**_tracking(False),)+ async def send_owner_email(+ self,+ user_email: str,+ email: RenderedEmail,+ attachments: list[dict[str, str]],+ ) -> None:+ """Raises when Postmark is not configured: the run that asked for this+ reports a failure instead of a send that never happened."""+ if not self.postmark:+ raise RuntimeError("Email sending isn't set up on this server.")+ await self._send(+ user_email=user_email,+ tag=OWNER_EMAIL_TAG,+ sender=_sender_for(DeliveryStream.PRODUCT),+ reply_to=_reply_to_for(DeliveryStream.PRODUCT),+ subject=email.subject,+ html_body=email.html,+ text_body=email.text,+ headers=None,+ attachments=attachments,+ # Its links are the user's own, and can carry their tokens.+ tracked=False,+ )+async def _send(self,user_email: str,@@ -138,6 +166,8 @@ class EmailSender:text_body: str,headers: dict[str, str] | None,reply_to: str | None = None,+ attachments: list[dict[str, str]] | None = None,+ tracked: bool = True,) -> None:if not self.postmark:logger.warning("Email tried to send without Postmark configured")@@ -155,8 +185,9 @@ class EmailSender:TextBody=text_body,MessageStream=settings.config.postmark_transactional_stream,Headers=headers,+ Attachments=attachments,Tag=tag,- **_tracking(settings.config.postmark_track_opens_and_clicks),+ **_tracking(tracked and settings.config.postmark_track_opens_and_clicks),)diff --git a/autogpt_platform/backend/backend/notifications/notifications.py b/autogpt_platform/backend/backend/notifications/notifications.pyindex 02780965c7..98a76de585 100644--- a/autogpt_platform/backend/backend/notifications/notifications.py+++ b/autogpt_platform/backend/backend/notifications/notifications.py@@ -35,10 +35,11 @@ from backend.data.user import (generate_preference_link,generate_unsubscribe_link,)-from backend.notifications import briefing_runner, mailerlite+from backend.notifications import briefing_runner, mailerlite, owner_emailfrom backend.notifications.consent import log_kept_out_skipfrom backend.notifications.dedupe import claim_daily_sendfrom backend.notifications.email import EmailSender+from backend.notifications.owner_email_body import BodyFormat, InlineImagefrom backend.notifications.preferences import SERVICE_MESSAGES, wants_notificationfrom backend.notifications.queue import (AUDIENCE_QUEUE,@@ -58,6 +59,7 @@ from backend.util.service import (AppService,AppServiceClient,UnhealthyServiceError,+ endpoint_to_async,endpoint_to_sync,expose,)@@ -284,6 +286,25 @@ class NotificationManager(AppService):self.email_sender.send_email_or_raise, to, subject, body, text_body)+ @expose+ async def send_owner_email(+ self,+ user_id: str,+ subject: str,+ body: str,+ link: str | None = None,+ body_format: BodyFormat = BodyFormat.MARKDOWN,+ images: list[InlineImage] | None = None,+ ) -> None:+ """Email a message one of the user's own runs composed to that user's+ own address. Not gated by ENABLE_USER_NOTIFICATIONS: like the auth mail+ above, the user asked for it."""+ await owner_email.send_owner_email(+ self.email_sender,+ user_id,+ owner_email.parse_owner_email(subject, body, link, body_format, images),+ )+# ── consumers ───────────────────────────────────────────────────────async def _process_user_notification(self, message: str) -> bool:@@ -821,3 +842,4 @@ class NotificationManagerClient(AppServiceClient):send_due_briefings = endpoint_to_sync(NotificationManager.send_due_briefings)discord_system_alert = endpoint_to_sync(NotificationManager.discord_system_alert)send_email_or_raise = endpoint_to_sync(NotificationManager.send_email_or_raise)+ send_owner_email = endpoint_to_async(NotificationManager.send_owner_email)diff --git a/autogpt_platform/backend/backend/notifications/owner_email.py b/autogpt_platform/backend/backend/notifications/owner_email.pynew file mode 100644index 0000000000..46f45c17c7--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/owner_email.py@@ -0,0 +1,204 @@+"""Email the account owner a message one of their own runs composed.++The recipient is never a parameter: it is the account's verified address, read+here from the user id, so nothing upstream of this call can redirect it.+"""++import asyncio+import logging+from collections.abc import Awaitable+from datetime import UTC, datetime+from typing import Any, cast++from postmarker.exceptions import ClientError+from pydantic import BaseModel, Field, ValidationError, field_validator+from requests import RequestException++from backend.data.redis_client import get_redis_async+from backend.notifications.email import EmailSender+from backend.notifications.owner_email_body import (+ MAX_IMAGES,+ BodyFormat,+ InlineImage,+ render_body,+)+from backend.notifications.renderer import RenderedEmail, build_urls+from backend.notifications.template_env import create_body_environment+from backend.util.clients import get_database_manager_async_client+from backend.util.settings import Settings++logger = logging.getLogger(__name__)+settings = Settings()++_html_env = create_body_environment(autoescape=True)+_text_env = create_body_environment(autoescape=False)+# Counters outlive the UTC day they cover, so a send at 23:59 is still counted.+_COUNTER_TTL_SECONDS = 60 * 60 * 48+++class OwnerRenderedEmail(RenderedEmail):+ attachments: list[dict[str, str]] = Field(default_factory=list)+++class OwnerEmail(BaseModel):+ subject: str = Field(min_length=1, max_length=200)+ body: str = Field(min_length=1, max_length=50_000)+ link: str | None = None+ body_format: BodyFormat = BodyFormat.MARKDOWN+ images: list[InlineImage] = Field(default_factory=list, max_length=MAX_IMAGES)++ # Before the length check, so a subject of only whitespace is refused.+ @field_validator("subject", mode="before")+ @classmethod+ def _one_line(cls, subject: object) -> object:+ return " ".join(subject.split()) if isinstance(subject, str) else subject++ @field_validator("body")+ @classmethod+ def _not_blank(cls, body: str) -> str:+ if not body.strip():+ raise ValueError("The body is empty.")+ return body++ @field_validator("link")+ @classmethod+ def _web_link(cls, link: str | None) -> str | None:+ if link and not link.startswith(("https://", "http://")):+ raise ValueError("The link must be a web address (https:// or http://).")+ return link or None+++def parse_owner_email(+ subject: str,+ body: str,+ link: str | None,+ body_format: BodyFormat,+ images: list[InlineImage] | None = None,+) -> OwnerEmail:+ """A ValidationError's type is not one the RPC client can rebuild, so it+ crosses as the ValueError the block reports."""+ try:+ return OwnerEmail(+ subject=subject,+ body=body,+ link=link,+ body_format=body_format,+ images=images or [],+ )+ except ValidationError as e:+ raise ValueError(+ "; ".join(+ f"{'.'.join(map(str, err['loc']))}: "+ + err["msg"].removeprefix("Value error, ")+ for err in e.errors()+ )+ ) from e+++async def send_owner_email(+ email_sender: EmailSender, user_id: str, message: OwnerEmail+) -> None:+ """Raises ValueError when the send is refused and TimeoutError when its+ outcome is unknown, so the caller can say which."""+ db = get_database_manager_async_client(should_retry=False)+ address = await db.get_user_email_by_id(user_id)+ if not address:+ raise ValueError("This account has no email address to send to.")+ if not await db.get_user_email_verification(user_id):+ raise ValueError(+ "This account's email address isn't verified yet, so nothing can be "+ "sent to it. Verify it from the sign-in email, then try again."+ )++ # Off the event loop: rendering a long body takes a CPU-bound tenth of a second.+ email = await asyncio.to_thread(render_owner_email, message)+ counter = f"owner_email:daily:{user_id}:{_utc_today()}"+ limit = settings.config.owner_email_daily_limit+ if not await _take_daily_send(counter, limit):+ raise ValueError(+ f"Already sent {limit} emails to this account today, the most "+ "allowed in a day. Try again after midnight UTC."+ )+ # Only a send that certainly went nowhere gives its slot back: one that+ # timed out may have arrived, and refunding it lets a looping run past the cap.+ try:+ await email_sender.send_owner_email(address, email, email.attachments)+ except ClientError as e:+ await _give_back(counter)+ raise ValueError(f"The email service refused this email: {e}") from e+ except RuntimeError:+ await _give_back(counter)+ raise+ except RequestException as e:+ raise TimeoutError(+ "The email service didn't confirm the send, so it may still arrive. "+ "Check your inbox before sending it again."+ ) from e+ logger.info(f"Sent an owner email for user {user_id}")+++def render_owner_email(message: OwnerEmail) -> OwnerRenderedEmail:+ content_ids = {+ image.source: f"image-{n}" for n, image in enumerate(message.images, 1)+ }+ body = render_body(message.body, message.body_format, content_ids)+ context = {+ "subject": message.subject,+ "content": body.html,+ "content_text": body.text,+ "link": message.link,+ "assets": settings.config.email_asset_base_url.rstrip("/"),+ "urls": build_urls(""),+ }+ return OwnerRenderedEmail(+ subject=message.subject,+ preheader="",+ html=_html_env.get_template("owner.html.j2").render(**context),+ text=_text_env.get_template("owner.txt.j2").render(**context).strip(),+ attachments=[+ {+ "Name": f"{content_ids[image.source]}.{image.content_type[6:]}",+ "Content": image.data,+ "ContentType": image.content_type,+ "ContentID": f"cid:{content_ids[image.source]}",+ }+ for image in message.images+ ],+ )+++def _utc_today() -> str:+ return datetime.now(tz=UTC).date().isoformat()+++async def _take_daily_send(counter: str, limit: int) -> bool:+ """Fails closed, unlike the notification cap: this one exists to stop a+ looping run, and a run can retry where a lost payment notice cannot."""+ try:+ redis_client = await get_redis_async()+ async with redis_client.pipeline(transaction=True) as pipe:+ pipe.incr(counter)+ pipe.expire(counter, _COUNTER_TTL_SECONDS)+ sent, _ = await cast(Awaitable[list[Any]], pipe.execute())+ except Exception as e:+ raise RuntimeError(+ "Couldn't check today's email count, so nothing was sent. "+ "Try again in a minute."+ ) from e+ if sent <= limit:+ return True+ # A refused send gives its slot back too, or a later failed send's give-back+ # leaves the count above what was really sent.+ await _give_back(counter)+ return False+++async def _give_back(counter: str) -> None:+ try:+ redis_client = await get_redis_async()+ await redis_client.decr(counter)+ except Exception:+ logger.warning(+ "Could not return a failed owner email to the daily count",+ exc_info=True,+ )diff --git a/autogpt_platform/backend/backend/notifications/owner_email_body.py b/autogpt_platform/backend/backend/notifications/owner_email_body.pynew file mode 100644index 0000000000..4d46a3f2ad--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/owner_email_body.py@@ -0,0 +1,289 @@+"""Turns the body a run wrote into its email's HTML and plain-text parts.++Every format ends as the same small set of tags, each styled inline, so all+three look alike in the template and no mail client needs a stylesheet. An+image is shown only as an attachment of the email itself (``cid:``): the mail+client never fetches anything the body names.+"""++import html+import re+from collections.abc import Mapping+from enum import Enum+from html.parser import HTMLParser+from typing import Literal++import bleach+import html2text+from markdown_it import MarkdownIt+from markupsafe import Markup+from pydantic import BaseModel, ConfigDict+++class BodyFormat(str, Enum):+ PLAIN = "plain"+ MARKDOWN = "markdown"+ HTML = "html"+++ImageType = Literal["image/png", "image/jpeg", "image/gif", "image/webp"]+MAX_IMAGES = 8+++class InlineImage(BaseModel):+ source: str+ content_type: ImageType+ data: str # base64+++class RenderedBody(BaseModel):+ model_config = ConfigDict(arbitrary_types_allowed=True)++ html: Markup+ text: str+++def render_body(+ body: str, body_format: BodyFormat, images: Mapping[str, str] | None = None+) -> RenderedBody:+ """``images`` maps an image source in the body to the content id of the+ attachment carrying it; any other image is shown as its alt text."""+ if body_format is BodyFormat.PLAIN:+ return RenderedBody(html=Markup(_restyle(_plain_to_html(body), {})), text=body)+ clean = _restyle(_sanitize(_to_html(body, body_format)), images or {})+ return RenderedBody(html=Markup(clean), text=_html_to_text(clean))+++def image_sources(body: str, body_format: BodyFormat) -> list[str]:+ """The fetchable image sources a body names, in order, each once."""+ if body_format is BodyFormat.PLAIN:+ return []+ collector = _ImageSources()+ collector.feed(_sanitize(_to_html(body, body_format)))+ collector.close()+ return list(dict.fromkeys(collector.sources))+++ALLOWED_TAGS = frozenset(+ {"a", "b", "blockquote", "br", "code", "del", "div", "em", "hr", "i", "li", "ol"}+ | {"h1", "h2", "h3", "h4", "h5", "h6", "p", "pre", "s", "span", "strong", "u"}+ | {"img", "table", "tbody", "td", "th", "thead", "tr", "ul"}+)+# No style, class or id: the template styles every tag itself.+ALLOWED_ATTRIBUTES = {+ "a": ["href", "title"],+ "img": ["src", "alt"],+ "td": ["colspan", "rowspan"],+ "th": ["colspan", "rowspan"],+}+ALLOWED_PROTOCOLS = frozenset({"http", "https", "mailto", "workspace", "data"})+LINK_SCHEMES = ("http://", "https://", "mailto:")+IMAGE_SCHEMES = ("https://", "workspace://", "data:image/")+# bleach strips a disallowed tag but keeps its text, which for these is code.+_DROPPED_TAGS = (+ "script",+ "style",+ "head",+ "title",+ "template",+ "noscript",+ "iframe",+ "object",+ "svg",+ "math",+ "form",+ "button",+ "select",+ "textarea",+)+# ASCII-only, like HTML's own tag names, so "ſcript" isn't read as "script".+_DROPPED_WITH_CONTENT = re.compile(+ rf"<({'|'.join(_DROPPED_TAGS)})\b", re.IGNORECASE | re.ASCII+)+_CLOSING_TAGS = {+ name: re.compile(rf"</{name}\b", re.IGNORECASE | re.ASCII) for name in _DROPPED_TAGS+}+_VOID_TAGS = frozenset(+ {"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta"}+ | {"param", "source", "track", "wbr"}+)+# Opening one of these closes its open sibling, so they never stack up.+_CLOSED_BY_SIBLINGS = frozenset({"p", "li", "dt", "dd", "tr", "td", "th", "option"})+MAX_NESTING = 100++_BODY = "font-size:15px; line-height:24px; color:#2D2A3E;"+_STYLES = {+ "p": f"margin:0 0 16px; {_BODY}",+ "div": _BODY,+ "h1": "margin:24px 0 10px; font-size:22px; line-height:30px; font-weight:700; color:#171925;",+ "h2": "margin:22px 0 8px; font-size:19px; line-height:27px; font-weight:700; color:#171925;",+ "h3": "margin:20px 0 8px; font-size:17px; line-height:25px; font-weight:700; color:#171925;",+ "h4": "margin:18px 0 6px; font-size:15px; line-height:23px; font-weight:700; color:#171925;",+ "h5": "margin:18px 0 6px; font-size:15px; line-height:23px; font-weight:700; color:#171925;",+ "h6": "margin:18px 0 6px; font-size:15px; line-height:23px; font-weight:700; color:#575072;",+ "a": "color:#4D36AD; text-decoration:underline;",+ "ul": f"margin:0 0 16px; padding-left:22px; {_BODY}",+ "ol": f"margin:0 0 16px; padding-left:22px; {_BODY}",+ "li": "margin:0 0 6px;",+ "blockquote": "margin:0 0 16px; padding:2px 0 2px 14px; border-left:3px solid #E7E4F4; color:#575072;",+ "pre": "margin:0 0 16px; padding:12px 14px; background-color:#F5F3FC; border-radius:8px; white-space:pre-wrap; word-break:break-word; font-family:'Geist Mono', ui-monospace, Menlo, Consolas, monospace; font-size:13px; line-height:20px; color:#171925;",+ "code": "padding:1px 4px; background-color:#F5F3FC; border-radius:4px; font-family:'Geist Mono', ui-monospace, Menlo, Consolas, monospace; font-size:13px; color:#171925;",+ "hr": "margin:20px 0; border:0; border-top:1px solid #E7E4F4;",+ "img": "display:block; max-width:100%; height:auto; margin:0 0 16px; border:0; border-radius:8px;",+ "table": "margin:0 0 16px; width:100%; border-collapse:collapse; font-size:14px; line-height:20px; color:#2D2A3E;",+ "th": "padding:7px 9px; border:1px solid #E7E4F4; background-color:#F5F3FC; text-align:left; vertical-align:top; font-weight:700;",+ "td": "padding:7px 9px; border:1px solid #E7E4F4; text-align:left; vertical-align:top;",+}+++def _to_html(body: str, body_format: BodyFormat) -> str:+ return _MARKDOWN.render(body) if body_format is BodyFormat.MARKDOWN else body+++def _plain_to_html(body: str) -> str:+ paragraphs = [p.strip() for p in body.split("\n\n") if p.strip()]+ return "".join(+ "<p>" + "<br>".join(html.escape(line) for line in p.splitlines()) + "</p>"+ for p in paragraphs+ )+++def _sanitize(fragment: str) -> str:+ fragment = _drop_with_content(fragment)+ _refuse_deep_nesting(fragment)+ return bleach.clean(+ fragment,+ tags=ALLOWED_TAGS,+ attributes=ALLOWED_ATTRIBUTES,+ protocols=ALLOWED_PROTOCOLS,+ strip=True,+ strip_comments=True,+ )+++def _refuse_deep_nesting(fragment: str) -> None:+ """bleach's parser slows with the square of the nesting depth (20,000 nested+ divs took 13 s), so count it first with a linear parser that reads quoted+ attribute values the way bleach's does."""+ counter = _NestingCounter()+ counter.feed(fragment)+ counter.close()+++def _drop_with_content(fragment: str) -> str:+ """Removes each dropped element with everything inside it, in one forward+ scan. An unclosed one takes the rest of the body, as an unclosed script does."""+ kept: list[str] = []+ position = 0+ while opening := _DROPPED_WITH_CONTENT.search(fragment, position):+ kept.append(fragment[position : opening.start()])+ closing_tag = _CLOSING_TAGS[opening.group(1).lower()]+ closing = closing_tag.search(fragment, opening.end())+ end = fragment.find(">", closing.end()) if closing else -1+ if end == -1:+ return "".join(kept)+ position = end + 1+ kept.append(fragment[position:])+ return "".join(kept)+++def _restyle(clean: str, images: Mapping[str, str]) -> str:+ """Re-serializes sanitized HTML from its parsed tags, escaping every+ attribute, so a ``>`` inside a value can't split a tag."""+ writer = _Restyler(images)+ writer.feed(clean)+ writer.close()+ return "".join(writer.out)+++def _html_to_text(clean: str) -> str:+ converter = html2text.HTML2Text()+ converter.body_width = 0+ converter.images_to_alt = True+ return converter.handle(clean).strip()+++class _Restyler(HTMLParser):+ def __init__(self, images: Mapping[str, str]):+ super().__init__(convert_charrefs=True)+ self.images = images+ self.out: list[str] = []++ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:+ values = {name: value or "" for name, value in attrs}+ if tag == "img":+ self._image(values)+ return+ if tag == "a" and not values.get("href", "").startswith(LINK_SCHEMES):+ values.pop("href", None)+ if tag in _STYLES:+ values["style"] = _STYLES[tag]+ attributes = "".join(+ f' {name}="{html.escape(value, quote=True)}"'+ for name, value in values.items()+ )+ self.out.append(f"<{tag}{attributes}>")++ def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:+ self.handle_starttag(tag, attrs)++ def handle_endtag(self, tag: str) -> None:+ if tag not in _VOID_TAGS:+ self.out.append(f"</{tag}>")++ def handle_data(self, data: str) -> None:+ self.out.append(html.escape(data, quote=False))++ def _image(self, values: dict[str, str]) -> None:+ alt = values.get("alt", "")+ content_id = self.images.get(values.get("src", ""))+ if content_id is None:+ self.out.append(html.escape(alt, quote=False))+ return+ self.out.append(+ f'<img src="cid:{html.escape(content_id, quote=True)}" '+ f'alt="{html.escape(alt, quote=True)}" style="{_STYLES["img"]}">'+ )+++class _NestingCounter(HTMLParser):+ """Follows what bleach's parser does: ``<div/>`` opens a div, and an end tag+ closes only an element that is open."""++ def __init__(self):+ super().__init__(convert_charrefs=False)+ self.open: list[str] = []++ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:+ if tag in _VOID_TAGS or tag in _CLOSED_BY_SIBLINGS:+ return+ self.open.append(tag)+ if len(self.open) > MAX_NESTING:+ raise ValueError(f"The HTML is nested more than {MAX_NESTING} levels deep.")++ def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:+ self.handle_starttag(tag, attrs)++ def handle_endtag(self, tag: str) -> None:+ # At most MAX_NESTING entries, so the search stays cheap.+ if tag in self.open:+ while self.open.pop() != tag:+ pass+++class _ImageSources(HTMLParser):+ def __init__(self):+ super().__init__(convert_charrefs=True)+ self.sources: list[str] = []++ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:+ src = dict(attrs).get("src") or ""+ if tag == "img" and src.startswith(IMAGE_SCHEMES):+ self.sources.append(src)+++# Inline HTML goes through the same sanitizer as an HTML body, as on GitHub;+# breaks keep the line breaks the writer typed.+_MARKDOWN = MarkdownIt("commonmark", {"html": True, "breaks": True}).enable(+ ["table", "strikethrough"]+)diff --git a/autogpt_platform/backend/backend/notifications/owner_email_images.py b/autogpt_platform/backend/backend/notifications/owner_email_images.pynew file mode 100644index 0000000000..42860d27ca--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/owner_email_images.py@@ -0,0 +1,76 @@+"""Fetches the images an owner email's body names, so they travel as+attachments of the email itself.++Runs where the block runs: a ``workspace://`` image is read with the run's own+workspace scope, and an ``https://`` one through the request guard that refuses+private and internal addresses. An image that can't be used is left out, and+the body shows its alt text instead.+"""++import base64+import logging+from typing import TYPE_CHECKING++from backend.notifications.owner_email_body import MAX_IMAGES, ImageType, InlineImage+from backend.util.file import parse_data_uri, store_media_file+from backend.util.type import MediaFileType++if TYPE_CHECKING:+ from backend.data.execution import ExecutionContext++logger = logging.getLogger(__name__)++MAX_IMAGE_BYTES = 2 * 1024 * 1024+# Base64 grows it by a third, which keeps a full email well under Postmark's 10 MB.+MAX_TOTAL_BYTES = 4 * 1024 * 1024+++async def fetch_images(+ sources: list[str], execution_context: "ExecutionContext"+) -> list[InlineImage]:+ images: list[InlineImage] = []+ total = 0+ for source in sources[:MAX_IMAGES]:+ content = await _read(source, execution_context)+ content_type = sniff_image_type(content) if content else None+ if content is None or content_type is None:+ continue+ if len(content) > MAX_IMAGE_BYTES or total + len(content) > MAX_TOTAL_BYTES:+ logger.info(f"Leaving out an email image of {len(content)} bytes")+ continue+ total += len(content)+ images.append(+ InlineImage(+ source=source,+ content_type=content_type,+ data=base64.b64encode(content).decode("ascii"),+ )+ )+ return images+++def sniff_image_type(content: bytes) -> ImageType | None:+ """The type the bytes are, whatever the file's name or header claims."""+ if content.startswith(b"\x89PNG\r\n\x1a\n"):+ return "image/png"+ if content.startswith(b"\xff\xd8\xff"):+ return "image/jpeg"+ if content.startswith((b"GIF87a", b"GIF89a")):+ return "image/gif"+ if content[:4] == b"RIFF" and content[8:12] == b"WEBP":+ return "image/webp"+ return None+++async def _read(source: str, execution_context: "ExecutionContext") -> bytes | None:+ try:+ data_uri = await store_media_file(+ MediaFileType(source), execution_context, return_format="for_external_api"+ )+ # Missing, refused, blocked, unreachable or infected: the image is left out+ # either way, and the email still goes.+ except Exception as e:+ logger.info(f"Leaving out an email image that couldn't be read: {e}")+ return None+ parsed = parse_data_uri(data_uri)+ return base64.b64decode(parsed[1]) if parsed else Nonediff --git a/autogpt_platform/backend/backend/notifications/owner_email_test.py b/autogpt_platform/backend/backend/notifications/owner_email_test.pynew file mode 100644index 0000000000..651c334c83--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/owner_email_test.py@@ -0,0 +1,33 @@+"""The owner-email day counter on a real Redis: what the test double assumes."""++import uuid++import pytest++from backend.data import redis_client+from backend.data.redis_client import get_redis_async+from backend.notifications import owner_email+from backend.util.testing import is_tcp_port_reachable++pytestmark = pytest.mark.skipif(+ not is_tcp_port_reachable(redis_client.HOST, redis_client.PORT),+ reason="needs Redis",+)+++async def test_the_day_count_stops_at_the_limit_and_expires():+ counter = f"owner_email:daily:test-{uuid.uuid4()}:2026-10-21"+ redis = await get_redis_async()+ try:+ taken = [await owner_email._take_daily_send(counter, 2) for _ in range(3)]+ count = int(await redis.get(counter))+ ttl = await redis.ttl(counter)+ await owner_email._give_back(counter)+ after_give_back = await owner_email._take_daily_send(counter, 2)+ finally:+ await redis.delete(counter)++ assert taken == [True, True, False]+ assert count == 2+ assert 0 < ttl <= owner_email._COUNTER_TTL_SECONDS+ assert after_give_backdiff --git a/autogpt_platform/backend/backend/notifications/templates/owner.html.j2 b/autogpt_platform/backend/backend/notifications/templates/owner.html.j2new file mode 100644index 0000000000..98d475b287--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/templates/owner.html.j2@@ -0,0 +1,26 @@+{% import '_email_ui.j2' as ui with context %}+<!doctype html>+<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">+<head>{{ ui.head_block(subject) }}</head>+{{ ui.body_open() }}+{{ ui.preheader('') }}+{{ ui.page_open() }}+{{ ui.masthead() }}+{{ ui.sheet_open() }}+{% call ui.eyebrow(top=28) %}FROM OTTO{% endcall %}+{% call ui.headline() %}{{ subject }}{% endcall %}+ <tr><td class="gutter" style="padding:18px 48px 0;">+ <div style="font-size:15px; line-height:24px; color:#2D2A3E; overflow-wrap:anywhere; word-break:break-word;">{{ content }}</div>+ </td></tr>+{% if link %}+{{ ui.cta('Open the link', link, micro='Button not working? Copy this link into your browser:') }}+{% call ui.para(top=6, size=12, lh=18) %}+<a href="{{ link }}" style="color:#4D36AD; text-decoration:underline; font-family:{{ ui.MONO }}; font-size:12px; overflow-wrap:anywhere; word-break:break-word;">{{ link }}</a>+{% endcall %}+{% endif %}+{{ ui.close_row("You asked Otto, or one of your workflows, to email you this. To stop a scheduled one, turn off its schedule.", 'Your workflows', urls.dashboard) }}+{{ ui.sheet_close() }}+{{ ui.page_close() }}+{{ ui.legal_footer("Sent to your AutoGPT account address at your request.", 'Discord', urls.discord) }}+</td></tr></table>+</body></html>diff --git a/autogpt_platform/backend/backend/notifications/templates/owner.txt.j2 b/autogpt_platform/backend/backend/notifications/templates/owner.txt.j2new file mode 100644index 0000000000..5b4033f724--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/templates/owner.txt.j2@@ -0,0 +1,15 @@+{{ subject }}++{{ content_text }}++{% if link %}+{{ link }}++{% endif %}+You asked Otto, or one of your workflows, to email you this. To stop a scheduled one, turn off its schedule.+Your workflows: {{ urls.dashboard }}++Sent to your AutoGPT account address at your request.+AutoGPT, 3rd Floor, 1 Ashley Road, Altrincham, WA14 2DT, UK+https://agpt.co+Discord: {{ urls.discord }}diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.pyindex 76a214f31b..32de8b0ebc 100644--- a/autogpt_platform/backend/backend/util/settings.py+++ b/autogpt_platform/backend/backend/util/settings.py@@ -692,6 +692,13 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):"deferred. Auth emails and internal ops mail are unaffected."),)+ owner_email_daily_limit: int = Field(+ default=20,+ description=(+ "Most emails a user's own runs may send to that user's account "+ "address in a UTC day, through SendEmailToMeBlock."+ ),+ )# Separated so each kind carries its own reputation. Marketing mail goes# from MailerLite as [email protected] and has no sender here.diff --git a/autogpt_platform/backend/poetry.lock b/autogpt_platform/backend/poetry.lockindex 6ca64ec8a5..f719d0918e 100644--- a/autogpt_platform/backend/poetry.lock+++ b/autogpt_platform/backend/poetry.lock@@ -8857,4 +8857,4 @@ cffi = ["cffi (>=1.17,<2.0) ; platform_python_implementation != \"PyPy\" and pyt[metadata]lock-version = "2.1"python-versions = ">=3.11,<3.14"-content-hash = "24f4d7c6c84b5c816735bbd6ac2284414e88cc54de543ef97f194c872ede7160"+content-hash = "7f6c38e2efbf79053dbb4db916bc71b8c8fddcf557824a43459dcf77ec33f124"diff --git a/autogpt_platform/backend/pyproject.toml b/autogpt_platform/backend/pyproject.tomlindex 45ea5d4a55..f7281968d6 100644--- a/autogpt_platform/backend/pyproject.toml+++ b/autogpt_platform/backend/pyproject.toml@@ -44,6 +44,7 @@ jsonref = "^1.1.0"jsonschema = "^4.25.0"langfuse = "^3.14.1"launchdarkly-server-sdk = "^9.14.1"+markdown-it-py = "^4.0.0"mem0ai = "^0.1.115"# Pinned to an upstream master commit until a moviepy release allows pillow 13# (every release up to 2.2.1 caps pillow below 12). Switch back to the releasediff --git a/docs/integrations/README.md b/docs/integrations/README.mdindex 0fee91773f..53f46a0399 100644--- a/docs/integrations/README.md+++ b/docs/integrations/README.md@@ -530,6 +530,7 @@ Below is a comprehensive list of all available blocks, categorized by their prim| [Gmail Reply](block-integrations/google/gmail.md#gmail-reply) | Reply to Gmail threads with automatic HTML detection and proper text formatting || [Gmail Send](block-integrations/google/gmail.md#gmail-send) | Send emails via Gmail with automatic HTML detection and proper text formatting || [Hub Spot Engagement](block-integrations/hubspot/engagement.md#hub-spot-engagement) | Manages HubSpot engagements - sends emails and tracks engagement metrics |+| [Send Email To Me](block-integrations/communication.md#send-email-to-me) | Email yourself: sends a message to your own inbox, at your account's email address, from the platform, with no email account to sign in to |## Developer Toolsdiff --git a/docs/integrations/SUMMARY.md b/docs/integrations/SUMMARY.mdindex 70f2816d69..6322fc8b5a 100644--- a/docs/integrations/SUMMARY.md+++ b/docs/integrations/SUMMARY.md@@ -47,6 +47,7 @@* [Baas Bots](block-integrations/baas/bots.md)* [Bannerbear Text Overlay](block-integrations/bannerbear/text_overlay.md)* [Basic](block-integrations/basic.md)+* [Communication](block-integrations/communication.md)* [Compass Triggers](block-integrations/compass/triggers.md)* [Conductor Account](block-integrations/conductor/account.md)* [Conductor Create Session](block-integrations/conductor/create_session.md)diff --git a/docs/integrations/block-integrations/communication.md b/docs/integrations/block-integrations/communication.mdnew file mode 100644index 0000000000..c71d12ab4c--- /dev/null+++ b/docs/integrations/block-integrations/communication.md@@ -0,0 +1,41 @@+# Communication+<!-- MANUAL: file_description -->+Blocks that send messages from the platform itself, with no account of your own to connect.+<!-- END MANUAL -->++## Send Email To Me++### What it is+Email yourself: sends a message to your own inbox, at your account's email address, from the platform, with no email account to sign in to. Use it for "email me" or "send it to myself": a result, a summary or a recurring digest. It can't send to anyone else; to email other people, use Gmail or SMTP.++### How it works+<!-- MANUAL: how_it_works -->+The block hands the subject, body, body format and optional link to the platform's notification service. The body is written in Markdown (the default), plain text or HTML, and all three render into the same AutoGPT email, with a plain-text copy for mail apps that don't show HTML. Markdown and HTML are cleaned first: scripts, forms and inline styles are removed, and links must be web or mailto addresses. Images (`` or an `https://` URL in Markdown, `<img>` in HTML) are fetched once when the email is sent and attached to it, so the mail app never fetches anything. An image that can't be read, isn't a PNG, JPEG, GIF or WebP, or is too big is left out, and its alt text shows instead. The service looks up the email address of the account the workflow or chat is running for, checks that the address is verified and that today's limit (20 emails a day by default) isn't reached, and sends the email through the platform's mail provider. There is no recipient input, so the email can only reach the account owner.++A refusal (an unverified address, the daily limit, or no mail set up on the server) comes back on the `error` output.+<!-- END MANUAL -->++### Inputs++| Input | Description | Type | Required |+|-------|-------------|------|----------|+| subject | Subject of the email | str | Yes |+| body | Body of the email, written in the chosen format. Images ( or an https:// URL in Markdown, <img> in HTML) are attached to the email. | str | Yes |+| body_format | How the body is written: Markdown, plain text, or HTML (scripts, forms and styles are removed) | "plain" \| "markdown" \| "html" | No |+| link | Optional web link to show as a button, e.g. to the full result | str | No |++### Outputs++| Output | Description | Type |+|--------|-------------|------|+| error | Error message if the operation failed | str |+| status | Status of the email sending operation | str |++### Possible use case+<!-- MANUAL: use_case -->+**Weekly digest**: a scheduled workflow researches the week's news every Monday and emails you the summary, with no Google account connected.++**Result by email**: you ask Otto to "email me the result", and the finished report lands in your inbox.+<!-- END MANUAL -->++---