Sending Emails from Python Flask Applications with Twilio SendGrid

Grab the opportunity of subscribing with no charge HandySends account. We guide you at every step, simply conduct a few steps and register for a FREE trial account and you will be entitled to sending one hundred emails each day and over 40050 emails for a month.

20/08/2020 By

HandySends Applications SendGrid

If you haven’t migrated to Python Flask Application supported by Twilio SendGrid, you've missed the countdown and your business is currently running on an unsupported application. Python Flask Application is the power-packed dominating application that promises to punch in swift ability to send emails to your users with absolute ease. In a world where online security and customer database protection is the top priority, still functioning on an unsupported language is both risking your business and customers' privacy. A data hack on your business and customer database could cause some serious impact to your business reputation and customer loyalty.

What is meant by Python Flask-Mail

Flask-Mail is constructed using the most basic Flask config API. Available in various options and emails are managed through email instance.


Still, if all is not lost, you need to connect with a Python Flask application service provider and have your business migrate to the updated technology. The migration service might look like a daunting task but several benefits await you including an improvised interface and enhanced performance The interface has been improvised not only for the backend operations but gives the customers a mobile-friendly user interface. Python Flask-Mail is indeed one of the most helpful applications.

Deciding on the fact if you would like to purchase or build an email structure is not a cakewalk decision. You need to calculate if your bandwidth can support and sustain the email deliverability issues? Are you able to match pace with the volume as this is bound to grow with the increasing business? Have you matched the rates well and done your comparison? These are just a few of the important factors that you ought to keep in mind while making a decision.

Our experts will take you on a tour so you are aware of every aspect to send emails using committed email services. Post-reading the article, you will be able to understand how this email management system works and functions. We take a small example and run you through the process of how to send email to vide Handysends using the Python Flask Applications and Flask-email extensions in your email.

Understanding the basic Tutorial tools:

The following elements are essential to follow the guide through:

Your system must be installed and ready with Python three point six 93.6), and check thoroughly that the system supports the functionalities –you have to download it from and the installer will assist you the process.
Grab the opportunity of subscribing with no charge Handysends account. We guide you at every step, simply conduct a few steps and register for a FREE trial account and you will be entitled to sending one hundred emails each day and over 40050 emails for a month.

Let’s get going with SendGrid, the email accelerator

How and what are the key factors to keep in mind when sending emails through SendGrid.

Generate a smart API key [ this will help you authenticate your access to all services offered by Handysends)
Simply log into the Account using the details shared with you by our team.

Click the “settings’ option followed by API keys – you have successfully generated the API key.


