Skip to content

feat: add FastMail.get_message() to return the prepared message without sending#322

Open
tungyhardDevOps wants to merge 1 commit into
sabuhish:masterfrom
tungyhardDevOps:feat/expose-prepared-message-234
Open

feat: add FastMail.get_message() to return the prepared message without sending#322
tungyhardDevOps wants to merge 1 commit into
sabuhish:masterfrom
tungyhardDevOps:feat/expose-prepared-message-234

Conversation

@tungyhardDevOps

Copy link
Copy Markdown

Closes #234

Motivation

#234 asks for a way to sign outgoing mail (e.g. S/MIME) — or, as the issue itself suggests, simply to "get the prepared message" so it can be signed and sent externally. Today the fully-built email.message.Message is only created inside the private send pipeline, so there is no supported way to obtain it without sending.

Change

Adds a public async method FastMail.get_message(...) that runs the same preparation pipeline as send_message (template rendering, sender resolution, MIME construction) but returns the prepared email.message.Message instead of delivering it. It reuses the existing internal __prepare_messages_for_sending, so behaviour stays identical to send_message.

  • Same arguments as send_message (message, template_name, html_template, plain_template).
  • A single MessageSchema returns one message; a list returns a list.
  • Opens no SMTP connection and dispatches nothing.

This unblocks the S/MIME / DKIM signing use case without fastapi-mail taking on a cryptography dependency — callers sign/post-process the returned message and send it via their own transport.

Tests

tests/test_get_message.py (3 tests, all passing):

  • single message → returns a prepared Message with correct headers/body, and asserts nothing is dispatched (record_messages outbox stays empty);
  • list input → returns a list of prepared messages;
  • templated message → template is rendered into the returned message.

Existing suite still green (tests/test_connection.py: 22 passed). black, isort, flake8 clean; mypy clean on fastmail.py.

Docs

Documented the new method in docs/getting-started.md.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Signing emails

1 participant