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.2.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.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ct_mail_handler-0.0.2.tar.gz
Algorithm Hash digest
SHA256 266b5f759d8ffae33cb97f9c5bce1ac63396f995c904a24959b1b88b75204558
MD5 962a66de0c0d9b3bb5caa2e2c30470e7
BLAKE2b-256 08cbdcde47756d54c2151893dd1bd7fb82005ca579fbbb2f62a4ffb357e64d9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ct_mail_handler-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c653c9b24f54100cff8bb0a4e5366ffc59a3cbdc0c1f2a4631746dba468f4028
MD5 30c595d0fff7957a9aa2c05b72f74fe8
BLAKE2b-256 af1424967729332fea329067bc01e8361e7e59fb8d38601d43a7fc09404d44a5

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