问题
i'm trying to get a contact form on a personal site to work. i'm using the smtp outlook. Right now it gives me the following error:
Traceback:
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'],['nunomaster_@hotmail.com'],)
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
my settings:
EMAIL_HOST = 'smtp-mail.outlook.com'
EMAIL_HOST_USER = 'myemail@hotmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 25
EMAIL_USE_TLS = True
view file:
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,['myemail@hotmail.com'], fail_silently=False)
return HttpResponseRedirect('/thankyou/')
return render(request, "mypage.html", {'errors': errors})
Does anyone has a clue on what is wrong? If necessary i can give more info on the app
[EDIT] I'm hosting the page at pythonanywhere.com, does the error have something to do with the server?
[EDIT] SOLVED Pythonanywhere for free users only works for Gmail, and i had to give it permission for less secure apps! Now it works fine.
回答1:
Try using use_ssl=True
in your SMTP
settings and changing the port to 465
.
You can directly refer to Django SMTP options for list of all the settings you can tweak. Also please mention which Django version you are using for building your app?
来源:https://stackoverflow.com/questions/33543475/django-email-sending-error