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: strto_address: List[str]subject: strbody: strattachments: Optional[List[Attachment]]
Attachment
file_content_b64: strfile_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
266b5f759d8ffae33cb97f9c5bce1ac63396f995c904a24959b1b88b75204558
|
|
| MD5 |
962a66de0c0d9b3bb5caa2e2c30470e7
|
|
| BLAKE2b-256 |
08cbdcde47756d54c2151893dd1bd7fb82005ca579fbbb2f62a4ffb357e64d9a
|
File details
Details for the file ct_mail_handler-0.0.2-py3-none-any.whl.
File metadata
- Download URL: ct_mail_handler-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c653c9b24f54100cff8bb0a4e5366ffc59a3cbdc0c1f2a4631746dba468f4028
|
|
| MD5 |
30c595d0fff7957a9aa2c05b72f74fe8
|
|
| BLAKE2b-256 |
af1424967729332fea329067bc01e8361e7e59fb8d38601d43a7fc09404d44a5
|