Try and assign a simple name to your API smart key (remember complicated names and use of too many combinations often lands you in trouble, hence create a simple easy to remember API Key.

We are discussing pure Flask mail application

Select “Full Access” this will give you complete permission and your assigned key the capacity to function all email functionalities [egg. Delete/Get/Patch/Put and even Post options or endpoints as it usually referred]
Note: In case you are looking at having special permission –you have to opt for “Limited Access “and you are free to configure as required.

When you opt for the option [Create and View] option and click the tab –you can view your key. Make sure you copy the key as it is visible and appears on the screen only once. Save the key securely so that it can be used later. You are not supposed to lose the Key. Once saved the “Done/complete” tab needs to click to complete the process.

How Python works

In the next step, we shall discuss the method of sending emails from the simplest featured Python Flask application, after our set up is complete. We shall create a new organized list and name it as *Handysends-tests or alike, for example, and then within the new directory, we will create a fresh virtual environment with Python.
Please make a note of the commands used for Mac & users who prefer UNIX.
The most generic commands for For Mac and UNIX users are as follows:


In your virtual Python environment, the second step is to install the Python Flask Applications which comprises of [Flask, Flask-Mail, and python-dotenv]

How will you set up a Flask Python application for your work

Let us follow the steps as in the file [] and note how to start Flask


On this page, you can figure out how to configure him most talked about Flask-mail to work with the SMTP service of SendGrid’s Handysends. You are requested to make a note of the main settings:

The settings for mail-server must be ‘`.

Set the email-port at 587 ( check out our blog posts to find out which important ports to use, which is the correct one )

You must enable the TLS ( learn how to differentiate between SSL & TLS )

Set ‘Authentication’ as required.

Please use ‘apikey’ as username ( this remains same in all Handysends accounts )

The Handysends API key created is simply the password ( we have learned about this step in previous steps )

Right from setting up of extension of the Flask-Mail, we need to add the Flask application settings with correct configuration keys, import from such a setting inconstant API key named `SENDGRID_API_KEY’ is imported ensure security of the application. From an environment inconstant a defaulter sender email id is generated. In the “from’ filed the email id will be visible to all.

How would you generate a.env file having two must-have variables:


The variables discussed will be automatically imported by the Flask in the given [.env] file. The two variables configured are sufficient in the Flask application. The automatic import takes place till the time you possess the smart [python-dotenv] packages installed.

How will you test to send an e-mail

Now let’s practice ourselves, how to send a test mail to yourself from Python shell:
(venv) $ flask shell

The Python application(shell) was initiated with the ‘flask-shell’ command that confirms that the Flask application built-in is imported.
The 'mail" instance created in is imported, once in the shell, and the class of the ‘Message’ from Flask-Mail.

from app import mail


With all settings & configurations in place now, the email should function smoothly and you should start receiving emails in your inbox within a few seconds.


At the bottom of the, you can add a simple Flask Route sample that can integrate with these functions.


The index.html template file can be added in order, to sum up, the application in the example. A list for your templets needs to be created.

(venv) $ mkdir templates

The following data and information in file templates/index.html needs to be written


Too quickly search and check to try typing http://localhost:5000 in the desired web browser and to access the application and see what appears:

Correct and authenticate email address of the subscriber is important need to be added and a test mail will be directed to the address with a click of a button.

Troubleshooting complications related to sending emails.

The mails can start functioning smoothly now, by applying the Python Flask application as mentioned above. Even after the mail is sent successfully to the recipient, if the mail fails to deliver, it might so happen that the recipient’s mail server might have rejected your email. With SendGrid the delivery errors are seen after the dispatcher concludes distributing emails, this is because the emails are sent asynchronously, and the application does not show any notification that an error has occurred.

However, we have a solution to rectify the above situation in the form of the Handysends dashboard. This can help you to debug errors while sending emails. Some steps can be followed to execute this option. 1st click on activity from the SendGrid dashboard and then click "show every activity” that appears in the center of the web page. The display featured on the screen will show the entire list of emails send from your side and also shows the emails whether effectively distributed or the other way round.


The diagram shows how it appears on your computer screen, the list of delivered and undelivered emails. You can find out the details of the email just by clicking on the undelivered email address, you can also be able to view the error report directed by the recipient server.

The delivery problems can be sorted out in various ways after you find out which emails have not been delivered or have bounced back. For example, common spelling errors do occur quite often and the same can be rectified by using an Email Authentication tool. These common spelling/typo errors can be in the form of words missing in email extension names like, or,, etc

A failed delivery can be caused by several other issues also, this might be due to unnavigable IP address or bad sending reputation. Our email delivery guide has complete tutorials that discuss all email delivery.

Summing up Notes:

Twilio SendGrid can be very easily integrated into the initiation of Flask email sending workflow. It can be seen in the tutorial that sending an email is a very simple task.

Now no need to explore any more options or venture around any further, as you have a complete solution of receiving emails from the Flask Framework in Python. We request you to read our detailed article on HandySends.

You can also visit our Knowledge Centre that can equip you with much more insight on how to integrate with Handysends’ or use our APIs to send emails.

Signup To Access Free Trial

  • 40,000 Free Emails for 1st Month 
  • Extensive Support Documentation
  • Multi-Language Customer Support
Make sure you use an email you can access.