Sendgrid-python: HTTP Error 403: Forbidden with first email

Created on 23 Apr 2020  路  11Comments  路  Source: sendgrid/sendgrid-python

Issue Summary

I was setting up sendgrid with python and followed the sendgrid guide, then when I tried to send the first email I got the error

Steps to Reproduce

  1. Install sendgrid package (pip install sendgrid)
  2. paste in code and change to correct variables

Code Snippet

# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='EMAIL',
    to_emails='EMAIL',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')
try:
    sg = SendGridAPIClient(
        'KEY'
        )
    response = sg.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e)

Exception/Log

HTTP Error 403: Forbidden

Technical details:

  • sendgrid-python version: 6.2
  • python version: 3.6.8
question

Most helpful comment

I solve the issue by verifying the from_email (which is also sender email) address from the dashboard.

All 11 comments

I am also running into this.

  • python version: 3.5.9
  • sendgrid-python version: 6.2.2

Though the example above doesn't seem to actually include the api key, but I did, via:

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

@mmoomocow Recommend upgrading to a later release of this library. I'm not even seeing a 3.2.7 version.

@jaimemarijke Are you seeing this 403 when performing the same mail send operation?

@childish-sambino Yes, I got the 403 after following these instructions: https://app.sendgrid.com/guide/integrate/langs/python

I use pyenv to set the python version in my repo, and tox to create a python virtual environment (py35)

鈺攢(py35) jaime@kulshan /Users/jaime/personal/small-questions-email  <master*>
鈺扳攢$ python --version
Python 3.5.9
鈺攢(py35) jaime@kulshan /Users/jaime/personal/small-questions-email  <master*>
鈺扳攢$ pip freeze | ag sendgrid
sendgrid==6.2.2
鈺攢(py35) jaime@kulshan /Users/jaime/personal/small-questions-email  <master*>
鈺扳攢$ printenv | ag SENDGRID                                                                                                                                                                                                               
SENDGRID_API_KEY=<my key>
鈺攢(py35) jaime@kulshan /Users/jaime/personal/small-questions-email  <master*>
鈺扳攢$ cat small_questions_email/sendgrid.py

# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='[email protected]',
    to_emails='[email protected]',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</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)
鈺攢(py35) jaime@kulshan /Users/jaime/personal/small-questions-email  <master*>
鈺扳攢$ python -m small_questions_email.sendgrid
Traceback (most recent call last):
  File "/Users/jaime/personal/small-questions-email/small_questions_email/sendgrid.py", line 15, in <module>
    response = sg.send(message)
  File "/Users/jaime/personal/small-questions-email/.tox/py35/lib/python3.5/site-packages/sendgrid/sendgrid.py", line 95, in send
    response = self.client.mail.send.post(request_body=message.get())
  File "/Users/jaime/personal/small-questions-email/.tox/py35/lib/python3.5/site-packages/python_http_client/client.py", line 262, in http_request
    self._make_request(opener, request, timeout=timeout)
  File "/Users/jaime/personal/small-questions-email/.tox/py35/lib/python3.5/site-packages/python_http_client/client.py", line 178, in _make_request
    raise exc
python_http_client.exceptions.ForbiddenError: HTTP Error 403: Forbidden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jaime/.pyenv/versions/3.5.9/lib/python3.5/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/jaime/.pyenv/versions/3.5.9/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/jaime/personal/small-questions-email/small_questions_email/sendgrid.py", line 20, in <module>
    print(e.message)
AttributeError: 'ForbiddenError' object has no attribute 'message'

I had the same issue until I finally got a user email registered. So you might need to check to see if the domain or sender email is validate on the dashboard.

I also used the without helper class example.

Good luck.

+1 to @devsetgo's comment. @jaimemarijke Let me know if that doesn't resolve it for you.

Also, try changing the exception logic to log the response body for more details:

print(e)
print(e.body)

Hi there folks, I'm facing the same issue. I'm getting 403 forbidden error when I try to send my first e-mail. I already checked my e-mail. How do I check my account on dashboard? Or is it the same thing? @childish-sambino @devsetgo

@thiagolara Have you tried logging the response body for additional error details?

Hey @childish-sambino , yes I just did it and I got this message:

b'{"errors":[{"message":"The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements","field":"from","help":null}]}'

I was using my customer's email as sender in a contact form.

I solved the problem changing the "from email" (sender email) to my own account email, instead of using my customer's email :)

As we can see on the link above, they changed it April 6, 2020.

Thank you !

I solve the issue by verifying the from_email (which is also sender email) address from the dashboard.

You might need to do a Sender Identity verification. Checkout this page:
https://sendgrid.com/docs/ui/sending-email/sender-verification/

Was this page helpful?
0 / 5 - 0 ratings