Skip to main content

flask-mailjet

A lightweight, secure, and production-ready Flask extension for sending emails through the Mailjet API, built on top of the official mailjet-rest library.


🚀 Features

  • Simple configuration via MAILJET_API_KEY and MAILJET_API_SECRET
  • Straightforward email sending (HTML, multiple recipients)
  • Built‑in support for file attachments
  • Templating helper for rendering email templates
  • Works with both eager app initialization and the Flask Factory pattern
  • Clean API fully aligned with Mailjet's expectations

📦 Installation

pip install flask-mailjet

🛠️ Configuration

Add the following to your Flask application configuration:

app.config["MAILJET_API_KEY"] = "YOUR_API_KEY"
app.config["MAILJET_API_SECRET"] = "YOUR_API_SECRET"

Variable Description


MAILJET_API_KEY Your Mailjet API Public Key MAILJET_API_SECRET Your Mailjet API Secret Key


🔧 Initialization

Option 1: Eager Initialization

from flask import Flask
from flask_mailjet import Mailjet

app = Flask(__name__)
app.config["MAILJET_API_KEY"] = "YOUR_API_KEY"
app.config["MAILJET_API_SECRET"] = "YOUR_API_SECRET"

mailjet = Mailjet(app)

Option 2: Factory Pattern

from flask_mailjet import Mailjet

mailjet = Mailjet()

def create_app():
    app = Flask(__name__)
    app.config.from_mapping(
        MAILJET_API_KEY="YOUR_API_KEY",
        MAILJET_API_SECRET="YOUR_API_SECRET",
    )

    mailjet.init_app(app)
    return app

✉️ Sending Emails

All mail‑sending methods expect the sender field as:

{"Email": "sender@example.com", "Name": "Sender Name"}

1. send_email --- Basic HTML Email

from_user = {"Email": "alerts@app.com", "Name": "App Alerts"}
to_user = ["user1@mail.com", "user2@mail.com"]

mailjet.send_email(
    sender=from_user,
    recipients=to_user,
    subject="Your Account Is Active",
    html="<h1>Welcome!</h1><p>Your subscription is confirmed.</p>"
)

2. send_email_with_attachment --- Email + File Attachment

import os

pdf_path = os.path.join(app.root_path, "static", "invoice.pdf")
with open(pdf_path, "rb") as f:
    pdf_bytes = f.read()

mailjet.send_email_with_attachment(
    sender={"Email": "billing@app.com"},
    recipients="client@corp.com",
    subject="Q4 Financial Report",
    html="<p>Attached is the report.</p>",
    filename="Q4_Report.pdf",
    file_bytes=pdf_bytes,
    content_type="application/pdf",
)

🎨 Template Rendering (loader.py)

You can render email templates stored in:

flask_mailjet/templates/mailjet/

Example

from flask_mailjet.loader import render_email_template

email_html = render_email_template(
    "welcome.html",
    user_name="Tim",
    link="https://app.example.com"
)

mailjet.send_email(
    sender={"Email": "system@app.com"},
    recipients="user@app.com",
    subject="Welcome!",
    html=email_html,
)

📁 Project Structure Example

yourapp/
├── app.py
├── templates/
│   └── mailjet/
│       └── welcome.html
└── extensions/
    └── mailjet.py

🧪 Testing

You can mock Mailjet easily using Python's builtin tools:

from unittest.mock import patch

@patch("flask_mailjet.Mailjet.send_email")
def test_send(mock_send):
    mock_send.return_value = {"Status": "success"}

📜 License

MIT License.


🤝 Contributing

Pull requests are welcome.
Please open an issue first to discuss major changes.


⭐ Support

If this extension helps you, consider starring the repository!

Metadata

Release files for flask-mailjet 0.3.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-mailjet 0.3.7
File Size Uploaded Provenance Actions
flask_mailjet-0.3.7.tar.gz 6.1 kB Without provenance Details for flask_mailjet-0.3.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-mailjet 0.3.7
File Interpreter ABI Platform Actions
flask_mailjet-0.3.7-py3-none-any.whl Python 3 none any Details for flask_mailjet-0.3.7-py3-none-any.whl

Total release size: 11.4 kB

Release files / flask_mailjet-0.3.7.tar.gz

Download URL flask_mailjet-0.3.7.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
15838100918d2645db058740257bb9b026542c4c26f6682d015d5c16f14be6cf
BLAKE2b-256 checksum
How to use checksums
63d541c6d62ec6f6f4e8917df2e99f4b5daf2c9f810ea0b8b8522be312768ea5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / flask_mailjet-0.3.7-py3-none-any.whl

Download URL flask_mailjet-0.3.7-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b7b3165fdf2d9f6205136c62151700a1d542eb6ce9e4c1d385a47d46482934ef
BLAKE2b-256 checksum
How to use checksums
31de475f22b97c51c3e331d75a26f115b8623862a39115ecaf2294b1f85a6ba3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Security for flask-mailjet 0.3.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page