sklarsa / django-sendgrid-v5

An implementation of Django's EmailBackend compatible with sendgrid-python v5+
MIT License
323 stars 54 forks source link

Sending mail works with send_mail command but not on password reset form - Invalid Header? #67

Closed AliBharwani closed 3 years ago

AliBharwani commented 4 years ago

Hello, I added password reset functionality with the built in defaults in Django and got it to output emails to a file, but am unable to integrate it with SendGrid. I've tried using this API, my settings are as follows:

SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_BACKEND = "sendgrid_backend.SendGridBackend" 

I tested in shell with send_mail, and it worked. But I tried testing the password reset email on my localhost and received this error -

ImproperlyConfigured at /password_reset/
                SENDGRID_API_KEY must be declared in settings.py

Confusing, as I do have it declared in settings.py. I instead set the variable directly instead of reading it from env, but then trying to password reset gave me a HTTP 403 Forbidden Error.

ponse Internal Server Error: /password_reset/
Traceback (most recent call last):
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/contrib/auth/views.py", line 222, in dispatch
    return super().dispatch(*args, **kwargs)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/views/generic/edit.py", line 142, in post
    return self.form_valid(form)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/contrib/auth/views.py", line 235, in form_valid
    form.save(**opts)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/contrib/auth/forms.py", line 324, in save
    user_email, html_email_template_name=html_email_template_name,
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/contrib/auth/forms.py", line 272, in send_mail
    email_message.send()
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/django/core/mail/message.py", line 276, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/sgbackend/mail.py", line 66, in send_messages
    self.sg.client.mail.send.post(request_body=mail)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/python_http_client/client.py", line 262, in http_request
    self._make_request(opener, request, timeout=timeout)
  File "/Users/alibharwani/Documents/projects/webapps/opendict/django-venv/lib/python3.7/site-packages/python_http_client/client.py", line 178, in _make_request
    raise exc
python_http_client.exceptions.ForbiddenError: HTTP Error 403: Forbidden

I thought it might have something to do with my authentication on SendGrid, as I had authenticated my website so it might work. Testing on my website however, I get this:

2020-08-16T23:24:18.060199+00:00 app[web.1]: 2020-08-16 23:24:18 [10] [ERROR] pathname=/app/.heroku/python/lib/python3.7/site-packages/django/utils/log.py lineno=228 funcname=log_response Internal Server Error: /password_reset/
2020-08-16T23:24:18.060234+00:00 app[web.1]: Traceback (most recent call last):
2020-08-16T23:24:18.060235+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
2020-08-16T23:24:18.060236+00:00 app[web.1]:     response = get_response(request)
2020-08-16T23:24:18.060236+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
2020-08-16T23:24:18.060237+00:00 app[web.1]:     response = self.process_exception_by_middleware(e, request)
2020-08-16T23:24:18.060237+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
2020-08-16T23:24:18.060238+00:00 app[web.1]:     response = wrapped_callback(request, *callback_args, **callback_kwargs)
2020-08-16T23:24:18.060238+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
2020-08-16T23:24:18.060238+00:00 app[web.1]:     return self.dispatch(request, *args, **kwargs)
2020-08-16T23:24:18.060239+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
2020-08-16T23:24:18.060239+00:00 app[web.1]:     return bound_method(*args, **kwargs)
2020-08-16T23:24:18.060239+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
2020-08-16T23:24:18.060240+00:00 app[web.1]:     response = view_func(request, *args, **kwargs)
2020-08-16T23:24:18.060240+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/views.py", line 222, in dispatch
2020-08-16T23:24:18.060240+00:00 app[web.1]:     return super().dispatch(*args, **kwargs)
2020-08-16T23:24:18.060240+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
2020-08-16T23:24:18.060240+00:00 app[web.1]:     return handler(request, *args, **kwargs)
2020-08-16T23:24:18.060240+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/views/generic/edit.py", line 142, in post
2020-08-16T23:24:18.060241+00:00 app[web.1]:     return self.form_valid(form)
2020-08-16T23:24:18.060241+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/views.py", line 235, in form_valid
2020-08-16T23:24:18.060241+00:00 app[web.1]:     form.save(**opts)
2020-08-16T23:24:18.060241+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/forms.py", line 324, in save
2020-08-16T23:24:18.060242+00:00 app[web.1]:     user_email, html_email_template_name=html_email_template_name,
2020-08-16T23:24:18.060242+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/forms.py", line 272, in send_mail
2020-08-16T23:24:18.060242+00:00 app[web.1]:     email_message.send()
2020-08-16T23:24:18.060242+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/django/core/mail/message.py", line 276, in send
2020-08-16T23:24:18.060243+00:00 app[web.1]:     return self.get_connection(fail_silently).send_messages([self])
2020-08-16T23:24:18.060243+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/sgbackend/mail.py", line 66, in send_messages
2020-08-16T23:24:18.060243+00:00 app[web.1]:     self.sg.client.mail.send.post(request_body=mail)
2020-08-16T23:24:18.060243+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/python_http_client/client.py", line 262, in http_request
2020-08-16T23:24:18.060243+00:00 app[web.1]:     self._make_request(opener, request, timeout=timeout)
2020-08-16T23:24:18.060244+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/python_http_client/client.py", line 174, in _make_request
2020-08-16T23:24:18.060244+00:00 app[web.1]:     return opener.open(request, timeout=timeout)
2020-08-16T23:24:18.060244+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/urllib/request.py", line 525, in open
2020-08-16T23:24:18.060244+00:00 app[web.1]:     response = self._open(req, data)
2020-08-16T23:24:18.060245+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/urllib/request.py", line 543, in _open
2020-08-16T23:24:18.060245+00:00 app[web.1]:     '_open', req)
2020-08-16T23:24:18.060245+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/urllib/request.py", line 503, in _call_chain
2020-08-16T23:24:18.060245+00:00 app[web.1]:     result = func(*args)
2020-08-16T23:24:18.060245+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/urllib/request.py", line 1393, in https_open
2020-08-16T23:24:18.060246+00:00 app[web.1]:     context=self._context, check_hostname=self._check_hostname)
2020-08-16T23:24:18.060246+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/urllib/request.py", line 1350, in do_open
2020-08-16T23:24:18.060246+00:00 app[web.1]:     encode_chunked=req.has_header('Transfer-encoding'))
2020-08-16T23:24:18.060247+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/http/client.py", line 1262, in request
2020-08-16T23:24:18.060247+00:00 app[web.1]:     self._send_request(method, url, body, headers, encode_chunked)
2020-08-16T23:24:18.060247+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/http/client.py", line 1303, in _send_request
2020-08-16T23:24:18.060247+00:00 app[web.1]:     self.putheader(hdr, value)
2020-08-16T23:24:18.060248+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.7/http/client.py", line 1240, in putheader
2020-08-16T23:24:18.060248+00:00 app[web.1]:     raise ValueError('Invalid header value %r' % (values[i],))
2020-08-16T23:24:18.060248+00:00 app[web.1]: ValueError: Invalid header value b'Bearer <API_KEY>\n'

where is my the value of my API Key.

Any help would be much appreciated, thank you

more info:

sendgrid==6.0.0
django-sendgrid-v5==0.8.1
Django==3.0.7
sklarsa commented 4 years ago

Can you confirm that os.getenv('SENDGRID_API_KEY') returns your api key? My first instinct is that it's not properly set in your environment