Skip to contentSkip to navigationSkip to topbar
Page tools
Useful for sharing or LLM

On this page
Looking for more inspiration?Visit the

Email API Quickstart: How to Send Email with Python


Send your first email using the Twilio SendGrid Mail Send API and Python(link takes you to an external page).


Complete the prerequisites

complete-the-prerequisites page anchor

Complete the Twilio prerequisites

complete-the-twilio-prerequisites page anchor

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(link takes you to an external page).
  2. Enable two-factor authentication.
  3. Create and store a Twilio SendGrid API Key with Mail Send > Full Access permissions.
  4. Complete Domain Authentication.

Complete the language prerequisites

complete-the-language-prerequisites page anchor
  • Install Python 3.10 or later(link takes you to an external page).
  • Install the Twilio SendGrid Python Helper Library(link takes you to an external page). Using pip(link takes you to an external page), run the following command:
    pip install sendgrid

Send an API email using Python

send-an-api-email-using-python page anchor

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/.
      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.
      import os
    3. Import the library's Mail, Email, To, and Content classes.
      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.
      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 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.
    1
    from_email = Email("test@example.com") # Change to your verified sender
    2
    to_email = To("test@example.com") # Change to your recipient
    3
    subject = "Sending with Twilio SendGrid is Fun"
    4
    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.

    1
    # Create and assign a Mail constructor to a variable
    2
    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.

    1
    # Get a JSON-ready representation of the Mail object
    2
    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.

    1
    # Send an HTTP POST request to /mail/send
    2
    response = sg.client.mail.send.post(request_body=mail_json)
    3
    print(response.status_code)
    4
    print(response.headers)
  7. From your command line, run your app.py file. This sends your email message.

    python app.py

    If you receive a 202 status code(link takes you to an external page) 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.

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.


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