我正在开发一个Django应用程序,其中有向用户发送电子邮件的功能。在测试用例中,我有一个联系表单,通过gmail的smtp将表单数据作为邮件的一部分提交。由于我遵循的视频教程建议禁用验证码,但当我点击提交按钮时,出现了[Errno111]的错误。在查看此问题和另一个SO帖子后,我了解到我需要在真实服务器上运行站点(而不仅仅是本地主机)才能够发送电子邮件。然而,当我在服务器上尝试时,仍然出现相同的错误。我非常确定我已经按照正确的步骤操作,那么问题可能是什么呢?
以下是错误追踪信息:
以下是错误追踪信息:
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)