---
"@context": https://schema.org
"@type": TechArticle
"@id": https://www.twilio.com/docs/sendgrid/for-developers/sending-email/quickstart-python#article
headline: "Email API Quickstart: How to Send Email with Python"
description: Sending your first email using the SendGrid REST API and Python.
url: https://www.twilio.com/docs/sendgrid/for-developers/sending-email/quickstart-python
inLanguage: en
dateModified: 2026-08-14T19:01:57.000Z
author:
  "@type": Organization
  name: Twilio Developer Education Team
publisher:
  "@type": Organization
  name: Twilio
---

# Email API Quickstart: How to Send Email with Python

Send your first email using the [Twilio SendGrid Mail Send API][] and [Python][].

## Complete the prerequisites

### Complete the Twilio prerequisites

Complete the following prerequisites before starting this tutorial. You can skip ahead if you've already completed these tasks.

1. [Create a Twilio account in the legacy Console][twilio-account].
2. Enable [two-factor authentication][2fa].
3. Create and store a [Twilio SendGrid API Key][tsg-api-key] with **Mail Send** > **Full Access** permissions.
4. Complete [Domain Authentication][domain-auth].

### Complete the language prerequisites

* [Install Python 3.10 or later][install-py].
* Install the [Twilio SendGrid Python Helper Library][twsg-py]. Using [pip][], run the following command:
  ```bash
  pip install sendgrid
  ```

## Send an API email using Python

An API call requires a resource and an authentication method. To send an email message, the Twilio SendGrid Mail API sends a `POST` request to the Twilio SendGrid API endpoint and authenticates it with an API key.

To send an email message using Python, build a Python app.

1. Create a file in your project directory. For this guide, name the file `app.py`.
2. Import the required Python libraries and classes.
   1. Import the Twilio SendGrid Python library. The library sets the host: `https://api.sendgrid.com/v3/`.
      ```python
      import sendgrid
      ```
   2. Import the Python OS library. As you stored your API key in an environment variable, you need to import the Python `os` library.
      ```python
      import os
      ```
   3. Import the library's `Mail`, `Email`, `To`, and `Content` classes.
      ```python
      from sendgrid.helpers.mail import Mail, Email, To, Content
      ```
   4. Import the API key from the local environmental variable. Assign the key to a variable named `sg` using the SDK's `SendGridAPIClient()` method. The SDK passes your API key to the v3 API in an Authorization header using Bearer token authentication.
      ```python
      sg = sendgrid.SendGridAPIClient(api_key = os.environ.get('SENDGRID_API_KEY'))
      ```
3. Define the values for the To and From email addresses and the subject and body of your email message.

   * Assign these values to the `from_email`, `to_email`, `subject`, and `content` variables.
   * To properly format your email message, the imported constructors can accept and pass the data to a [`personalizations`][personalizations] object that the Python SDK creates. Assign the `to_email` to an email address with an inbox that you can access.
   * The `Content()` helper takes two arguments: the content type and the content itself. Accepted values for the first parameter, content type, include `text/plain` or `text/html`. The second parameter takes the plain text or HTML content you wish to send.

   ```python
   from_email = Email("test@example.com")  # Change to your verified sender
   to_email = To("test@example.com")       # Change to your recipient
   subject = "Sending with Twilio SendGrid is Fun"
   content = Content("text/plain", "and easy to do anywhere, even with Python")
   ```
4. Assign a Twilio SendGrid Python SDK `Mail` constructor to a variable called `mail`. Pass each of the previous variables into this `Mail` constructor.
   ```python
   # Create and assign a Mail constructor to a variable
   mail = Mail(from_email, to_email, subject, content)
   ```
5. Retrieve a JSON-ready representation of the `Mail` object with the Mail constructor's `get()` method.
   ```python
   # Get a JSON-ready representation of the Mail object
   mail_json = mail.get()
   ```
6. To deliver your message, make a `POST` request to the Twilio SendGrid Mail Send API.
   This request passes the `mail_json` value to the Mail Send resource.
   ```python
   # Send an HTTP POST request to /mail/send
   response = sg.client.mail.send.post(request_body=mail_json)
   print(response.status_code)
   print(response.headers)
   ```
7. From your command line, run your `app.py` file. This sends your email message.
   ```shell
   python app.py
   ```
   If you receive a [`202` status code][202] printed to the command line, Twilio SendGrid succeeded in sending your message.
8. Check the inbox of the `to_email` address.
   * Check the inbox of the `to_email` address, and you should see your demo message.
   * If you don't see the email, check your spam folder.

#### View the complete Python code example

```python
import sendgrid
import os
from sendgrid.helpers.mail import Mail, Email, To, Content

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")  # Change to your verified sender
to_email = To("test@example.com")       # Change to your recipient
subject = "Sending with Twilio SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
# Pass the data for the email message in a Mail constructor
mail = Mail(from_email, to_email, subject, content)

# Get a JSON-ready representation of the Mail object
mail_json = mail.get()

# Send an HTTP POST request to /mail/send with the Mail object
response = sg.client.mail.send.post(request_body=mail_json)
print(response.status_code)
print(response.headers)
```

## Troubleshooting

If you receive an error message, see the Twilio [response message documentation][] for clues about what may have gone wrong.

Twilio SendGrid returns all responses in JSON format. To specify this format, add the `Content-Type` header. To interpret the responses to your API requests, the Twilio SendGrid API v3 provides a selection of [response codes][], [content-type headers][], and [pagination][] options.

## Next Steps

To do more with the Twilio SendGrid APIs, see the following resources.

* [API Reference][]
* [Sending Email with Dynamic Transactional Templates][]
* [Getting Started with the Event Webhook][]
* [The Email Activity Feed][]
* [Sender Authentication][]
* [Twilio SendGrid SDK for Python][]

[2fa]: /docs/sendgrid/for-developers/sending-email/authentication#two-factor-authentication

[202]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/202

[API Reference]: /docs/sendgrid/api-reference

[content-type headers]: /docs/sendgrid/api-reference/how-to-use-the-sendgrid-v3-api/responses#content-type-header

[domain-auth]: /docs/sendgrid/ui/account-and-settings/how-to-set-up-domain-authentication

[Getting Started with the Event Webhook]: /docs/sendgrid/for-developers/tracking-events/getting-started-event-webhook

[install-py]: https://www.python.org/downloads

[pagination]: /docs/sendgrid/api-reference/how-to-use-the-sendgrid-v3-api/responses#pagination

[personalizations]: /docs/sendgrid/for-developers/sending-email/personalizations

[pip]: https://pypi.org/project/pip

[Python]: https://www.python.org

[response codes]: /docs/sendgrid/api-reference/how-to-use-the-sendgrid-v3-api/responses#status-codes

[response message documentation]: /docs/sendgrid/api-reference/how-to-use-the-sendgrid-v3-api/responses#status-codes

[Sender Authentication]: /docs/sendgrid/ui/account-and-settings/how-to-set-up-domain-authentication

[Sending Email with Dynamic Transactional Templates]: /docs/sendgrid/ui/sending-email/how-to-send-an-email-with-dynamic-templates

[The Email Activity Feed]: /docs/sendgrid/ui/analytics-and-reporting/email-activity-feed

[tsg-api-key]: /docs/sendgrid/ui/account-and-settings/api-keys

[Twilio SendGrid Mail Send API]: /docs/sendgrid/api-reference/mail-send/mail-send

[Twilio SendGrid SDK for Python]: https://github.com/sendgrid/sendgrid-python

[twilio-account]: https://www.twilio.com/try-twilio

[twsg-py]: https://github.com/sendgrid/sendgrid-python
