Skip to main content

A Python library for simplified email sending and receiving with secure credentials management

Project description

MailScript

A Python library for simplified email sending and receiving with support for templates, attachments, and more.

Features

  • Send plain text and HTML emails
  • Template rendering with Jinja2
  • Email address validation
  • File attachments
  • Support for CC and BCC recipients
  • TLS/SSL encryption
  • Receive emails via IMAP
  • Extract and save email attachments
  • Output emails in JSON format

Installation

pip install mailscript

Configuration

MailScript provides multiple ways to configure your email credentials:

Option 1: Direct Configuration

Provide credentials directly to the Mailer or Receiver classes:

from mailscript import Mailer

mailer = Mailer(
    smtp_host="smtp.example.com",
    smtp_port=587,
    smtp_user="your-email@example.com",
    smtp_password="your-password",
    use_tls=True
)

Option 2: Configuration File

Create a configuration file in one of these locations:

  • ./config/mailscript_config.py
  • ~/.config/mailscript/config.py
  • ./mailscript_config.py

Example configuration file:

# mailscript_config.py
smtp_config = {
    "username": "your-email@example.com",
    "password": "your-password",
    "host": "smtp.example.com",
    "port": 587,
    "use_tls": True
}

imap_config = {
    "username": "your-email@example.com",
    "password": "your-password",
    "host": "imap.example.com",
    "port": 993
}

default_recipient = "recipient@example.com"

Then use the credential loader:

from mailscript import Mailer
from mailscript.credentials import get_smtp_config

smtp_config = get_smtp_config()
mailer = Mailer(
    smtp_host=smtp_config["host"],
    smtp_port=smtp_config["port"],
    smtp_user=smtp_config["username"],
    smtp_password=smtp_config["password"],
    use_tls=smtp_config["use_tls"]
)

Quick Start

Basic Usage

from mailscript import Mailer

# Initialize mailer
mailer = Mailer(
    smtp_host="smtp.example.com", 
    smtp_port=587,
    smtp_user="your-email@example.com",
    smtp_password="your-password",
    use_tls=True
)

# Send a simple email
mailer.send(
    sender="your-email@example.com",
    recipients="recipient@example.com",
    subject="Hello from MailScript",
    body="This is a test email from MailScript."
)

HTML Emails

# Send an HTML email
html_content = """
<!DOCTYPE html>
<html>
<body>
    <h1>Hello from MailScript</h1>
    <p>This is an <b>HTML</b> email!</p>
</body>
</html>
"""

mailer.send(
    sender="your-email@example.com",
    recipients="recipient@example.com",
    subject="HTML Email Test",
    body=html_content,
    is_html=True
)

Template Rendering

from mailscript.templates import TemplateRenderer

# Initialize renderer
renderer = TemplateRenderer()

# Render template from string
template = """
<h1>Hello, {{ name }}!</h1>
<p>Welcome to {{ company }}.</p>
"""

context = {"name": "John", "company": "Example Corp"}
html_content = renderer.render_from_string(template, context)

# Send templated email
mailer.send(
    sender="your-email@example.com",
    recipients="recipient@example.com",
    subject="Welcome Email",
    body=html_content,
    is_html=True
)

Built-in Template Methods

# Send an email using a template string directly
context = {
    "name": "John", 
    "features": ["Templates", "Attachments", "HTML Support"]
}

mailer.send_template(
    sender="your-email@example.com",
    recipients="recipient@example.com",
    subject="Welcome Email",
    template_string="<h1>Welcome, {{ name }}!</h1><ul>{% for feature in features %}<li>{{ feature }}</li>{% endfor %}</ul>",
    context=context,
    is_html=True
)

# Send an email using a template file
mailer.send_template_file(
    sender="your-email@example.com",
    recipients="recipient@example.com",
    subject="Newsletter",
    template_name="newsletter.html",
    template_folder="/path/to/templates",
    context={"name": "John", "month": "June"},
    is_html=True
)

With Attachments

# Send email with attachment
with open("document.pdf", "rb") as f:
    attachments = {"document.pdf": f}
    
    mailer.send(
        sender="your-email@example.com",
        recipients="recipient@example.com",
        subject="Email with attachment",
        body="Please find the attached document.",
        attachments=attachments
    )

Receiving Emails

from mailscript import Receiver

# Initialize receiver
receiver = Receiver(
    imap_host="imap.example.com", 
    imap_port=993,
    username="your-email@example.com", 
    password="your-password"
)

# Connect to the server
receiver.connect()

# Select mailbox
receiver.select_mailbox("INBOX")

# Fetch recent emails
emails = receiver.fetch_emails(
    count=5,                          # Number of recent emails to retrieve
    save_attachments=True,            # Save attachments to disk
    output_dir="./my_attachments"     # Directory to save attachments
)

# Process emails
for email in emails:
    print(f"From: {email['from']}")
    print(f"Subject: {email['subject']}")
    print(f"Date: {email['date']}")
    print(f"Body: {email['body'][:100]}...")  # Print first 100 chars
    
    # Process attachments
    for attachment in email['attachments']:
        print(f"Attachment: {attachment['filename']}")
        print(f"Saved at: {attachment['saved_path']}")
    
    print("---")

# Always logout when done
receiver.logout()

Command Line Usage

Sending Emails

# Send a plain text email
python -m mailscript send --host smtp.example.com --port 587 --user user@example.com \
    --from "Sender <sender@example.com>" --to recipient@example.com \
    --subject "Hello" --body "This is a test email"

# Send an HTML email with attachments
python -m mailscript send --host smtp.example.com --port 587 --user user@example.com \
    --from "Sender <sender@example.com>" --to recipient@example.com \
    --subject "Hello" --body-file email.html --html --attach document.pdf image.jpg

Receiving Emails

# Retrieve 10 recent emails and display them
python -m mailscript receive --host imap.example.com --port 993 --user user@example.com \
    --count 10

# Retrieve emails and save attachments
python -m mailscript receive --host imap.example.com --port 993 --user user@example.com \
    --count 5 --save-attachments --output-dir ./downloads

# Save email data to a JSON file
python -m mailscript receive --host imap.example.com --port 993 --user user@example.com \
    --output-file emails.json

License

MIT License

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

mailscript-0.2.0.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

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

mailscript-0.2.0-py2.py3-none-any.whl (15.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file mailscript-0.2.0.tar.gz.

File metadata

  • Download URL: mailscript-0.2.0.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for mailscript-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7df65bdb253b2225699e488987e0cd1e7b9d083a973566f0a73d8644187ce112
MD5 2f21b0a9f280f55ee997b0f434692035
BLAKE2b-256 216f9e4caee8d05a7419e672875f9faaa7f58fe19e4401605b4010b744820891

See more details on using hashes here.

File details

Details for the file mailscript-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: mailscript-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for mailscript-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fcae0e8e74ec30fb52cf41ca5c5f1f02aa2e8319e1d6e1e90b12c9242923acc7
MD5 c8f5045ba29b097aa40e4805f2b73362
BLAKE2b-256 9f4294db11f36065a6b6bcc52067df44e6f00921368c778f0a2c1f9808136305

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