How to Send Emails with a CC and BCC using Python and Twilio SendGrid

August 15, 2023
Written by
Sam Agnew
Twilion

Copy of C04 Blog Text(2)

So you're building a Django or Flask app and you need to programmatically send some emails. The Twilio SendGrid API for sending email is a great solution to this problem. The code for simply sending an email from one address to another address is straightforward, but adding recipients in the CC and BCC fields is another common task that requires slightly more effort.

If you have a SendGrid account and an API key set as an environment variable, here is all the code you need to send an email in Python, with addresses in both the CC and BCC fields:

 

 

 

 

import os

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Personalization, Cc, Bcc, To


sg_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))

cc_recipients = ['FIRST_CC_EMAIL', 'SECOND_CC_EMAIL']
bcc_recipients = ['FIRST_BCC_EMAIL', 'SECOND_BCC_EMAIL']

email = Mail(from_email='FROM_EMAIL_ADDRESS',
               subject='Sending with SendGrid is Fun',
               html_content='<strong>And easy to do anywhere with Python!</strong>')

personalization = Personalization()

personalization.add_to(To('TO_EMAIL_ADDRESS'))

for cc_address in cc_recipients:
    personalization.add_cc(Cc(cc_address))

for bcc_address in bcc_recipients:
    personalization.add_bcc(Bcc(bcc_address))

email.add_personalization(personalization)

response = sg_client.send(email)

Let's walk through how to get this running step by step.

Developer Environment Setup

Make sure we have the right software installed that we'll need to use for the rest of this post. Throughout this post you will need:

Here is a good guide to follow on setting up your development environment if you are going to be doing more web development with Python.

Sign up for SendGrid and create an API key

The first thing you need to do is create a SendGrid account. You can choose the free tier for the purpose of this tutorial. Once you have an account, you need to create an API key as seen in this screenshot. You can name it whatever you want, but once it is created make sure you save it before moving on!

Creating a SendGrid API Key

A good way to save this API key is to set it as an environment variable that you can access from your Python code in order to avoid writing it directly in your code. Set the value of the SENDGRID_API_KEY environment variable to be the API key from your SendGrid account. Here's a useful tutorial if you need help setting environment variables. We will use this later on.

Sending an Email using CC and BCC with Python

Now that you have a SendGrid account and an API key, you're ready to dive into some code and send emails! Start by opening your terminal and navigating to the directory where you want your project to live.

First install the virtualenv package then run the following command:

 

 

 

 

virtualenv emails

After either of those steps, activate the virtual environment:

 

 

 

 

source ./emails/bin/activate

Install the SendGrid Python helper library in the virtualenv:

 

 

 

 

pip install sendgrid

Now create a file called send_email.py in this directory and add the following code to it:

 

 

 

 

import os

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Personalization, Cc, Bcc, To


sg_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))

cc_recipients = ['FIRST_CC_EMAIL', 'SECOND_CC_EMAIL']
bcc_recipients = ['FIRST_BCC_EMAIL', 'SECOND_BCC_EMAIL']

email = Mail(from_email='FROM_EMAIL_ADDRESS',
               subject='Sending with SendGrid is Fun',
               html_content='<strong>And easy to do anywhere with Python!</strong>')

personalization = Personalization()

personalization.add_to(To('TO_EMAIL_ADDRESS'))

for cc_address in cc_recipients:
    personalization.add_cc(Cc(cc_address))

for bcc_address in bcc_recipients:
    personalization.add_bcc(Bcc(bcc_address))

email.add_personalization(personalization)

response = sg_client.send(email)

Before running this code, make sure you have the SENDGRID_API_KEY environment variable set, and remember to replace the email values with the addresses you want to send emails to and from.

Finally, in your terminal run the following command to run this code to send yourself a picture of an email:

 

 

 

 

python send_email.py

Check your inbox and you should see something like this!

An email successfully sent to your inbox

What next?

You just successfully sent your first email using Python, but what if you want to do more than just send emails? You can also process and respond to incoming emails using SendGrid's Inbound Email Parse Webhook. You can also check out the SendGrid docs for a ton of other cool features and uses.

I’m looking forward to seeing what you build. Feel free to reach out and share your experiences or ask any questions.