pythondjangodjango-formsdjango-settingsdjango-email

Django email backend and smtp configuration


I'm trying to use my Zoho account within my django project, in order to receive emails via contact forms.

I also followed this guide: https://www.zoho.com/mail/help/zoho-smtp.html

In the 'settings.py' file I wrote:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtppro.zoho.eu'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '<domain name email>'
EMAIL_HOST_PASSWORD = '<password>'

and in views.py:

def home(request):
    allTemplates = AllTemplates.objects.all()
    if request.method == 'POST':
        form = forms.ContactForm(request.POST)
        if form.is_valid():
            body = {
                'name': form.cleaned_data['name'],
                'surname': form.cleaned_data['surname'],
                'from_email': form.cleaned_data['from_email'],
                'message': form.cleaned_data['message'],
            }
            mail_body = "\n".join(body.values())
        try:
            send_mail("Generic contact", mail_body, '<domain name email>',
                      ['<domain name email>'], fail_silently=False)
        except BadHeaderError:
            return HttpResponse('Ops, qualcosa è andato storto')

    form = forms.ContactForm
    context = {'form': form, 'allTemplates': allTemplates,
               'allTemplates_length': len(allTemplates)}
    return render(request, 'home.html', context)

N.B. in 'send_email' I entered my email address twice to test

I also tried to use ssl

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtppro.zoho.eu'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_HOST_USER = '<domain name email>'
EMAIL_HOST_PASSWORD = '<password>'

but nothing, I don't receive any email.

Is there anyone who has already gone through it or who can direct me towards some document or guide to study?


Solution

  • I use

    EMAIL_PORT = 587
    EMAIL_USE_TLS = True
    
    1. my biggest pain was that all emails went to spam folder and I did not realize that for 2 hours.

    2. test with local output to terminal:

    settings.py

    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
    
    1. test with local mail server to make sure the email is correctly created:

    settings.py

    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST='localhost' 
    EMAIL_PORT=1025 
    

    and start local test mail server parallel to runserver in terminal window:

    python -m smtpd -n -c DebuggingServer localhost:1025