Django- [Errno 111]连接在使用smtp时拒绝 [英] Django- [Errno 111] Connection refused when using smtp
问题描述
我正在使用一个具有向用户发送电子邮件功能的Django应用程序。在一个测试用例中,我有一个联系表单,提交表单数据作为电子邮件的一部分使用smtp通过gmail。我在此视频播放列表中建议我禁用了验证码,我已经关注了教程,但是当我点击提交按钮,我得到[Errno 111]由于某些原因。看到此和另一个SO文章,我了解到,我需要在实时服务器(而不是本地主机)上运行该站点,以便能够发送电子邮件。但是,当我在服务器上尝试这样做时,我遇到了与之前相同的错误。我很确定我遵循所有正确的步骤,那可能是什么问题?
这是错误追溯:
Django版本:1.8.6
Python版本:2.7.6
安装的应用程序:
('django.contrib.admin' ,
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'artist_tracker')
安装的中间件:
('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.secu rity.SecurityMiddleware')
追溯:
文件/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py在get_response
132. response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件/var/www/students/ceyron/bandsync/src/bandsync-repo/artist_tracker/views.py联系
58. send_mail(subject,contact_message,from_email,to_email,fail_silently = False)
文件/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__ .py在send_mail
62. return mail.send()
文件/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py在发送
303. return self.get_connection(fail_silently).send_messages([self])
文件/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp .pyin send_messages
107. sent = self._send(message)
文件/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp。 py在_send
123. self.connection.sendmail(from_email,recipient,message.as_bytes(linesep ='\r\\\
'))
文件/usr/lib/python2.7/smtplib.py sendmail
731. raise SMTPSenderRefused(code,resp,from_addr)
异常类型:SMTPSenderRefused at / contact /
异常值:(530,5' 。了解更多信息,请参阅\"\5.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
#在这里创建您的观点。
from django.core.mail import send_mail
def contact(request):
form = ContactForm(request.POST or None)
如果表单.is_valid():
#用于键,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 =站点联系表
from_email = settings.EMAIL_HOST_USER
to_email = [from_email,'celijahh@gmail.com']
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)
在您的代码中
to_email = [from_email,'someone@gmail.com']
是一个列表,再次传递给列表中的内容参数。
更改此;
send_mail(subject,contact_message,from_email,[to_email],fail_silently = True)
/ p>
send_mail(subject,contact_message,from_email,to_email,fail_silally = True)
pre>
I'm working on a Django app that has a feature for sending emails to users. In a test case, I have a contact form that submits form data as a part of an email using smtp through gmail. I disabled captchas as I was advised in this video playlist that I've been following for tutorials but when I click the submit button I get the [Errno 111] for some reason. After seeing this and another SO post, I learned that I needed to be running the site on a live server (instead of just localhost) in order to be able to send an email. However, when I tried this on a server, I got the same error as before. I'm pretty sure I followed all of the correct steps, so what could be the problem?
Here is the error traceback:
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')
contact form from views.py file
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, 'celijahh@gmail.com'] 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)
解决方案In your code;
to_email = [from_email, 'someone@gmail.com']
is a list and again you passing to_email argument inside list.
Change this;
send_mail(subject, contact_message, from_email, [to_email], fail_silently = True )
to
send_mail(subject, contact_message, from_email, to_email, fail_silently = True )
这篇关于Django- [Errno 111]连接在使用smtp时拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!