Install bavimail, set your API key, and call client.emails.send() with an inbox id, a list of recipients, a subject and an HTML body. The snippets below use bavimail 1.0.0, the current release on PyPI, which needs Python 3.9 or later. The last section receives the reply on a signed webhook and answers in the same thread.
Bavimail email is Live, with a free plan.
What you need
- A Bavimail plan with a domain allowance: Developer ($5 a month) includes 10 domains with 10 inboxes each, and the catalog lists no custom-domain allowance on Free. See pricing.
- A verified domain with at least one inbox on it. See domains.
- An API key from the dashboard.
- Python 3.9 or later.
1. Install the SDK and set your key
pip install bavimail
export BAVIMAIL_API_KEY=your_api_key2. Find the address to send from
Each address on your domain is an inbox with an id. emails.send() takes the id.
import os
from bavimail import Bavimail
client = Bavimail(api_key=os.environ["BAVIMAIL_API_KEY"])
for inbox in client.inboxes.list():
print(inbox.id, inbox.full_email)No inbox yet? client.inboxes.create(domain_id, "agent") creates [email protected] on a verified domain.
Set the id of the address you want as BAVIMAIL_INBOX_ID.
3. Send an email
import os
from bavimail import Bavimail
client = Bavimail(api_key=os.environ["BAVIMAIL_API_KEY"])
email = client.emails.send(
inbox_id=os.environ["BAVIMAIL_INBOX_ID"],
to_emails=["[email protected]"],
subject="Hello from Python",
body="<p>Sent with the Bavimail Python SDK.</p>",
)
print(email.id, email.status)It prints the email id and queued.
4. Check delivery
A few seconds later:
print(client.emails.get(email.id).status)In our October 14 test it printed delivered 10 seconds after the send.
5. Read replies
import os
from bavimail import Bavimail
client = Bavimail(api_key=os.environ["BAVIMAIL_API_KEY"])
page = client.inbound_emails.list(inbox_id=os.environ["BAVIMAIL_INBOX_ID"], limit=10)
for message in page.data:
print(message.received_at, message.from_address, message.subject)To get replies as they arrive instead of polling, use a webhook, as below.
Receive the reply and answer in the thread
Every message to the inbox fires an email.inbound.received event. Its data carries email_id, inbox_id, inbox_address, from_email, from_name, subject, body_preview, attachment_count and received_at. The handler verifies the signature, acknowledges at once, then fetches the full message and replies from the same inbox with its thread_id and in_reply_to, so the answer lands in the sender's existing thread.
pip install fastapi uvicornimport json
import os
from fastapi import BackgroundTasks, FastAPI, Request, Response
from bavimail import Bavimail, WebhookVerificationError, verify_webhook_signature
client = Bavimail(api_key=os.environ["BAVIMAIL_API_KEY"])
app = FastAPI()
answered: set[str] = set() # event ids already handled; use your database in production
def answer(email_id: str) -> None:
message = client.inbound_emails.get(email_id)
if message.from_address == message.inbox_address:
return
reply = "<p>Thanks, we received your message.</p>" # have your agent write this from message.body_text
subject = message.subject if message.subject.lower().startswith("re:") else f"Re: {message.subject}"
client.emails.send(
inbox_id=message.inbox_id,
to_emails=[message.reply_to or message.from_address],
subject=subject,
body=reply,
thread_id=message.thread_id,
in_reply_to=message.message_id,
)
@app.post("/webhooks/bavimail")
async def bavimail_webhook(request: Request, background: BackgroundTasks) -> Response:
body = await request.body()
try:
verify_webhook_signature(
payload=body,
signature=request.headers.get("x-webhook-signature", ""),
timestamp=request.headers.get("x-webhook-timestamp", ""),
secret=os.environ["BAVIMAIL_WEBHOOK_SECRET"],
)
except WebhookVerificationError:
return Response(status_code=403)
event = json.loads(body)
if event.get("type") == "webhook.verification":
await client.webhooks.verify_async(os.environ["BAVIMAIL_WEBHOOK_ID"], event["verification_code"])
elif event.get("event_type") == "email.inbound.received" and event["event_id"] not in answered:
answered.add(event["event_id"])
background.add_task(answer, event["data"]["email_id"])
return Response(status_code=200)Run it with uvicorn app:app --port 8000 behind a public HTTPS URL, then register the webhook once:
wh = client.webhooks.create(
url="https://agent.example.com/webhooks/bavimail",
event_types=["email.inbound.received"],
)
print(wh.id, wh.secret) # the secret is shown onceSet BAVIMAIL_WEBHOOK_ID and BAVIMAIL_WEBHOOK_SECRET from that output, restart the app, and call client.webhooks.resend_verification(wh.id). The app receives the signed challenge and activates the webhook with webhooks.verify; until then the webhook delivers nothing.
What the code relies on:
- Signature.
X-Webhook-Signatureissha256=<hex>, an HMAC-SHA256 of{X-Webhook-Timestamp}.{raw body}keyed with the hex-decoded secret.verify_webhook_signaturechecks it and rejects timestamps more than 5 minutes old. Pass the raw bytes, not re-serialized JSON. - Acknowledge first. Bavimail waits 10 seconds for an answer. A non-2xx answer is retried, up to 6 attempts in all; a timeout is not retried and leaves the delivery for an operator to resolve. So the handler answers 200 at once and writes the reply in a background task.
- Duplicates.
event_idstays the same across redeliveries, so the handler records it and answers once. - Threading.
thread_idkeeps the reply in the same Bavimail thread, and Bavimail writes theIn-Reply-ToandReferencesheaders so mail clients thread it too. - Loops. The handler skips mail the inbox sent to itself. If your agent may talk to other automated senders, add your own stop rule.
The SDK can also do the verification and dispatch for you: client.webhook_router("/webhooks/bavimail", secret=..., on_verification=...) returns a FastAPI router that calls handlers registered with @client.on(EventType.INBOUND_RECEIVED); it awaits them before answering, so keep them fast. See webhooks.
FAQ
How do I send an email with Python?
Install bavimail with pip, set BAVIMAIL_API_KEY, and call client.emails.send() with an inbox id, a list of recipients, a subject and an HTML body.
Which Python versions does the SDK support?
Python 3.9 or later.
How do I know the email was delivered?
Call client.emails.get(email_id) and read status. A new email starts as queued and moves to delivered.
How do I receive replies and answer them in Python?
Subscribe a webhook to email.inbound.received, verify each delivery with verify_webhook_signature, fetch the message with client.inbound_emails.get(email_id), and answer with client.emails.send() from the same inbox with thread_id and in_reply_to set from the message.
How much does it cost?
The free plan has 6,000 emails a month and 200 a day, with no credit card. The Developer plan is US$5 a month for 50,000 emails. See pricing.