0

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 = 'xyz@gmail.com'
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',
                ['my@gmail.com',],
            )

            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?

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
Maxwell
  • 138
  • 1
  • 9
  • Surely `'settings.EMAIL_HOST_USER'` should not be in quotes (twice). You want the value of the variable here, not just a string that contains the variable name. – Tim Roberts Jul 21 '23 at 23:20
  • _3 mos ago entire code worked perfectly fine_ Did you make any changes to the code since then? – John Gordon Jul 22 '23 at 00:18
  • nope, I didnt change anything. Even when I issue 'git checkout .' in order to revert all changes, the result is the same. I'm curious whats going on :D – Maxwell Jul 22 '23 at 09:16
  • 2
    `EMAIL_PORT: 587` must be `EMAIL_PORT = 587` – Ivan Starostin Jul 22 '23 at 18:34
  • https://stackoverflow.com/questions/48323493/what-is-this-odd-colon-behavior-doing – Ivan Starostin Jul 22 '23 at 18:37
  • @IvanStarostin omg I spent a lot of time to find the reason and I couldn't notice this... yeap if you replace equal mark to colon it works... – Maxwell Jul 22 '23 at 19:00

2 Answers2

2

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,
                ['my@gmail.com'],
            )

            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']],
            )
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • how can I get access to the settings variable in the view.py file? – Maxwell Jul 21 '23 at 23:51
  • Have you done `import settings`? – Tim Roberts Jul 22 '23 at 01:00
  • do you mean? `from django.conf import settings`. It looks that I dont have access to `EMAIL_HOST_USER`, I dont see this variable on the displayed list when `settings.` is provided (screenshot attached at the end of the main entry) – Maxwell Jul 22 '23 at 09:24
  • I found the reason and it is not related to this variable. Somehow port changes to 25... I modified main entry. – Maxwell Jul 22 '23 at 15:39
-1

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.

Maxwell
  • 138
  • 1
  • 9