Repository: lavr/python-emails Branch: master Commit: 3a5a55c390de Files: 91 Total size: 296.2 KB Directory structure: gitextract_9o3m1iyk/ ├── .coveragerc ├── .github/ │ └── workflows/ │ ├── python-publish.yml │ └── tests.yaml ├── .gitignore ├── .pre-commit-config.yaml ├── .readthedocs.yaml ├── AUTHORS.rst ├── CHANGELOG.md ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.rst ├── docs/ │ ├── Makefile │ ├── _static/ │ │ └── .gitkeep │ ├── advanced.rst │ ├── api.rst │ ├── conf.py │ ├── examples.rst │ ├── faq.rst │ ├── howtohelp.rst │ ├── index.rst │ ├── install.rst │ ├── links.rst │ ├── quickstart.rst │ ├── requirements.txt │ └── transformations.rst ├── emails/ │ ├── __init__.py │ ├── backend/ │ │ ├── __init__.py │ │ ├── factory.py │ │ ├── inmemory/ │ │ │ └── __init__.py │ │ ├── response.py │ │ └── smtp/ │ │ ├── __init__.py │ │ ├── aio_backend.py │ │ ├── aio_client.py │ │ ├── backend.py │ │ ├── client.py │ │ └── exceptions.py │ ├── django/ │ │ └── __init__.py │ ├── django_.py │ ├── exc.py │ ├── loader/ │ │ ├── __init__.py │ │ └── helpers.py │ ├── message.py │ ├── py.typed │ ├── signers.py │ ├── store/ │ │ ├── __init__.py │ │ ├── file.py │ │ └── store.py │ ├── template/ │ │ ├── __init__.py │ │ ├── base.py │ │ ├── jinja_template.py │ │ └── mako_template.py │ ├── testsuite/ │ │ ├── __init__.py │ │ ├── conftest.py │ │ ├── django_/ │ │ │ └── test_django_integrations.py │ │ ├── loader/ │ │ │ ├── data/ │ │ │ │ └── html_import/ │ │ │ │ └── oldornament/ │ │ │ │ └── oldornament/ │ │ │ │ └── index.html │ │ │ ├── test_helpers.py │ │ │ ├── test_loaders.py │ │ │ └── test_rfc822_loader.py │ │ ├── message/ │ │ │ ├── __init__.py │ │ │ ├── helpers.py │ │ │ ├── test_dkim.py │ │ │ ├── test_lazy_gettext.py │ │ │ ├── test_message.py │ │ │ ├── test_send.py │ │ │ ├── test_send_async.py │ │ │ ├── test_send_async_e2e.py │ │ │ └── test_template.py │ │ ├── smtp/ │ │ │ ├── test_aio_client.py │ │ │ ├── test_async_smtp_backend.py │ │ │ ├── test_factory.py │ │ │ ├── test_smtp_backend.py │ │ │ └── test_smtp_response.py │ │ ├── smtp_servers.py │ │ ├── store/ │ │ │ └── test_store.py │ │ ├── test_templates.py │ │ ├── test_utils.py │ │ └── transformer/ │ │ ├── data/ │ │ │ └── premailer_load/ │ │ │ └── style.css │ │ ├── test_parser.py │ │ └── test_transformer.py │ ├── transformer.py │ └── utils.py ├── release.sh ├── requirements/ │ ├── base.txt │ ├── tests-base.txt │ ├── tests-django.txt │ └── tests.txt ├── scripts/ │ └── make_rfc822.py ├── setup.cfg ├── setup.py └── tox.ini ================================================ FILE CONTENTS ================================================ ================================================ FILE: .coveragerc ================================================ [run] source = emails [report] omit = emails/testsuite* emails/packages* emails/compat* ================================================ FILE: .github/workflows/python-publish.yml ================================================ name: Upload Python Package on: release: types: [created] jobs: deploy: runs-on: ubuntu-latest permissions: id-token: write steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: '3.12' - name: Install build tools run: pip install build - name: Build package run: python -m build - name: Publish to PyPI uses: pypa/gh-action-pypi-publish@release/v1 ================================================ FILE: .github/workflows/tests.yaml ================================================ name: Tests on: push: branches: - master - '*' tags: - '**' pull_request: branches: - master jobs: tests: name: "unit / ${{ matrix.name }}" runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: include: - {name: '3.14', python: '3.14', os: ubuntu-latest, tox: py314} - {name: '3.13', python: '3.13', os: ubuntu-latest, tox: py313} - {name: '3.12', python: '3.12', os: ubuntu-latest, tox: py312} - {name: '3.11', python: '3.11', os: ubuntu-latest, tox: py311} - {name: '3.10', python: '3.10', os: ubuntu-latest, tox: py310} steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python }} cache: pip - name: update pip run: | pip install -U wheel pip install -U setuptools python -m pip install -U pip - run: pip install tox - name: run tests run: tox -e ${{ matrix.tox }} -- -m "not e2e and not django" django: name: "django / ${{ matrix.django }}" runs-on: ubuntu-latest strategy: fail-fast: false matrix: include: - {django: '4.2', tox: django42} - {django: '5.2', tox: django52} - {django: '6.0', tox: django60} steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: '3.12' cache: pip - name: update pip run: | pip install -U wheel pip install -U setuptools python -m pip install -U pip - run: pip install tox - name: run django tests run: tox -e ${{ matrix.tox }} docs: name: "docs" runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: '3.12' cache: pip - name: install dependencies run: | pip install -e ".[html]" pip install sphinx -r docs/requirements.txt - name: build docs run: sphinx-build -W -b html docs docs/_build/html - name: run doctests run: sphinx-build -b doctest docs docs/_build/doctest typecheck: name: "typecheck" runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: '3.12' cache: pip - name: update pip run: | pip install -U wheel pip install -U setuptools python -m pip install -U pip - run: pip install tox - name: run mypy run: tox -e typecheck e2e: name: "e2e / ${{ matrix.name }}" runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: include: - {name: '3.14', python: '3.14', os: ubuntu-latest, tox: py314} services: mailpit: image: axllent/mailpit ports: - 1025:1025 - 8025:8025 steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python }} cache: pip - name: update pip run: | pip install -U wheel pip install -U setuptools python -m pip install -U pip - run: pip install tox - name: run e2e tests env: SMTP_TEST_SUBJECT_SUFFIX: "github-actions sha:${{ github.sha }} run_id:${{ github.run_id }}" SMTP_TEST_MAIL_FROM: python-emails-tests@lavr.me SMTP_TEST_MAIL_TO: python-emails-tests@lavr.me SMTP_TEST_SETS: LOCAL SMTP_TEST_LOCAL_HOST: 127.0.0.1 SMTP_TEST_LOCAL_PORT: 1025 SMTP_TEST_LOCAL_WITHOUT_TLS: true run: tox -e ${{ matrix.tox }} -- -m e2e publish_rtd: name: "publish read the docs" needs: - tests - django - docs - typecheck - e2e if: github.event_name == 'push' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/')) runs-on: ubuntu-latest env: RTD_API_TOKEN: ${{ secrets.RTD_API_TOKEN }} RTD_PROJECT_SLUG: python-emails steps: - name: Trigger Read the Docs build run: | set -euo pipefail : "${RTD_API_TOKEN:?RTD_API_TOKEN secret is required}" api_base="https://app.readthedocs.org/api/v3/projects/${RTD_PROJECT_SLUG}" auth_header="Authorization: Token ${RTD_API_TOKEN}" get_version_details() { local version_slug="$1" local response_file="${2:-version.json}" curl \ --silent \ --show-error \ --output "${response_file}" \ --write-out '%{http_code}' \ --header "${auth_header}" \ "${api_base}/versions/${version_slug}/" } wait_for_version_slug() { local version_name="$1" for attempt in {1..12}; do local status_code local version_slug status_code="$( curl \ --silent \ --show-error \ --output versions.json \ --write-out '%{http_code}' \ --get \ --header "${auth_header}" \ --data-urlencode "type=tag" \ --data-urlencode "verbose_name=${version_name}" \ "${api_base}/versions/" )" if [[ "${status_code}" == "200" ]]; then version_slug="$( jq \ --raw-output \ --arg version_name "${version_name}" \ '.results[] | select(.verbose_name == $version_name) | .slug' \ versions.json | head -n 1 )" if [[ -n "${version_slug}" && "${version_slug}" != "null" ]]; then printf '%s\n' "${version_slug}" return 0 fi fi sleep 5 done echo "Read the Docs version '${version_name}' was not found after sync." if [[ -f versions.json ]]; then cat versions.json fi return 1 } trigger_build() { local version_slug="$1" echo "Triggering Read the Docs build for version slug '${version_slug}'." curl \ --fail-with-body \ --silent \ --show-error \ --request POST \ --header "${auth_header}" \ "${api_base}/versions/${version_slug}/builds/" } if [[ "${GITHUB_REF_TYPE}" == "branch" ]]; then trigger_build latest exit 0 fi version_name="${GITHUB_REF_NAME}" curl \ --fail-with-body \ --silent \ --show-error \ --request POST \ --header "${auth_header}" \ "${api_base}/sync-versions/" version_slug="$(wait_for_version_slug "${version_name}")" status_code="$(get_version_details "${version_slug}")" if [[ "${status_code}" != "200" ]]; then echo "Failed to fetch Read the Docs version details for '${version_slug}'." cat version.json exit 1 fi active="$(jq -r '.active' version.json)" hidden="$(jq -r '.hidden' version.json)" echo "Read the Docs version '${version_slug}' status: active=${active}, hidden=${hidden}." if [[ "${active}" == "true" && "${hidden}" == "false" ]]; then trigger_build "${version_slug}" exit 0 fi echo "Activating and unhiding Read the Docs version '${version_slug}'." curl \ --fail-with-body \ --silent \ --show-error \ --request PATCH \ --header "${auth_header}" \ --header "Content-Type: application/json" \ --data '{"active": true, "hidden": false}' \ "${api_base}/versions/${version_slug}/" status_code="$(get_version_details "${version_slug}")" if [[ "${status_code}" != "200" ]]; then echo "Failed to re-fetch Read the Docs version details for '${version_slug}' after PATCH." cat version.json exit 1 fi active="$(jq -r '.active' version.json)" hidden="$(jq -r '.hidden' version.json)" echo "Read the Docs version '${version_slug}' updated status: active=${active}, hidden=${hidden}." if [[ "${active}" == "true" && "${hidden}" == "false" ]]; then trigger_build "${version_slug}" exit 0 fi echo "Read the Docs version '${version_slug}' is still not buildable after PATCH." cat version.json exit 1 ================================================ FILE: .gitignore ================================================ local_settings.py local_*.py *.py[cod] # C extensions *.so # Packages *.egg *.egg-info dist build eggs parts bin var sdist develop-eggs .installed.cfg lib lib64 # Installer logs pip-log.txt # Unit test / coverage reports .coverage .tox nosetests.xml # Translations *.mo # Mr Developer .mr.developer.cfg .project .pydevproject .idea venv/ .env docs/plans/ docs/_build/ .claude/*local* .claude/worktrees/ # CodeQL .codeql-db codeql-results.sarif # ralphex progress logs .ralphex/progress/ ================================================ FILE: .pre-commit-config.yaml ================================================ repos: - repo: https://github.com/asottile/pyupgrade rev: v3.21.2 hooks: - id: pyupgrade args: ["--py310-plus"] - repo: https://github.com/asottile/reorder_python_imports rev: v3.16.0 hooks: - id: reorder-python-imports name: Reorder Python imports (src, tests) files: "^(?!examples/)" args: ["--application-directories", "src"] - repo: https://github.com/python/black rev: 26.3.1 hooks: - id: black - repo: https://github.com/pycqa/flake8 rev: 7.3.0 hooks: - id: flake8 additional_dependencies: - flake8-bugbear - flake8-implicit-str-concat - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 hooks: - id: check-byte-order-marker - id: trailing-whitespace - id: end-of-file-fixer ================================================ FILE: .readthedocs.yaml ================================================ version: 2 build: os: ubuntu-24.04 tools: python: "3.12" sphinx: configuration: docs/conf.py python: install: - requirements: docs/requirements.txt - method: pip path: . ================================================ FILE: AUTHORS.rst ================================================ Authors ``````` python-emails is maintained by: - `@lavr `_ With inputs and contributions from (in chronological order): - `@smihai `_ - `@Daviey `_ - `@positiveviking `_ See `all Github contributors `_ ================================================ FILE: CHANGELOG.md ================================================ # Changelog ## 1.0.2 ### Added - Documentation build check in CI (#208) - ReadTheDocs configuration (`.readthedocs.yaml`) ### Fixed - Jinja2 is now an optional dependency — install with `pip install emails[jinja2]` (#207, #161) ## 1.0 ### Breaking changes - Require Python 3.10+ (dropped 3.9) (#188) - HTML transformation dependencies (`cssutils`, `lxml`, `chardet`, `requests`, `premailer`) are now optional — install with `pip install emails[html]` (#190) - Removed Python 2 compatibility helpers `to_bytes`, `to_native`, `to_unicode` from `emails.utils` (#197) - Replaced vendored `emails.packages.dkim` with upstream `dkimpy` package — use `import dkim` directly (#196) ### Added - `reply_to` parameter for Message (#115) - Content-based MIME type detection via `puremagic` when file extension is missing (#163) - Data URI support in transformer — `data:` URIs are preserved as-is (#62) - Type hints for public API (#191) - mypy in CI (#194) - Python 3.13 and 3.14 support (#184) - Django CI jobs with Django 4.2, 5.2, 6.0 (#201) - CC/BCC importing in MsgLoader (#182) - RFC 6532 support — non-ASCII characters in email addresses (#138) - In-memory SMTP backend (#136) - SMTP integration tests using Mailpit (#186) ### Fixed - Double stream read in `BaseFile.mime` for file-like attachments (#199) - `as_bytes` DKIM signing bug (#194) - SMTP connection is now properly closed on any initialization failure (#180) - SMTP connection is now properly closed on failed login (#173) - Incorrect `isinstance` check in `parse_name_and_email_list` (#176) - Message encoding to bytes in SMTP backend (#152) - Unique filename generation for attachments - Regex escape sequence warning (#148) - Replaced deprecated `cgi` module with `email.message` - Coverage reports now correctly exclude `emails/testsuite/` ### Maintenance - Removed vendored dkim package (~1400 lines) - Removed Python 2 compatibility code and helpers (#188, #197, #198) - Updated pre-commit hooks to current versions - Updated GitHub Actions to supported versions - Removed universal wheel flag (py3-only) - Cleaned up documentation and project metadata - Added Python 3.12 to test matrix (#169) ## 0.6 — 2019-07-14 Last release before the changelog was introduced. ================================================ FILE: LICENSE ================================================ Copyright 2013-2015 Sergey Lavrinenko Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: MANIFEST.in ================================================ include README.rst LICENSE requirements.txt include emails/py.typed ================================================ FILE: Makefile ================================================ DOCS_PYTHON = .venv/bin/python DOCS_SOURCE = docs DOCS_BUILD = $(DOCS_SOURCE)/_build/html SPHINXOPTS ?= .PHONY: clean docs test pypi codeql-db codeql-analyze codeql-clean clean: find . -name '*.pyc' -exec rm -f {} \; find . -name '*.py~' -exec rm -f {} \; find . -name '__pycache__' -exec rm -rf {} \; find . -name '.coverage.*' -exec rm -rf {} \; rm -rf build dist emails.egg-info tmp-emails _files $(DOCS_SOURCE)/_build docs: $(DOCS_PYTHON) -m sphinx -b html $(SPHINXOPTS) $(DOCS_SOURCE) $(DOCS_BUILD) @echo @echo "Build finished. Open $(DOCS_BUILD)/index.html" test: tox pypi: python setup.py sdist bdist_wheel upload CODEQL_DB = .codeql-db CODEQL_PYTHON = .venv/bin/python codeql-db: rm -rf $(CODEQL_DB) codeql database create $(CODEQL_DB) --language=python --source-root=. \ --extractor-option=python.python_executable=$(CODEQL_PYTHON) codeql-analyze: codeql-db codeql pack download codeql/python-queries codeql database analyze $(CODEQL_DB) codeql/python-queries \ --format=sarif-latest --output=codeql-results.sarif codeql-clean: rm -rf $(CODEQL_DB) codeql-results.sarif ================================================ FILE: README.rst ================================================ python-emails ============= .. |pypi| image:: https://img.shields.io/pypi/v/emails.svg :target: https://pypi.org/project/emails/ :alt: PyPI version .. |python| image:: https://img.shields.io/pypi/pyversions/emails.svg :target: https://pypi.org/project/emails/ :alt: Python versions .. |tests| image:: https://github.com/lavr/python-emails/workflows/Tests/badge.svg?branch=master :target: https://github.com/lavr/python-emails/actions?query=workflow%3ATests :alt: Test status .. |docs| image:: https://readthedocs.org/projects/python-emails/badge/?version=latest :target: https://python-emails.readthedocs.io/ :alt: Documentation status .. |license| image:: https://img.shields.io/pypi/l/emails.svg :target: https://github.com/lavr/python-emails/blob/master/LICENSE :alt: License |pypi| |python| |tests| |docs| |license| Build, transform, and send emails in Python with a high-level API. ``python-emails`` helps you compose HTML and plain-text messages, attach files, embed inline images, render templates, apply HTML transformations, sign with DKIM, and send through SMTP without hand-building MIME trees. Why python-emails ----------------- - A concise API over ``email`` and ``smtplib`` - HTML and plain-text messages in one object - File attachments and inline images - CSS inlining, image embedding, and HTML cleanup - Jinja2, Mako, and string template support - DKIM signing - Loaders for URLs, HTML files, directories, ZIP archives, and RFC 822 messages - SMTP sending with SSL/TLS support - Async sending via ``aiosmtplib`` Quick Example ------------- .. code-block:: python import emails message = emails.html( subject="Your receipt", html="

