Skip to main content

Custom mail function and middleware to send logs to a mail server

Project description

CredentiMail Logging System

This project provides a custom logging handler for Python applications that sends log messages via HTTP POST to a remote mail server. It includes structured data models for email and attachment handling using Python dataclasses.

📆 Features

  • Custom logging handler: CredentiMailHandler
  • Data models for email and attachments: MailDetail, Attachment
  • Sends logs to a mail server over HTTP
  • Flask-compatible
  • Includes test command via Flask CLI
  • Installable as a Python package

🚀 Installation

Install the package via pip:

pip install ct-mail-handler

📅 Packaging and Uploading to PyPI

To build and upload the package to PyPI:

1. Install required tools

pip install build twine

2. Build the package

python -m build

This creates dist/ containing .tar.gz and .whl files.

3. Upload to PyPI

python -m twine upload dist/*

You'll be prompted for your PyPI credentials.

For test uploads, use:

python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*

🚧 Testing Requirements

The dependencies listed in requirements.txt are used only for testing purposes (e.g., Flask CLI and testing libraries). They are not needed when installing this package for runtime use.

Install testing dependencies:

pip install -r requirements.txt

🌐 Usage

1. Add the Logging Handler

from credenti_mail_handler import CredentiMailHandler
import logging

handler = CredentiMailHandler(
    fromaddr="logger@example.com",
    toaddrs=["admin@example.com"],
    subject="Application Error",
    mail_server_url="http://localhost:5000/send-mail"
)

logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)
logger.addHandler(handler)

logger.error("This is a test error log.")

2. Email Data Models

MailDetail

  • from_address: str
  • to_address: List[str]
  • subject: str
  • body: str
  • attachments: Optional[List[Attachment]]

Attachment

  • file_content_b64: str
  • file_name: Optional[str>
  • file_type: Optional[str>
  • disposition: Literal['attachment', 'inline']

Add attachments using:

mail_detail.add_attachment(
    file_content_b64="base64content...",
    file_name="report.pdf",
    file_type="application/pdf"
)

🔧 Internal Methods

CredentiMailHandler.emit()

Formats and sends a log record as an email via send_email().

prepare_email()

Returns a MailDetail object from parameters.

send_email()

Uses requests.post() to send the MailDetail as JSON to the mail server.


🔮 Testing

To run tests using Flask CLI:

manage.py

Ensure you have a manage.py file that defines the test command, e.g.:

import click
from flask import Flask

app = Flask(__name__)

@app.cli.command("test")
def test():
    import unittest
    tests = unittest.TestLoader().discover("tests")
    unittest.TextTestRunner().run(tests)

Run tests:

flask --app manage.py test

📅 Folder Structure

.
├── README.md
├── app.py
├── ct_mail_handler/
│   ├── __init__.py
│   ├── credenti_mail_handler.py
│   └── models.py
├── manage.py
├── requirements.txt
├── setup.py
├── tests/
    └── test_handler.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ct_mail_handler-0.0.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ct_mail_handler-0.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file ct_mail_handler-0.0.1.tar.gz.

File metadata

  • Download URL: ct_mail_handler-0.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for ct_mail_handler-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2359fbeb47d6ce06294fc217ed1e3e1b30db9da7ef7e862054581eab1f421446
MD5 e19b1fe86b95a7ecaf37e8535212ac50
BLAKE2b-256 5073677eca8d0d40db0cc9051df715e37741fb069e1966237c928bcf97067ea3

See more details on using hashes here.

File details

Details for the file ct_mail_handler-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ct_mail_handler-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6ab3b435ae58f8d61261b31f0e99491e7bd829f94cdd2ac2d8991cff4bdf5cec
MD5 b318c30089b0c7f49719cefe8e957c1a
BLAKE2b-256 1221a21e0ee0dc0ca8e0697bad25c917b732ead97c003025fc4da8743c57575b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page