一个简单而强大的Python邮件通知工具
Project description
PyMail
PyMail
A simple yet powerful Python error email notification tool. Automatically sends email notifications to specified recipients when program errors occur.
Features
- Multiple recipient support
- Configurable SMTP server settings
- SSL/TLS secure connection support
- Timeout limits to prevent email flooding
- Simple decorator interface
- Environment variables and YAML configuration support
- Secure sensitive information storage using .env file
Installation
pip install pymail-notify
Configuration
You can configure email settings through:
-
Environment Variables File (Recommended):
- Copy
.env.exampleto.env - Configure your email information in
.env
# SMTP Server Configuration SMTP_SERVER=smtp.gmail.com SMTP_PORT=465 # Email Account Configuration (Sensitive) SENDER_EMAIL=your-email@gmail.com EMAIL_PASSWORD=your-app-password # Recipients Configuration EMAIL_RECIPIENTS=recipient1@example.com,recipient2@example.com
- Copy
-
YAML Configuration File (for non-sensitive settings):
- Copy
email_config.yaml.exampletoemail_config.yaml - Configure non-sensitive information in
email_config.yaml - Keep sensitive information (like passwords) in
.env
- Copy
Security Notes
- Never commit
.envfile to version control - Add to
.gitignore:.env email_config.yaml - Set correct permissions for
.env:chmod 600 .env # Owner read/write only
Common Email Server Configurations
-
Gmail
- SMTP Server: smtp.gmail.com
- SSL Port: 465
- Requires "App Password"
-
QQ Mail
- SMTP Server: smtp.qq.com
- SSL Port: 465
- Requires authorization code
-
163 Mail
- SMTP Server: smtp.163.com
- SSL Port: 465
- Requires authorization code
Usage Examples
from pymail import email_on_error
@email_on_error(subject="Custom Error Subject")
def my_function():
# Your code
raise Exception("An error occurred")
# Or directly use EmailSender
from pymail import EmailSender
sender = EmailSender()
try:
# Your code
except Exception as e:
sender.send_error(str(e))
Important Notes
- For Gmail, use App Password instead of account password
- For QQ/163 Mail, obtain authorization code
- Use correct SMTP ports (465 for SSL, 587 for STARTTLS)
- Default configuration limits identical error notifications (once per 5 minutes)
PyMail
一个简单而强大的Python错误邮件通知工具。当程序发生错误时,自动发送邮件通知到指定的收件人列表。
特性
- 支持多收件人
- 可配置SMTP服务器设置
- 支持SSL/TLS安全连接
- 支持超时限制,避免邮件轰炸
- 提供简单的装饰器接口
- 支持环境变量和YAML配置文件
- 使用.env文件安全存储敏感信息
安装
pip install -r requirements.txt
配置
你可以通过以下方式配置邮件设置:
-
环境变量文件(推荐):
- 复制
.env.example到.env - 在
.env文件中配置你的邮箱信息
# SMTP服务器配置 SMTP_SERVER=smtp.gmail.com SMTP_PORT=465 # 邮箱账号配置(敏感信息) SENDER_EMAIL=your-email@gmail.com EMAIL_PASSWORD=your-app-password # 收件人配置 EMAIL_RECIPIENTS=recipient1@example.com,recipient2@example.com
- 复制
-
YAML配置文件(用于非敏感配置):
- 复制
email_config.yaml.example到email_config.yaml - 在
email_config.yaml中配置非敏感信息 - 敏感信息(如密码)请配置在
.env文件中
- 复制
安全注意事项
- 永远不要将
.env文件提交到版本控制系统 - 在
.gitignore中添加以下内容:.env email_config.yaml - 确保
.env文件的权限设置正确:chmod 600 .env # 只允许文件所有者读写
常见邮件服务器配置
-
Gmail
- SMTP服务器:smtp.gmail.com
- SSL端口:465
- 需要开启"应用专用密码"
-
QQ邮箱
- SMTP服务器:smtp.qq.com
- SSL端口:465
- 需要在QQ邮箱设置中获取授权码
-
163邮箱
- SMTP服务器:smtp.163.com
- SSL端口:465
- 需要在163邮箱设置中获取授权码
使用示例
from pymail import email_on_error
@email_on_error(subject="自定义错误主题")
def my_function():
# 你的代码
raise Exception("发生了一个错误")
# 或者直接使用 EmailSender
from pymail import EmailSender
sender = EmailSender()
try:
# 你的代码
except Exception as e:
sender.send_error(str(e))
注意事项
- 如果使用Gmail,需要使用应用专用密码而不是账户密码
- 如果使用QQ邮箱或163邮箱,需要获取授权码
- 确保使用正确的SMTP端口(SSL用465,STARTTLS用587)
- 默认配置会限制相同错误的发送频率(默认5分钟内只发送一次)
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
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 pymail_notify-0.1.5.tar.gz.
File metadata
- Download URL: pymail_notify-0.1.5.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea2144b2fb390f2313bd1be7573a51de7c0dd7552a7a9b7246fb70f2f91f4874
|
|
| MD5 |
b389e9406aaf72042a10df069fbd7a8e
|
|
| BLAKE2b-256 |
b084a6f0d479d9cbecd1c1055b488c16af8b8883c62cb42e5b1ed1152650c8a3
|
File details
Details for the file pymail_notify-0.1.5-py3-none-any.whl.
File metadata
- Download URL: pymail_notify-0.1.5-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d0f62c71dff3795e4d9cd53fe7ba4175a7e0424009018b40d48c253cc1fc46b
|
|
| MD5 |
754d468cbef015c6e9c43d4955791020
|
|
| BLAKE2b-256 |
f5b538608efe909ef6f2566cc9b067d4d41e9d4eff18401eabb49bd264bc48db
|