Search code examples
pythondjangoreduxsmtpsendgrid

SMTPServerDisconnected at /auth/users/


I am currently working with Python 3.10.1 and Django 4.0.1 on the back end, and React/Redux on the front end. I have an app where after a user signs up, an activation email will be sent. However, the email never shows up and while visiting the backend, I see this error inside the network tab in devtools.

enter image description here

I switched from gmail due to the work arounds that ended up not working for me so I went with SendGrid, but haven't gotten that to work yet either.

From all of the posts I have seen so far, this looks right.

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'api'
EMAIL_HOST_PASSWORD = 'password'
DEFAULT_FROM_EMAIL = '[email protected]'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

auth.js

// sign up function
export const signup = (email, password, re_password) => async (dispatch) => {
    const config = {
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': csrftoken,
        },
    };
    const body = JSON.stringify({ email, password, re_password });
    try {
        const response = await axios.post(
            `${process.env.REACT_APP_API_URL}/auth/users/`,
            body,
            config
        );
        dispatch({
            type: SIGNUP_SUCCESS,
            payload: response.data,
        });
    } catch (err) {
        dispatch({
            type: SIGNUP_FAIL,
        });
    }
};

Traceback (if it helps)

Environment:


Request Method: POST
Request URL: https://total-weather-backend.herokuapp.com/auth/users/

Django Version: 4.0.1
Python Version: 3.10.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'api',
 'rest_framework',
 'djoser']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.10/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/app/.heroku/python/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/app/.heroku/python/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/app/.heroku/python/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/app/.heroku/python/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/app/.heroku/python/lib/python3.10/site-packages/rest_framework/mixins.py", line 19, in create
    self.perform_create(serializer)
  File "/app/.heroku/python/lib/python3.10/site-packages/djoser/views.py", line 144, in perform_create
    settings.EMAIL.activation(self.request, context).send(to)
  File "/app/.heroku/python/lib/python3.10/site-packages/templated_mail/mail.py", line 78, in send
    super(BaseEmailMessage, self).send(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/mail/backends/smtp.py", line 69, in open
    self.connection.login(self.username, self.password)
  File "/app/.heroku/python/lib/python3.10/smtplib.py", line 739, in login
    (code, resp) = self.auth(
  File "/app/.heroku/python/lib/python3.10/smtplib.py", line 642, in auth
    (code, resp) = self.docmd("AUTH", mechanism + " " + response)
  File "/app/.heroku/python/lib/python3.10/smtplib.py", line 432, in docmd
    return self.getreply()
  File "/app/.heroku/python/lib/python3.10/smtplib.py", line 405, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")

Exception Type: SMTPServerDisconnected at /auth/users/
Exception Value: Connection unexpectedly closed

Solution

  • The username should be apikey, not api. From the docs:

    To use your API key with the SMTP integration, you must set your username to the string, apikey. Your password will be the API key you generated in the previous step.