Django - Mail is not sent. Port changes to 25 but in seetings.py is set to 587

101 Views Asked by At

Email is not sent. What is interesting... before (like 3 mos ago) entire code worked perfectly fine.

Settings:

DEBUG = True

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT: 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'xyz'
EMAIL_USE_TLS = True

ALLOWED_HOSTS = []

view.py:

def index(request):
    """The home page."""

    # Send a message.
    if request.method != 'POST':
        # No data submitted; create a blank form.
        form_email = EmailForm()
        form_message = EmailMessageForm()
    else:
        # POST data submitted; proecess data.
        form_email = EmailForm(data=request.POST)
        form_message = EmailMessageForm(data=request.POST)
        if form_email.is_valid() and form_message.is_valid():
            try:
                email = Email.objects.get(text=request.POST['text'])
            except:
                form_email.save()
                email = Email.objects.last()
            
            message_db = form_message.save(commit=False)
            message_db.email = email
            message_db.save()

            message_owner = (
                f'New email on your website from {request.POST["text"]}',
                f"Email has been sent from: {request.POST['text']}\n\n"
                f"Full message:\n\"{request.POST['message']}\"",
                'settings.EMAIL_HOST_USER',
                ['[email protected]',],
            )

            message_guest = ('Your email has been sent',
                "Many thanks for getting in contact with me. Your message was "
                "successfully sent. I will reach out to you as soon as possible."
                f"\n\nYour message:\n\"{request.POST['message']}\"",
                'settings.EMAIL_HOST_USER',
                [request.POST['text'],],
            )

            send_mass_mail((message_owner, message_guest), fail_silently=False)

            return redirect('home:contact_thank_you')

    # Display a blank or invalid form.
    context = {'form_email': form_email, 'form_message': form_message}
    return render(request, 'home/index.html', context)

Traceback: Request Method: POST

Request URL: http://127.0.0.1:8000/

Django Version: 4.1.5

Exception Type: TimeoutError

Exception Value: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connectio n failed because connected host has failed to respond

Raised during: home.views.index

enter image description here

I found the reason but I can't find any solution to fixe it! This is what I've got when email has been sent:

enter image description here

The port is set to 587 in seetings.py (as shown above), but somehow this value is 25 instead of 587 after sending the email.

In the docs you can see that 25 is the default value, but why is it not overridden by my settings?

2

There are 2 best solutions below

4
Tim Roberts On

Pretty sure you don't want quotes around the email addresses. Also note that, unlike with a tuple, you do not need a trailing comma to create a one-item list:


            message_owner = (
                f'New email on your website from {request.POST["text"]}',
                f"Email has been sent from: {request.POST['text']}\n\n"
                f"Full message:\n\"{request.POST['message']}\"",
                settings.EMAIL_HOST_USER,
                ['[email protected]'],
            )

            message_guest = ('Your email has been sent',
                "Many thanks for getting in contact with me. Your message was "
                "successfully sent. I will reach out to you as soon as possible."
                f"\n\nYour message:\n\"{request.POST['message']}\"",
                settings.EMAIL_HOST_USER,
                [request.POST['text']],
            )
0
Maxwell On

I found not the best solution but it works...

If you have the same issue, go to:

\AppData\Local\Programs\Python\Python311\Lib\smtplib.py

then open this file and replace port to 587: enter image description here enter image description here

But it is not the best practice I hope somebody find better solution how to fix it.