A Python library for simplified email sending
Project description
MailScript
A Python library for simplified email sending 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
Installation
pip install mailscript
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
)
Command Line Usage
MailScript can be used from the command line directly:
# Send a simple email
python -m mailscript send --host smtp.example.com --port 587 --user "your-user" \
--from "sender@example.com" --to "recipient@example.com" \
--subject "Hello from CLI" --body "This is a test email from the command line"
# Send an HTML email from a file
python -m mailscript send --host smtp.example.com --port 587 --user "your-user" \
--from "sender@example.com" --to "recipient@example.com" \
--subject "HTML Test" --body-file email.html --html
# Send a templated email
python -m mailscript template --host smtp.example.com --port 587 --user "your-user" \
--from "sender@example.com" --to "recipient@example.com" \
--subject "Template Test" --template "templates/welcome.html" \
--context "name=John" "company=Example Corp"
# Show version
python -m mailscript version
For more CLI options, run:
python -m mailscript send --help
python -m mailscript template --help
License
MIT License
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
mailscript-0.1.0.tar.gz
(14.6 kB
view details)
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 mailscript-0.1.0.tar.gz.
File metadata
- Download URL: mailscript-0.1.0.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad81b0919d8929592a390c08313ac1118463929e910a8d11bcf9fb2fd4b901b4
|
|
| MD5 |
6cbd7c68af4c7ade13be12bdedb5d394
|
|
| BLAKE2b-256 |
9b876f8e2e1f2ee593d86b1982262fab37cf459d8cffd1a8c459ded7d08f3655
|
File details
Details for the file mailscript-0.1.0-py2.py3-none-any.whl.
File metadata
- Download URL: mailscript-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0025c6bcf648b8cfb5502892e3707c8504cca4fc511bfeda8db21f51ddc0139d
|
|
| MD5 |
f3e760800e60ca1013f7f7b0290168aa
|
|
| BLAKE2b-256 |
90a80f3a2ab79559126c9cf149e1486686fbed8bd50bc88f64bbcdad77a214bf
|