Sending emails with python and Twilio-sendgrid API

Prerequisites

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

After activating the environment, In…


This content originally appeared on DEV Community and was authored by Emma Donery

Prerequisites

  • 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

After activating the environment, Install the necessary packages:

(venv) $ pip3 install sendgrid
(venv) $ pip3 install sendgrid

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

SENDGRID_API_KEY="mskj.bjsknlfjlsjnsnsnkjaknkkkwnkakJKkjnkKKNXKBVThdubsckeuisi"

Step 2: create an app.py 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

load_dotenv()

message = Mail(
    from_email='senders_email@mail.com',
    to_emails='recipients_email@mail.com',
    subject='Sending emails with python and Twilio-sendgrid API',
    html_content='<strong>Thankyou for your business</strong>')
try:
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sg.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e.message)

Step 3: In the terminal, run your app:

python3 app.py

Step 4: Congratulations ??? check your inbox!!!

The email arrives to the recipients inbox within seconds


This content originally appeared on DEV Community and was authored by Emma Donery


Print Share Comment Cite Upload Translate Updates
APA

Emma Donery | Sciencx (2021-09-08T20:24:23+00:00) Sending emails with python and Twilio-sendgrid API. Retrieved from https://www.scien.cx/2021/09/08/sending-emails-with-python-and-twilio-sendgrid-api/

MLA
" » Sending emails with python and Twilio-sendgrid API." Emma Donery | Sciencx - Wednesday September 8, 2021, https://www.scien.cx/2021/09/08/sending-emails-with-python-and-twilio-sendgrid-api/
HARVARD
Emma Donery | Sciencx Wednesday September 8, 2021 » Sending emails with python and Twilio-sendgrid API., viewed ,<https://www.scien.cx/2021/09/08/sending-emails-with-python-and-twilio-sendgrid-api/>
VANCOUVER
Emma Donery | Sciencx - » Sending emails with python and Twilio-sendgrid API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/08/sending-emails-with-python-and-twilio-sendgrid-api/
CHICAGO
" » Sending emails with python and Twilio-sendgrid API." Emma Donery | Sciencx - Accessed . https://www.scien.cx/2021/09/08/sending-emails-with-python-and-twilio-sendgrid-api/
IEEE
" » Sending emails with python and Twilio-sendgrid API." Emma Donery | Sciencx [Online]. Available: https://www.scien.cx/2021/09/08/sending-emails-with-python-and-twilio-sendgrid-api/. [Accessed: ]
rf:citation
» Sending emails with python and Twilio-sendgrid API | Emma Donery | Sciencx | https://www.scien.cx/2021/09/08/sending-emails-with-python-and-twilio-sendgrid-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.