Home > Enterprise >  Django form send emails in console but not to my mailbox
Django form send emails in console but not to my mailbox

Time:12-03

I'm getting some practice using Django and now I'm stuck trying to get a contact form to work.

What I'm trying to do is: once the "Submit" button is clicked, I should receive the form data in an email arriving at the email address linked to my website. Instead what happens is: once I clicked on the "Submit" button, the page loads for some time and at the end I get a SMTPServerDisconnected error.

Can you tell me if I made a mistake in writing some logic or if it is a problem that I have to solve with my hosting service?

This is forms.py:

from django import forms


class ContactForm(forms.Form):
    name = forms.CharField(label='Your name', max_length=200,
                           widget=forms.TextInput(attrs={'class': 'form-control', 'id': 'name'}))
    from_email = forms.EmailField(label='Your email', max_length=200, widget=forms.TextInput(
        attrs={'class': 'form-control', 'id': 'email'}))
    subject = forms.CharField(label='Enter a subject', max_length=200,
                              widget=forms.TextInput(attrs={'class': 'form-control', 'id': 'subject'}))
    message = forms.CharField(label='Write here your message', max_length=500,
                              widget=forms.TextInput(attrs={'class': 'form-control', 'id': 'message'}))

This is view.py (I replaced each address with dummy addresses):

def home(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            body = {
                'first_name': form.cleaned_data['name'],
                'email_from': form.cleaned_data['from_email'],
                'message': form.cleaned_data['message'],
            }
            message = "\n".join(body.values())
            try:
                send_mail(subject, message, '[email protected]',
                          ['[email protected]'], fail_silently=True)
            except BadHeaderError:
                return HttpResponse('Invalid header found')

    form = ContactForm()
    return render(request, 'index.html', {'form': form})

And this is settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'mail.mydomain.net'
EMAIL_PORT = SmtpPort
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'my email password'

I tried to see if in the VS Code terminal the form works and that's why I replaced

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

with

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

and it works.

So I tried to replace the EMAIL_PORT with the IMAP PORT and POP3 PORT, but nothing happened.

CodePudding user response:

One of the reason why that doesnt work is a lot of mail providers use two step authentication. for my case it was outlook that have the issue. i suggest creating a gmail account and add app password and use that email to send emails. here you can find how to use that.

  • Related