SMTPServerDisconnected 在 /auth/users/

SMTPServerDisconnected at /auth/users/

我目前在后端使用 Python 3.10.1 和 Django 4.0.1,在前端使用 React/Redux。我有一个应用程序,在用户注册后,将发送一封激活电子邮件。但是,电子邮件从未出现,在访问后端时,我在 devtools 的网络选项卡中看到了这个错误。

由于变通方法最终对我不起作用,所以我从 gmail 切换了,所以我选择了 SendGrid,但也没有让它起作用。

从我目前看到的所有帖子来看,这看起来是正确的。

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@gmail.com'
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,
        });
    }
};

回溯(如果有帮助的话)

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

用户名应该是 apikey,而不是 apiFrom 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.

如果您使用的是免费的 SendGrid 帐户,则需要 single sender verification。此附加步骤有助于 SendGrid 遵守反垃圾邮件法。

要修复它,请遵循 SendGrid 的 instructions to verify your email account. Sending emails from free services like gmail.com or yahoo.com is no longer supported. This is due to DMARC email authentication protocol。因此,现在要发送实际电子邮件,您必须使用自定义的非免费电子邮件帐户,您可以验证其所有权。