Hello!

Your payment was received.

", mail_from=("Billing", "billing@example.com"), ) message.attach(filename="receipt.pdf", data=open("receipt.pdf", "rb")) response = message.send( to="customer@example.com", smtp={ "host": "smtp.example.com", "port": 587, "tls": True, "user": "billing@example.com", "password": "app-password", }, ) assert response.status_code == 250 Installation ------------ Install the lightweight core: .. code-block:: bash pip install emails Install HTML transformation features such as CSS inlining, image embedding, and loading from URLs or files: .. code-block:: bash pip install "emails[html]" Install Jinja2 template support for the ``JinjaTemplate`` class: .. code-block:: bash pip install "emails[jinja]" Install async SMTP sending support for ``send_async()``: .. code-block:: bash pip install "emails[async]" Common Tasks ------------ - Build and send your first message: `Quickstart `_ - Configure installation extras: `Install guide `_ - Inline CSS, embed images, and customize HTML processing: `Advanced Usage `_ - Learn the full public API: `API Reference `_ - Troubleshoot common scenarios: `FAQ `_ - Explore alternatives and related projects: `Links `_ What You Get ------------ - Message composition for HTML, plain text, headers, CC/BCC, and Reply-To - Attachments, inline images, and MIME generation - Template rendering in ``html``, ``text``, and ``subject`` - HTML transformations through ``message.transform()`` - SMTP delivery through config dicts or reusable backend objects - Django integration via ``DjangoMessage`` - Flask integration via `flask-emails `_ When To Use It -------------- Use ``python-emails`` when you need more than a minimal plain-text SMTP call: HTML emails, attachments, inline images, template rendering, DKIM, message loading from external sources, or a cleaner API than hand-written ``email.mime`` code. If you only need to send a very small plain-text message and want zero dependencies, the standard library may be enough. Documentation ------------- - `Documentation home `_ - `Quickstart `_ - `Advanced Usage `_ - `API Reference `_ - `FAQ `_ Project Status -------------- ``python-emails`` is production/stable software and currently supports Python 3.10 through 3.14. Contributing ------------ Issues and pull requests are welcome. - `Report a bug or request a feature `_ - `Source code on GitHub `_ - `How to Help `_ License ------- Apache 2.0. See `LICENSE `_. ================================================ FILE: docs/Makefile ================================================ # Makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = BUILDDIR = _build # User-friendly check for sphinx-build ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) endif # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . # the i18n builder cannot share the environment and doctrees with the others I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext help: @echo "Please use \`make ' where is one of" @echo " html to make standalone HTML files" @echo " dirhtml to make HTML files named index.html in directories" @echo " singlehtml to make a single large HTML file" @echo " pickle to make pickle files" @echo " json to make JSON files" @echo " htmlhelp to make HTML files and a HTML help project" @echo " qthelp to make HTML files and a qthelp project" @echo " devhelp to make HTML files and a Devhelp project" @echo " epub to make an epub" @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" @echo " latexpdf to make LaTeX files and run them through pdflatex" @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" @echo " text to make text files" @echo " man to make manual pages" @echo " texinfo to make Texinfo files" @echo " info to make Texinfo files and run them through makeinfo" @echo " gettext to make PO message catalogs" @echo " changes to make an overview of all changed/added/deprecated items" @echo " xml to make Docutils-native XML files" @echo " pseudoxml to make pseudoxml-XML files for display purposes" @echo " linkcheck to check all external links for integrity" @echo " doctest to run all doctests embedded in the documentation (if enabled)" clean: rm -rf $(BUILDDIR)/* html: $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." dirhtml: $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." singlehtml: $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml @echo @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." pickle: $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle @echo @echo "Build finished; now you can process the pickle files." json: $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json @echo @echo "Build finished; now you can process the JSON files." htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in $(BUILDDIR)/htmlhelp." qthelp: $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp @echo @echo "Build finished; now you can run "qcollectiongenerator" with the" \ ".qhcp project file in $(BUILDDIR)/qthelp, like this:" @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/python-emails.qhcp" @echo "To view the help file:" @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/python-emails.qhc" devhelp: $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp @echo @echo "Build finished." @echo "To view the help file:" @echo "# mkdir -p $$HOME/.local/share/devhelp/python-emails" @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/python-emails" @echo "# devhelp" epub: $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub @echo @echo "Build finished. The epub file is in $(BUILDDIR)/epub." latex: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." @echo "Run \`make' in that directory to run these through (pdf)latex" \ "(use \`make latexpdf' here to do that automatically)." latexpdf: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through pdflatex..." $(MAKE) -C $(BUILDDIR)/latex all-pdf @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." latexpdfja: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through platex and dvipdfmx..." $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." text: $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text @echo @echo "Build finished. The text files are in $(BUILDDIR)/text." man: $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man @echo @echo "Build finished. The manual pages are in $(BUILDDIR)/man." texinfo: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." @echo "Run \`make' in that directory to run these through makeinfo" \ "(use \`make info' here to do that automatically)." info: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo "Running Texinfo files through makeinfo..." make -C $(BUILDDIR)/texinfo info @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." gettext: $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale @echo @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." changes: $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes @echo @echo "The overview file is in $(BUILDDIR)/changes." linkcheck: $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck @echo @echo "Link check complete; look for any errors in the above output " \ "or in $(BUILDDIR)/linkcheck/output.txt." doctest: $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest @echo "Testing of doctests in the sources finished, look at the " \ "results in $(BUILDDIR)/doctest/output.txt." xml: $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml @echo @echo "Build finished. The XML files are in $(BUILDDIR)/xml." pseudoxml: $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml @echo @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." ================================================ FILE: docs/_static/.gitkeep ================================================ ================================================ FILE: docs/advanced.rst ================================================ Advanced Usage ============== This section covers advanced features and usage patterns of ``python-emails``. SMTP Connections ---------------- By default, :meth:`~emails.Message.send` accepts an ``smtp`` dict and manages the connection internally: .. code-block:: python response = message.send( to="user@example.com", smtp={"host": "smtp.example.com", "port": 587, "tls": True, "user": "me", "password": "secret"} ) For more control, you can use :class:`~emails.backend.smtp.SMTPBackend` directly. Reusing Connections ~~~~~~~~~~~~~~~~~~~ When you call :meth:`~emails.Message.send` with the same ``smtp`` dict on the same message, the library automatically reuses the SMTP connection through an internal pool. Connections with identical parameters share a backend: .. code-block:: python smtp_config = {"host": "smtp.example.com", "port": 587, "tls": True, "user": "me", "password": "secret"} # These two calls reuse the same underlying SMTP connection message.send(to="alice@example.com", smtp=smtp_config) message.send(to="bob@example.com", smtp=smtp_config) For explicit connection management, create an :class:`SMTPBackend` instance and pass it instead of a dict. The backend supports context managers: .. code-block:: python from emails.backend.smtp import SMTPBackend with SMTPBackend(host="smtp.example.com", port=587, tls=True, user="me", password="secret") as backend: for recipient in recipients: message.send(to=recipient, smtp=backend) # Connection is closed automatically SSL vs STARTTLS ~~~~~~~~~~~~~~~ The library supports two encryption modes: - **Implicit SSL** (``ssl=True``): Connects over TLS from the start. Typically used with port 465. .. code-block:: python message.send(smtp={"host": "mail.example.com", "port": 465, "ssl": True, "user": "me", "password": "secret"}) - **STARTTLS** (``tls=True``): Connects in plain text, then upgrades to TLS. Typically used with port 587. .. code-block:: python message.send(smtp={"host": "smtp.example.com", "port": 587, "tls": True, "user": "me", "password": "secret"}) You cannot set both ``ssl`` and ``tls`` to ``True`` -- this raises a ``ValueError``. Timeouts ~~~~~~~~ The default socket timeout is 5 seconds. You can change it with the ``timeout`` parameter: .. code-block:: python message.send(smtp={"host": "smtp.example.com", "timeout": 30}) Debugging ~~~~~~~~~ Enable SMTP protocol debugging to see the full conversation with the server on stdout: .. code-block:: python message.send(smtp={"host": "smtp.example.com", "debug": 1}) All SMTP Parameters ~~~~~~~~~~~~~~~~~~~ The full list of parameters accepted in the ``smtp`` dict (or as :class:`SMTPBackend` constructor arguments): - ``host`` -- SMTP server hostname - ``port`` -- server port (int) - ``ssl`` -- use implicit SSL/TLS (for port 465) - ``tls`` -- use STARTTLS (for port 587) - ``user`` -- authentication username - ``password`` -- authentication password - ``timeout`` -- socket timeout in seconds (default: ``5``) - ``debug`` -- debug level (``0`` = off, ``1`` = verbose) - ``fail_silently`` -- if ``True`` (default), return errors in the response instead of raising exceptions - ``local_hostname`` -- FQDN for the EHLO/HELO command (auto-detected if not set) - ``keyfile`` -- path to SSL key file - ``certfile`` -- path to SSL certificate file - ``mail_options`` -- list of ESMTP MAIL command options (e.g., ``["smtputf8"]``) HTML Transformations -------------------- The :meth:`~emails.Message.transform` method processes the HTML body before sending -- inlining CSS, loading images, removing unsafe tags, and more. .. code-block:: python message = emails.Message( html="

Hello!

" ) message.transform() After transformation, the inline style is applied directly: .. code-block:: python print(message.html) #

Hello!

Parameters ~~~~~~~~~~ :meth:`~emails.Message.transform` accepts the following keyword arguments: ``css_inline`` (default: ``True``) Inline CSS styles using `premailer `_. External stylesheets referenced in ```` tags are loaded and converted to inline ``style`` attributes. ``remove_unsafe_tags`` (default: ``True``) Remove potentially dangerous HTML tags: ``