How to Send Email with Python

Install the official mailsurge package and send transactional email from Python 3.10+.

Before you start

You need a verified domain and API key in the Mailsurge dashboard. The from_ address must use that domain. See the API docs for field formats and response codes.

Install the official package

Add the mailsurge package to your environment. Python 3.10+ is required.


                                                1
                                pip install mailsurge
                    

Send with the SDK

Set MAILSURGE_API_KEY in your environment. Use from_ because from is reserved in Python.


                                                1
                                from mailsurge import Mailsurge, MailsurgeRateLimitError
                                                2
                                
                                                3
                                with Mailsurge() as client:
                                                4
                                    try:
                                                5
                                        client.messages.send(
                                                6
                                            from_='Acme <no-reply@yourdomain.com>',
                                                7
                                            to=['customer@example.com'],
                                                8
                                            subject='Password reset',
                                                9
                                            html='<p>Reset your password using the secure link.</p>',
                                                10
                                            text='Reset your password using the secure link.',
                                                11
                                        )
                                                12
                                    except MailsurgeRateLimitError as error:
                                                13
                                        print('Sending limit reached:', error.body.get('limit'))
                                                14
                                        raise
                    

Production checklist

Keep sends off the web request path and handle API failures explicitly.

  • Trigger sends from Celery, RQ, Django-Q, or your platform background worker so request threads stay fast.
  • Use verified domains for every from address.
  • Catch typed SDK exceptions for auth, validation, and rate limits.
  • Pair this integration with the deliverability checklist before increasing volume.

Continue reading

Related guides to help you ship production-ready transactional email.

Sending Email with Node.js

Official Node.js client with typed errors.

Read guide

Sending Email with PHP

Framework-agnostic cURL example for any PHP app.

Read guide
Also available: the official Node.js SDK and the Laravel package.

Ready to send your first email?

Verify a domain, create an API key, and call POST /api/v1/message from your stack.