How to Send Email with Python: Complete Guide
September 21, 2026 · 10 min read
TL;DR: This guide shows you how to send emails with Python using MailForge REST API. Includes basic emails, HTML content, attachments, and webhook integration.
Prerequisites
Before we begin, you'll need:
- Python 3.7 or higher
- A MailForge account (free at mailforge.dev/register)
- Your API key from the dashboard
Method 1: Using the MailForge Python SDK (Recommended)
First, install the SDK:
pip install mailforge
Now send your first email:
import mailforge
# Initialize the client
client = mailforge.Client(api_key="your_api_key_here")
# Send an email
response = client.emails.send(
to="recipient@example.com",
from_email="sender@yourdomain.com",
subject="Welcome to Our Service",
html="<h1>Welcome!</h1><p>Thank you for signing up.</p>"
)
print(f"Email sent! ID: {response['id']}")
Method 2: Using REST API Directly
For more control, use the REST API directly:
import requests
# Send email via REST API
response = requests.post(
"https://api.mailforge.dev/v1/emails",
headers={
"Authorization": "Bearer your_api_key_here",
"Content-Type": "application/json"
},
json={
"to": "recipient@example.com",
"from": "sender@yourdomain.com",
"subject": "Welcome!",
"html": "<h1>Welcome!</h1>"
}
)
print(response.json())
Method 3: Using SMTP
import smtplib
from email.mime.text import MIMEText
msg = MIMEText("<h1>Welcome!</h1><p>Thank you for signing up.</p>", 'html')
msg['Subject'] = 'Welcome to Our Service'
msg['From'] = 'sender@yourdomain.com'
msg['To'] = 'recipient@example.com'
with smtplib.SMTP('smtp.mailforge.dev', 587) as server:
server.starttls()
server.login('your_api_key', 'your_api_secret')
server.send_message(msg)
Sending with Attachments
import mailforge
client = mailforge.Client(api_key="your_api_key_here")
response = client.emails.send(
to="recipient@example.com",
from_email="sender@yourdomain.com",
subject="Your Invoice",
html="<h1>Invoice Attached</h1><p>Please find your invoice attached.</p>",
attachments=[
"invoice.pdf"
]
)
Tracking Delivery with Webhooks
Set up a webhook endpoint to receive delivery notifications:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
event = request.json
event_type = event.get('event')
email_id = event['data']['id']
if event_type == 'delivered':
print(f"Email {email_id} delivered!")
elif event_type == 'bounced':
print(f"Email {email_id} bounced!")
return jsonify({'status': 'ok'})
Start Sending Emails with Python
Free plan includes 100 emails/day. No credit card required.
Get Started Free