我試圖讓個人網站上的聯繫表單工作。 我使用smtp展望。 現在它給了我下面的錯誤:Django電子郵件發送錯誤
回溯:
Environment:
Request Method: POST
Request URL: *******
Django Version: 1.8.1
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles')
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 "/home/Nmaster88/Ngranja/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/Nmaster88/Ngranja/Ngranja/Ngranja/views.py" in sendmail
23. send_mail(request.POST['contactSubject'],"From: "+request.POST['contactEmail']+'\n\n'+request.POST['contactMessage'],request.POST['contactEmail'],['[email protected]'],)
File "/home/Nmaster88/Ngranja/lib/python3.4/site-packages/django/core/mail/__init__.py" in send_mail
62. return mail.send()
File "/home/Nmaster88/Ngranja/lib/python3.4/site-packages/django/core/mail/message.py" in send
303. return self.get_connection(fail_silently).send_messages([self])
File "/home/Nmaster88/Ngranja/lib/python3.4/site-packages/django/core/mail/backends/smtp.py" in send_messages
100. new_conn_created = self.open()
File "/home/Nmaster88/Ngranja/lib/python3.4/site-packages/django/core/mail/backends/smtp.py" in open
58. self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python3.4/smtplib.py" in __init__
242. (code, msg) = self.connect(host, port)
File "/usr/lib/python3.4/smtplib.py" in connect
321. self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.4/smtplib.py" in _get_socket
292. self.source_address)
File "/usr/lib/python3.4/socket.py" in create_connection
509. raise err
File "/usr/lib/python3.4/socket.py" in create_connection
500. sock.connect(sa)
Exception Type: ConnectionRefusedError at/
Exception Value: [Errno 111] Connection refused
我的設置:
EMAIL_HOST = 'smtp-mail.outlook.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 25
EMAIL_USE_TLS = True
視圖文件:
from django.http import HttpResponseRedirect
from django.core.mail import send_mail
from django.shortcuts import render
def sendmail(request):
errors=[]
if request.method == 'POST':
if not request.POST.get('contactName',''):
errors.append('Coloca um nome.')
if not request.POST.get('contactEmail',''):
errors.append('Coloca um email.')
if not request.POST.get('contactSubject',''):
errors.append('Coloca um assunto.')
if not request.POST.get('contactMessage',''):
errors.append('Coloca um assunto.')
if not errors:
subject=request.POST['contactSubject']
email=request.POST['contactEmail']
message=request.POST['contactMessage']
send_mail(subject,"From: "+email+'\n\n'+message,email,['[email protected]'], fail_silently=False)
return HttpResponseRedirect('/thankyou/')
return render(request, "mypage.html", {'errors': errors})
有誰有什麼線索是錯的? 如果有必要,我可以給應用程序更多的信息
[編輯] 我在pythonanywhere.com託管的頁面,錯誤是否與服務器有關?
[編輯]已解決 Pythonanywhere免費用戶只能用於Gmail,我必須授予它不太安全的應用程序的權限!現在它工作正常。
如果你打開一個REPL,導入smtplib,並嘗試連接到你的SMTP服務器,你能得到一個實際的連接?您使用本地交換服務器還是Office 365?您使用TLS還是SSL SNMP? –
您是否正在運行[它們的防火牆](https://help.pythonanywhere.com/pages/SMTPForFreeUsers/)? – dhke
在他們的論壇上,他們說,只有谷歌Gmail的作品。我現在使用gmail配置。我現在的錯誤是另一回事。我有一個SMTPAuthenticationError。 – Nmaster88