使用联系表单,我无法收到Django发送到收件箱的电子邮件。
这是我的功能:
def contact(request):
if request.method == 'GET':
form = ContactForm()
else:
form = ContactForm(request.POST)
if form.is_valid():
contact_name = form.cleaned_data['contact_name']
contact_phone = form.cleaned_data['contact_phone']
contact_period = form.cleaned_data['contact_period']
subject = contact_name + " | " + contact_phone + " | " + contact_period
content = form.cleaned_data['content']
contact_email = form.cleaned_data['contact_email']
try:
send_mail(subject,content,contact_email, ['office@cohen.ro'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('success')
return render(request, "contact.html", {'form': form})
def success(request):
return HttpResponse('Success! Thank you for your message.')
这些是我服务器的设置:
Secure SSL/TLS Settings (Recommended)
Username: office@cohen.ro
Password: Use the email account’s password.
Incoming Server: mail.cohen.ro
IMAP Port: 993 POP3 Port: 995
Outgoing Server: mail.cohen.ro
SMTP Port: 465
这就是我在Django设置中的含义: LATEREDIT:我已经评论了控制台,现在有这行代码,但没有提交电子邮件。
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
#EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
DEFAULT_FROM_EMAIL = 'office@cohen.ro'
EMAIL_HOST= 'mail.cohen.ro'
EMAIL_HOST_USER= 'office@cohen.ro'
EMAIl_HOST_PASSWORD='mypass'
EMAIL_PORT = 465
EMAIL_USE_TLS = True
在我的服务器中,我可以看到该消息,但不会将电子邮件发送到我的电子邮件地址,所以我不知道是否有人在问我什么。谢谢!
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: me | 0787877 | 12-13
From: test@test.com
To: office@cohen.ro
Date: Mon, 25 Dec 2017 11:29:39 -0000
Message-ID: <151420137937.1642.9393637578495359678@My-MacBook-Air.local>
me
我得到的错误:
SMTPServerDisconnected at /contact/
Connection unexpectedly closed
Request Method: POST
Request URL: http://127.0.0.1:8000/contact/
Django Version: 2.0
Exception Type: SMTPServerDisconnected
Exception Value:
Connection unexpectedly closed
Exception Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtplib.py in getreply, line 393
Python Executable: /Users/cohen/PycharmProjects/chn/venv/bin/python
Python Version: 3.6.1
Python Path:
['/Users/cohen/PycharmProjects/chn',
'/Users/cohen/PycharmProjects/chn',
'/Users/cohen/PycharmProjects/static_in_env',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
'/Users/cohen/PycharmProjects/chn/venv/lib/python3.6/site-packages',
'/Users/cohen/PycharmProjects/chn/venv/lib/python3.6/site-packages/setuptools-28.8.0-py3.6.egg',
'/Users/cohen/PycharmProjects/chn/venv/lib/python3.6/site-packages/pip-9.0.1-py3.6.egg',
'/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend']
Server time: Mon, 25 Dec 2017 13:49:16 +0200
任何帮助,将不胜感激!
答案 0 :(得分:1)
所以,问题是因为TLS是真的并且需要为假 - EMAIL_USE_TLS = False
现在正在提交电子邮件。