Bootstrap FreeKB - Flask - Send Email using SendGrid
Flask - Send Email using SendGrid

Updated:   |  Flask articles

Flask uses the MVC (Model View Controller) Framework. Just to make this as obvious as possible, I like my Flask apps to have the following.

  • Model -> models.py
  • View -> views.py
  • Controller -> __init__.py

Let's say your Flask app has the following structure.

├── main.py
├── database (directory)
│   ├── example.db
├── my-project (directory)
│   ├── __init__.py
│   ├── views.py
│   ├── models.py
│   ├── templates (directory)
│   │   ├── base.html
│   │   ├── home.html
│   │   ├── results.html
│   └── static (directory)
│       └── custom.css

 

First, use pip to install sendgird.

pip install sendgrid

 

Or, you can specify the version to install

pip install sendgrid==6.9.7

 

Or, better yet, use a requirements.txt file.

sendgrid==6.9.7

 

And then install the packages using the requirements.txt file.

pip install --requirement requirements.txt

 

Your view (views.py in this example) could have something like this.

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Email, To

@views.route('/sendEmail')
def email():

    from_email   = Email("john.doe@example.com")
    to_emails    = To("jane.doe@example.com")
    subject      = "Hello"
    html_content = "<strong>World</strong>"

    message = Mail(from_email, to_emails, subject, html_content)

    try:
        sg = SendGridAPIClient('YOUR API KEY')
        response = sg.send(message)
        print(f"response.status_code = {response.status_code}")
        print(f"response.body        = {response.body}")
        print(f"response.headers     = {response.headers}")
        return "Email has been sent"
    except Exception as exception:
        print(exception.message)
        return exception.message

 

It is important to recognize that if you are sending an email campaign, when using to_emails each recipient will be able to see the other recipient emails, which is not ideal.

 

bcc (blind carbon copy) can be used to hide each recipients email.

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

@views.route('/sendEmail')
def email():

    personalization = Personalization()

    recipients = ['john.doe@example.com', 'jane.doe@example.com']

    message = Mail(
        from_email="no-reply@example.com.com"
    )

    message = Mail(from_email, "example subject", "example content")

    personalization.add_to(To('no-reply@example.com'))

    for bcc_addr in recipients:
        personalization.add_bcc(Bcc(bcc_addr))

    message.add_personalization(personalization)    

    try:
        sg = SendGridAPIClient('YOUR API KEY')
        response = sg.send(message)
        print(f"response.status_code = {response.status_code}")
        print(f"response.body        = {response.body}")
        print(f"response.headers     = {response.headers}")
        return "Email has been sent"
    except Exception as exception:
        print(exception.message)
        return exception.message

 

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 8cebb7 in the box below so that we can be sure you are a human.