Skip to main content

A Python package for managing email transport.

Project description

MailTransport

MailTransport is a Python package designed to simplify email transport and management. It provides an easy-to-use interface for sending emails with support for advanced configurations, attachments, and error handling.

Features

  • Send emails with HTML content, plain text, or both.
  • Add CC, BCC, and reply-to addresses.
  • Attach files with automatic MIME type detection.
  • Use templates with dynamic variables.
  • Validate email addresses and handle errors gracefully.
  • Easy integration with Python applications.

Installation

Install the package using pip:

pip install mailtransport

Quick Start

Here’s how you can get started with MailTransport:

Import and Initialize

from mailtransport import MailTransportClient

# Initialize the client with your API key
transport = MailTransportClient(api_key="your_api_key")

Send Email

Send a Simple Email

from mailtransport import MailTransportClient

# Initialize the client with your API key
transport = MailTransportClient(api_key="your_api_key")

email_instance = transport.Mail(
    to_emails=['recipient@example.com'],
    from_email="noreply@company.com",
    html="<b>This is a test email</b>",
    subject="Test Email",
)
email_instance.send()

NB: For advanced usage with all the parameters, navigate to Mailtransport docs to see full documentations

Advanced Usage

Sending Emails with Attachments

You can attach files to your email using the attach_file or add_attachment methods:

email_instance = transport.Mail(
    to_emails=['recipient@example.com'],
    from_email="noreply@company.com",
    subject="Email with Attachment",
    html="<b>Please find the attachment below.</b>"
)

# Attach a file from the filesystem
email_instance.attach_file("path/to/file.pdf")

# Attach a file with custom content and MIME type
email_instance.add_attachment(
    filename="example.txt",
    content="This is the content of the file.",
    mimetype="text/plain"
)

email_instance.send()

Using Templates with Variables

You can use templates and pass dynamic variables for personalized emails:

email_instance = transport.Mail(
    to_emails=['recipient@example.com'],
    from_email="noreply@company.com",
    template_id="template_12345",
    variables={"name": "John Doe", "order_id": "123456"}
)

email_instance.send()

Adding CC, BCC, and Reply-To Addresses

email_instance = transport.Mail(
    to_emails=['recipient@example.com'],
    from_email="noreply@company.com",
    subject="Email with CC and BCC",
    html="<b>This email has CC and BCC recipients.</b>",
    cc=['cc@example.com'],
    bcc=['bcc@example.com'],
    reply_to="replyto@example.com"
)

email_instance.send()

Identity Management

The IdentityClient allows you to manage identities (e.g., domains or email addresses) for your email transport.

Create an Identity

from mailtransport import MailTransportClient

# Initialize the client with your API key
transport = MailTransportClient(api_key="your_api_key")

response = transport.identity.create(domain_name="example.com")
print(response)

Get an Identity

identity_id = "12345"
response = transport.identity.get(identity_id)
print(response)

List All Identities

response = transport.identity.list()
print(response)

Delete an Identity

identity_id = "12345"
response = transport.identity.delete(identity_id)
print(response)

Verify an Identity

identity_id = "12345"
response = transport.identity.verify(identity_id)
print(response)

Error Handling

MailTransport provides detailed error messages for failed email deliveries. Use try-except blocks to handle errors gracefully:

from mailtransport.exceptions import MailTransportValidationError, MailTransportAPIError

try:
    email_instance.send()
except MailTransportValidationError as e:
    print(f"Failed to send email due to incoreect input details: {e}")
except MailTransportAPIErroras e:
    print(f"Failed to send email due to api errors: {e}")

Methods Overview

MailClient.Mail()

Creates a new email instance with the following parameters:

  • to_emails (list or str): List of recipient email addresses.
  • from_email (str): Sender's email address.
  • subject (str): Subject of the email.
  • html (str): HTML content of the email.
  • text (str, optional): Plain text content of the email.
  • cc (list, optional): List of CC email addresses.
  • bcc (list, optional): List of BCC email addresses.
  • variables (dict, optional): Variables for email templates.
  • template_id (str, optional): Template ID for the email.
  • reply_to (str, optional): Reply-to email address.
  • in_reply_to (str, optional): Message ID this email is replying to.

MailClient.attach_file(path, mimetype=None)

Attaches a file from the filesystem. The MIME type is automatically detected if not provided.

MailClient.add_attachment(filename, content, mimetype=None)

Attaches a file with custom content and MIME type.

MailClient.send(fail_silently=False)

Sends the email. If fail_silently is False, raises an exception on failure.

MailClient.is_valid_email(email)

Validates a single email address.

MailClient.is_valid_email_list(email_list, type)

Validates a list of email addresses.

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Submit a pull request with a detailed description of your changes.

License

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

Support

For support or inquiries, please contact us at support@mailtransportai.com.

Documentation

For full documentation, visit the MailTransport Documentation.


Happy emailing with MailTransport!

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

mailtransport-0.1.3.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

mailtransport-0.1.3-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file mailtransport-0.1.3.tar.gz.

File metadata

  • Download URL: mailtransport-0.1.3.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for mailtransport-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5223f5ec2ffa9c3d5334dbaf4dbdf3be8463a1885aa10e1484982cdd24d6ca1f
MD5 f2b1163483222d25ebd40aeedbef9012
BLAKE2b-256 ecc86249ce44313d22e9f360b2f239e57e8a753676b57bfa820ab5a87d197591

See more details on using hashes here.

File details

Details for the file mailtransport-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: mailtransport-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for mailtransport-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7809d20ab1413e693d3fff8f746335643fd2e35bf3ea66d9fe29dba9ec3e9b9a
MD5 7d47c65c31e1190c56b5442da24b1796
BLAKE2b-256 abc459f5b908d52bef2a836efcd8b91b5418804fe994fdbc45d498531f2888d7

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