Skip to main content

Official Python SDK for Email Validation API with comprehensive security features

Project description

MailSafePro SDK for Python

Tests PyPI version Python versions License: MIT Code style: black Security: bandit Downloads

Official Python SDK for the MailSafePro Email Validation API with comprehensive security features including spam trap detection, breach checking, role email detection, and advanced SMTP/DNS validation.

✨ Features

  • 🔐 Dual Authentication: API Key and JWT support
  • 📧 Comprehensive Validation: Format, DNS, SMTP, deliverability
  • 🚫 Security Checks: Spam trap, breach detection, disposable emails
  • 📊 Batch Processing: Validate up to 10,000 emails at once
  • Fast & Reliable: Auto-retry with exponential backoff
  • 🎯 Type Safe: Full type hints for better IDE support
  • 📦 Easy Integration: Simple, intuitive API

🚀 Features

  • Dual Authentication: API Key (static) and JWT (dynamic with auto-refresh)
  • Comprehensive Validation: Format, DNS, SMTP, disposable detection
  • Security Checks: Spam trap detection, breach checking (HIBP), role email detection
  • DNS Authentication: SPF, DKIM, DMARC validation
  • Batch Processing: Validate thousands of emails efficiently
  • File Upload: Support for CSV/TXT files
  • Type Hints: Full type annotations for IDE autocompletion
  • Automatic Retries: Exponential backoff for rate limits and server errors
  • Detailed Results: Risk scores, quality scores, suggested actions

📦 Installation

pip install mailsafepro-sdk

Or with Poetry:

poetry add mailsafepro-sdk

🔑 Quick Start

API Key Authentication (Simple)

from mailsafepro import MailSafePro

# Initialize with API key
validator = MailSafePro(api_key="key_your_api_key_here")

# Validate single email
result = validator.validate("user@example.com")
print(f"Valid: {result.valid}")
print(f"Risk Score: {result.risk_score}")
print(f"Action: {result.suggested_action}")

JWT Authentication (Advanced)

from mailsafepro import MailSafePro

# Login with credentials (auto-refresh enabled)
validator = MailSafePro.login(
    username="your_email@example.com",
    password="your_password"
)

# Validate with SMTP check
result = validator.validate("test@example.com", check_smtp=True)
print(f"Mailbox exists: {result.smtp.mailbox_exists}")

# Logout when done
validator.logout()

📚 Usage Examples

Individual Validation

from mailsafepro import MailSafePro

validator = MailSafePro(api_key="key_xxx")

# Basic validation
result = validator.validate("user@example.com")

print(f"Email: {result.email}")
print(f"Valid: {result.valid}")
print(f"Status: {result.status}")  # deliverable, risky, undeliverable, unknown
print(f"Risk Score: {result.risk_score:.2f}")  # 0.0-1.0
print(f"Quality Score: {result.quality_score:.2f}")  # 0.0-1.0
print(f"Suggested Action: {result.suggested_action}")  # accept, review, monitor, reject

# Provider information
print(f"Provider: {result.provider_analysis.provider}")
print(f"Reputation: {result.provider_analysis.reputation:.2f}")

# DNS Security (PREMIUM)
if result.dns_security:
    print(f"SPF: {result.dns_security.spf.status}")
    print(f"DKIM: {result.dns_security.dkim.status}")
    print(f"DMARC: {result.dns_security.dmarc.status}")

# Spam Trap Detection (NEW)
if result.spam_trap_check and result.spam_trap_check.checked:
    print(f"Is Spam Trap: {result.spam_trap_check.is_spam_trap}")
    print(f"Confidence: {result.spam_trap_check.confidence:.2f}")

# Breach Information (PREMIUM/ENTERPRISE)
if result.breach_info:
    print(f"In Breach: {result.breach_info.in_breach}")
    print(f"Breach Count: {result.breach_info.breach_count}")

SMTP Verification (PREMIUM)

# Enable SMTP mailbox verification
result = validator.validate(
    "user@example.com",
    check_smtp=True,  # Requires PREMIUM plan
    include_raw_dns=True
)

print(f"SMTP Checked: {result.smtp.checked}")
print(f"Mailbox Exists: {result.smtp.mailbox_exists}")
print(f"MX Server: {result.smtp.mx_server}")
print(f"Response Time: {result.smtp.response_time}s")

Batch Validation

# Validate multiple emails
emails = [
    "user1@example.com",
    "user2@example.com",
    "invalid@domain.test",
]

