I have been trying to get emails working with my Django application and have not been able to. Ive been reading around on similar questions and still haven't been able to pin point my error.
My settings.py looks like :
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'email@domain'
EMAIL_HOST_PASSWORD = 'pass'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
MY code to send the email looks like :
def application(request):
if request.method == 'GET':
form = ApplyForm()
else:
form = ApplyForm(request.POST)
if (form.is_valid()):
try:
subject = 'Overland Application'
from_email = form.cleaned_data['useremail']
phone = form.cleaned_data['phone']
names = form.cleaned_data['names']
year = form.cleaned_data['year']
make = form.cleaned_data['make']
model = form.cleaned_data['model']
message = str(names) + '\n' + str(from_email) + '\n' + str(phone) + '\n' + str(year) + '\n' + str(make) + '\n' + str(model)
try:
send_mail(subject, message, settings.EMAIL_HOST_USER, ['email@domain.com'], fail_silently=False)
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('thanks')
except:
pass
return render(request, "overland/apply.html", {'form': form})
Some additional information is that it seems to be accessing my email account as I did receive an email from google saying there was suspicious access on my account from the location of the server.
I also pinged the smtp server from the live server to make sure that it was communicating.
I am not sure if it is a small syntax error on my part somewhere or I am using the django mail function incorrectly because locally it seemed to work and would redirect to my thanks page, but when I do this live it seems to just reload the page and not send anything.
Thanks in advance for any information.