[解决]Django smtp gmail在生产中无法使用。

问题描述 投票:0回答:1

我使用django通过gmail smtp发送邮件。然而,它只在部署前工作。在生产或部署过程中,不管你怎么称呼,当我尝试发送邮件时,它一直在加载,并且只显示 "服务器错误500"。

以下是我的部分内容 settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = config['EMAIL_USER']
EMAIL_HOST_PASSWORD = config['EMAIL_PASS']

以下是 .../django_project/users/views.py

from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm

def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f'Your account has been created! You are now able to log in.')
            return redirect('login')
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})

@login_required()
def profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST,
                                   request.FILES,
                                   instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'Your account has been updated!')
            return redirect('profile')
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'users/profile.html', context)

让人困惑的是,我的教程视频没有把任何从 django.core.mail import send_mail 或什么的。所以我不知道上面的view.py是不是应该显示的。

也许这个问题是Gmail本身的问题。请让我知道如何解决这个问题。谢谢。

编辑

我简单地设置了 DEBUG = True 并看到错误信息从 error.log.

[Tue Feb 18 12:10:04.400741 2020] [core:notice] [pid 11826:tid 139852027644992] AH00094: Command line: '/usr/sbin/apache2'
[Tue Feb 18 12:15:05.589207 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984] Internal Server Error: /password-reset/
[Tue Feb 18 12:15:05.589265 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984] Traceback (most recent call last):
[Tue Feb 18 12:15:05.589270 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
[Tue Feb 18 12:15:05.589274 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     response = get_response(request)
[Tue Feb 18 12:15:05.589278 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
[Tue Feb 18 12:15:05.589282 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     response = self.process_exception_by_middleware(e, request)
[Tue Feb 18 12:15:05.589286 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
[Tue Feb 18 12:15:05.589290 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     response = wrapped_callback(request, *callback_args, **callback_kwargs)
[Tue Feb 18 12:15:05.589294 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
[Tue Feb 18 12:15:05.589298 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     return self.dispatch(request, *args, **kwargs)
[Tue Feb 18 12:15:05.589301 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
[Tue Feb 18 12:15:05.589305 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     return bound_method(*args, **kwargs)
[Tue Feb 18 12:15:05.589309 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
[Tue Feb 18 12:15:05.589313 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     response = view_func(request, *args, **kwargs)
[Tue Feb 18 12:15:05.589316 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/contrib/auth/views.py", line 222, in dispatch
[Tue Feb 18 12:15:05.589320 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     return super().dispatch(*args, **kwargs)
[Tue Feb 18 12:15:05.589324 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
[Tue Feb 18 12:15:05.589327 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     return handler(request, *args, **kwargs)
[Tue Feb 18 12:15:05.589331 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/views/generic/edit.py", line 142, in post
[Tue Feb 18 12:15:05.589335 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     return self.form_valid(form)
[Tue Feb 18 12:15:05.590890 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/contrib/auth/views.py", line 235, in form_valid
[Tue Feb 18 12:15:05.590901 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     form.save(**opts)
[Tue Feb 18 12:15:05.590905 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/contrib/auth/forms.py", line 324, in save
[Tue Feb 18 12:15:05.590909 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     user_email, html_email_template_name=html_email_template_name,
[Tue Feb 18 12:15:05.590913 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/contrib/auth/forms.py", line 272, in send_mail
[Tue Feb 18 12:15:05.590916 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     email_message.send()
[Tue Feb 18 12:15:05.590920 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/core/mail/message.py", line 276, in send
[Tue Feb 18 12:15:05.590923 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     return self.get_connection(fail_silently).send_messages([self])
[Tue Feb 18 12:15:05.590927 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
[Tue Feb 18 12:15:05.590930 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     new_conn_created = self.open()
[Tue Feb 18 12:15:05.590934 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/home/djtu/django_project/venv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 62, in open
[Tue Feb 18 12:15:05.590942 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     self.connection = self.connection_class(self.host, self.port, **connection_params)
[Tue Feb 18 12:15:05.590946 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/usr/lib/python3.7/smtplib.py", line 251, in __init__
[Tue Feb 18 12:15:05.590949 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     (code, msg) = self.connect(host, port)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
[Tue Feb 18 12:15:05.590953 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/usr/lib/python3.7/smtplib.py", line 336, in connect
[Tue Feb 18 12:15:05.590956 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     self.sock = self._get_socket(host, port, self.timeout)
[Tue Feb 18 12:15:05.590960 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/usr/lib/python3.7/smtplib.py", line 307, in _get_socket
[Tue Feb 18 12:15:05.590963 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     self.source_address)
[Tue Feb 18 12:15:05.590966 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/usr/lib/python3.7/socket.py", line 727, in create_connection
[Tue Feb 18 12:15:05.590970 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     raise err
[Tue Feb 18 12:15:05.590973 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]   File "/usr/lib/python3.7/socket.py", line 716, in create_connection
[Tue Feb 18 12:15:05.590976 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984]     sock.connect(sa)
[Tue Feb 18 12:15:05.590980 2020] [wsgi:error] [pid 11827:tid 139851910838016] [remote 121.131.97.11:49984] TimeoutError: [Errno 110] Connection timed out

我希望这篇日志能给大家一些说明,到底出了什么问题。有人能给我指出正确的方向吗?

EDITED #2

谢谢Sachin。我按照建议进行了尝试,但我的服务器机器无法连接。相反,它显示这个消息。

(venv) myusername@hostname:~$ telnet smtp.gmail.com 587
Trying 2404:6800:4008:c00::6d...
Trying 108.177.97.109...
telnet: Unable to connect to remote host: Connection timed out

不幸的是,问题仍然存在。

django python-3.x smtp apache2
1个回答
0
投票

经过一段时间,我意识到哪里出了问题。我使用 两步验证 我的Gmail账户,这意味着我的密码不能在外部程序中使用,比如django,除非我把它替换为 应用程序密码.

更多信息,请访问官方网页 应用程序密码:https:/support.google.comaccountsanswer185833?hl=en

而如果你使用的是linode的虚拟主机公司,他们可能会因为某些原因而屏蔽电子邮件端口,所以也可能是外部问题。

© www.soinside.com 2019 - 2024. All rights reserved.