Django- [Errno 111] 使用 smtp 时连接被拒绝

Posted

技术标签:

【中文标题】Django- [Errno 111] 使用 smtp 时连接被拒绝【英文标题】:Django- [Errno 111] Connection refused when using smtp 【发布时间】:2016-03-01 06:06:31 【问题描述】:

我正在开发一个 Django 应用程序,该应用程序具有向用户发送电子邮件的功能。在一个测试用例中,我有一个联系表单,它使用 smtp 通过 gmail 将表单数据作为电子邮件的一部分提交。我禁用了验证码,正如我在this video playlist 中被告知的那样,我一直在关注教程,但是当我单击提交按钮时,出于某种原因,我得到了 [Errno 111]。在看到this 和another SO post 之后,我了解到我需要在实时服务器(而不仅仅是本地主机)上运行该站点才能发送电子邮件。但是,当我在服务器上尝试此操作时,我遇到了与以前相同的错误。我很确定我遵循了所有正确的步骤,那么可能是什么问题?

这是错误回溯:

Django Version: 1.8.6
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'artist_tracker')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/students/ceyron/bandsync/src/bandsync-repo/artist_tracker/views.py" in contact
  58.       send_mail(subject, contact_message, from_email, to_email, fail_silently =False)
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py" in send_mail
  62.     return mail.send()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py" in send
  303.         return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py" in send_messages
  107.                 sent = self._send(message)
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py" in _send
  123.             self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
File "/usr/lib/python2.7/smtplib.py" in sendmail
  731.             raise SMTPSenderRefused(code, resp, from_addr)

Exception Type: SMTPSenderRefused at /contact/
Exception Value: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1  https://support.google.com/mail/answer/14257 j34sm10137713qkh.39 - gsmtp', u'webmaster@localhost')

views.py 文件中的联系表

from django.shortcuts import render
from django.conf import settings
from .forms import ContactForm, SignUpForm
# Create your views here.

from django.core.mail import send_mail

def contact(request):
    form = ContactForm(request.POST or None)
    if form.is_valid():
        # for key, value in form.cleaned_data.iteritems():
        #   print key, value
        form_email = form.cleaned_data.get("email")
        form_message = form.cleaned_data.get("message")
        form_full_name = form.cleaned_data.get("full_name")
        # print form.cleaned_data
        subject = "Site contact form"
        from_email = settings.EMAIL_HOST_USER
        to_email = [from_email]
        contact_message = ''' 
        %s: %s via %s
        '''%(form_full_name, form_message, form_email)
        send_mail(subject, contact_message, from_email, [to_email], fail_silently = True )

    context = 
        "form": form
    
    return render(request, "forms.html", context)

【问题讨论】:

toemail = ', '.join(to_email) 并将字符串传递给 send_mail 【参考方案1】:

在设置文件中更改此项

EMAIL_HOSTS = 'smtp.gmail.com'
EMAIL_HOSTS_USER = "bandsyncdjango@gmail.com"

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "bandsyncdjango@gmail.com"

【讨论】:

谢谢,这似乎解决了一个错误,但现在我遇到了一个类型错误。我用回溯日志编辑了帖子。你知道发生了什么吗? 你能发表你对 /contact/ 的看法吗 我已经添加了答案。试试看。【参考方案2】:

在您的代码中;

to_email = [from_email, 'someone@gmail.com'] 

是一个列表,您再次在列表中传递 to_email 参数。

改变这个;

send_mail(subject, contact_message, from_email, [to_email], fail_silently = True )

send_mail(subject, contact_message, from_email, to_email, fail_silently = True )

【讨论】:

感谢您的新答案。不过,现在似乎存在某种身份验证错误。我刚刚用新的回溯日志更新了帖子。我将 fail_silently 设置为 False,所以我不确定这是否会导致问题。 这似乎是一个单独的问题,所以我再问一个问题,谢谢。

以上是关于Django- [Errno 111] 使用 smtp 时连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章

在 Heroku 上挂载 Celery 时出现“[Errno 111] Connection denied”错误

无法连接到 amqp://guest:**@127.0.0.1:5672//: [Errno 111] 连接被拒绝

celery + redis 无法连接到 amqp://guest:**@127.0.0.1:5672//: [Errno 111] 连接被拒绝

将 Celery 与 SQS 一起使用时 Errno 111 连接被拒绝

GraphQL:[Errno 111] 连接被拒绝

Python socket.error:[Errno 111] 连接被拒绝