Sending emails with python and Twilio-sendgrid API


  • Python 3.6 or later
  • A free Twilio SendGrid account.

Setting Up the Environment

$ mkdir email_sender
$ cd email_sender
$ python3 -m venv venv
$ source venv/bin/activate
Enter fullscreen mode

Exit fullscreen mode

After activating the environment, Install the necessary packages:

(venv) $ pip3 install sendgrid
(venv) $ pip3 install sendgrid
Enter fullscreen mode

Exit fullscreen mode

NB: the python-dotenv is a Python module that allows you to specify environment variables in traditional UNIX-like β€œ.env” (dot-env) file within your Python project directory. It helps us work with SECRETS and KEYS without exposing them to the outside world, and keep them safe during the development of applications.Read more

Step 1: Create a .env file with the required variables

Enter fullscreen mode

Exit fullscreen mode

Step 2: create an file (can name it according to your own preference)

import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from dotenv import load_dotenv


message = Mail(
    subject='Sending emails with python and Twilio-sendgrid API',
    html_content='<strong>Thankyou for your business</strong>')
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sg.send(message)
except Exception as e:
Enter fullscreen mode

Exit fullscreen mode

Step 3: In the terminal, run your app:

Enter fullscreen mode

Exit fullscreen mode

Step 4: Congratulations πŸŽ‰πŸŽ‰πŸŽ‰ check your inbox!!!

The email arrives to the recipients inbox within seconds


Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

GIPHY App Key not set. Please check settings

An Asynchronous KNX Library Written in Python

Open recent VS Code folders and files using Ulauncher