batch_result = validator.validate_batch(emails, check_smtp=False)

print(f"Total: {batch_result.count}")
print(f"Valid: {batch_result.valid_count}")
print(f"Invalid: {batch_result.invalid_count}")
print(f"Processing Time: {batch_result.processing_time:.2f}s")

# Iterate results
for result in batch_result.results:
    print(f"{result.email}: {result.valid} (risk: {result.risk_score:.2f})")

File Upload (CSV/TXT)

# Validate emails from CSV file
result = validator.validate_file(
    file_path="emails.csv",
    column="email",  # Column name (optional, auto-detects)
    check_smtp=False
)

print(f"Processed {result.count} emails from file")
print(f"Valid: {result.valid_count}, Invalid: {result.invalid_count}")

# TXT file (one email per line)
result = validator.validate_file("emails.txt")

Advanced Configuration

# Custom configuration
validator = MailSafePro(
    api_key="key_xxx",
    base_url="https://api.mailsafepro.com",  # Custom API endpoint
    timeout=60,  # Request timeout in seconds
    max_retries=5,  # Maximum retry attempts
    enable_logging=True  # Enable debug logging
)

# Validation with priority (ENTERPRISE)
result = validator.validate(
    "user@example.com",
    priority="high"  # low, standard, high
)

🔄 JWT Auto-Refresh

The SDK automatically refreshes JWT tokens before they expire:

import time
validator = MailSafePro.login("user@example.com", "password")

# Token is automatically refreshed before each request if needed
result1 = validator.validate("test1@example.com")
time.sleep(900)  # 15 minutes
result2 = validator.validate("test2@example.com")  # Auto-refreshed

validator.logout()  # Invalidate session

🛡️ Error Handling

from mailsafepro import MailSafePro
from mailsafepro.exceptions import (
    AuthenticationError,
    RateLimitError,
    ValidationError,
    QuotaExceededError,
    ServerError,
)

validator = MailSafePro(api_key="key_xxx")

try:
    result = validator.validate("user@example.com")
except AuthenticationError as e:
    print(f"Authentication failed: {e}")
except RateLimitError as e:
    print(f"Rate limit exceeded. Retry after {e.retry_after} seconds")
except ValidationError as e:
    print(f"Validation error: {e}")
except QuotaExceededError as e:
    print(f"Quota exceeded: {e}")
except ServerError as e:
    print(f"Server error (status {e.status_code}): {e}")

📊 Interpreting Results

Risk Score (0.0-1.0)

  • 0.0-0.3: Low risk, safe to accept
  • 0.3-0.6: Medium risk, review recommended
  • 0.6-0.8: High risk, monitor closely
  • 0.8-1.0: Very high risk, reject

Suggested Actions

  • accept: Email is valid and safe
  • monitor: Valid but requires monitoring
  • review: Requires manual review
  • reject: Invalid or dangerous, reject immediately

Validation Tiers

  • basic: Format + DNS validation
  • standard: Basic + SMTP verification
  • premium: Standard + advanced checks (breach, spam trap, etc.)

🔧 Configuration Options

Parameter Type Default Description
api_key str None API key for authentication
base_url str Production URL API base URL
timeout int 30 Request timeout in seconds
max_retries int 3 Maximum retry attempts
enable_logging bool False Enable debug logging

📖 API Documentation

For complete API documentation, visit: https://docs.mailsafepro.com

🤝 Support

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🔗 Links


Made with ❤️ by the MailSafePro Team

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

mailsafepro_sdk-1.0.1.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

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

mailsafepro_sdk-1.0.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file mailsafepro_sdk-1.0.1.tar.gz.

File metadata

  • Download URL: mailsafepro_sdk-1.0.1.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for mailsafepro_sdk-1.0.1.tar.gz
Algorithm Hash digest
SHA256 89b6e9cee2c83211a84c01750109099e31258e29a01f9eee6cc585824b3a9a23
MD5 a0f1e41db4866e696b4dac40591a1df1
BLAKE2b-256 4b194101510c83d9259cec349cd2745f570dd8ed28d3f7efd5dc81ac2f7e79d9

See more details on using hashes here.

File details

Details for the file mailsafepro_sdk-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mailsafepro_sdk-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1d32243408485fb6f991433a9fec9e05ea1225e0b98f5edf056e4aa2f1110846
MD5 3abe04b5bc3ecacc1a10e661087ded8a
BLAKE2b-256 0179b76bfedeb1e48c30dee45502151fd28523598fe0bfc3fb009232597adef1

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