Skip to main content

A Flask extension for sending emails via Mailjet API.

Project description

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!

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

flask_mailjet-0.3.5.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

flask_mailjet-0.3.5-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file flask_mailjet-0.3.5.tar.gz.

File metadata

  • Download URL: flask_mailjet-0.3.5.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for flask_mailjet-0.3.5.tar.gz
Algorithm Hash digest
SHA256 cacd6d0cfe05901fb4242a30e14dd06ade7109af59bd81b400cbda163cd2f1a2
MD5 3d693b656cde29997b93e121f5c7d5cf
BLAKE2b-256 31104404ef6e5caf9f8aca64e8b3afb8aec1a1b2efa114cf24d04752be5cf725

See more details on using hashes here.

File details

Details for the file flask_mailjet-0.3.5-py3-none-any.whl.

File metadata

  • Download URL: flask_mailjet-0.3.5-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for flask_mailjet-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e3e2948ab4a29efc445c1dc05c2879e9d1d32f219df03a48fbb8af8de688dd72
MD5 367e51d80064a9c39dbc27b9873d0fbb
BLAKE2b-256 9a7047cbe054ea3b239a8a219974fbbf7a2a062f4712591db50eb754e3a08aba

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