Python
Official MailSetu Python SDK. Production client for email, SMS, inbound, webhooks, and Verify / OTP.
Installation
bash
pip install mailsetu
# or: pip3 install mailsetuSetup
python
from mailsetu import MailSetu
import os
client = MailSetu(os.environ['MAILSETU_API_KEY'])Send an email
python
response = client.emails.send(
from_='Acme <noreply@acme.co>',
to=['user@example.com'],
subject='Welcome to Acme!',
html='<h1>Welcome!</h1><p>Thanks for signing up.</p>',
)
print(response['id']) # email-xxxxxxxxxxxxxxxx
print(response['status']) # queuedUsing templates
python
client.emails.send(
from_='Acme <noreply@acme.co>',
to=['user@example.com'],
subject='Your order is confirmed',
template_id='tmpl_xxxxxxxxxxxxxxxx',
variables={
'first_name': 'Priya',
'order_number': '#INV-2847',
'amount': '₹2,499',
},
)Error handling
python
from mailsetu import MailSetuError
try:
response = client.emails.send(
from_='Acme <noreply@acme.co>',
to=['user@example.com'],
subject='Welcome!',
html='<p>Hello!</p>',
)
print(f"Sent: {response['id']}")
except MailSetuError as e:
print(f"Error: {e}")
print(f"Code: {e.code}") # e.g. DOMAIN_NOT_VERIFIED
print(f"Status: {e.status}")Start an OTP verification
python
service = client.verify.services.create(
name='Login OTP',
code_length=6,
ttl_seconds=300,
)
verification = client.verify.start(
service_id=service['id'],
to='+919876543210',
idempotency_key='login-otp-1',
)
check = client.verify.check(verification['id'], code='123456')
print(check['approved'])