Forums

ConnectionRefusedError: [Errno 111] Connection refused while trying to send an e-mail in django

Hello, I have a paid account, and I have been trying to send a simple test mail. I'm using a paid account in hostinger for the mail

In my settings.py I have (inside my app folder) (edit.- note that I have a typo in EMAIL_BAKEND, it should be EMAIL_BACKEND)

# Email settings, from pylessons/django-email-confirm and some AI help from hostinger
EMAIL_BAKEND = "django.core.mail.backeneds.smtp.EmailBackend"
EMAIL_HOST = "smtp.hostinger.com"
EMAIL_FROM = "my_mail"
EMAIL_HOST_USER = "my_mail"
EMAIL_HOST_PASSWORD = "my_secret_pass"
EMAIL_PORT = 587 # according to hostinger mail (465) (587 in other examples)
EMAIL_USE_TLS = True

I'm calling these setting in my views with:

from . import settings

And I'm also using:

from django.core.mail import send_mail

This is my view that is taking care of rendering my subscribe page, that is right now really simple, I do have a form for this, and also a model for the db, that I'm currently not using (my program gets always to the object does not exist exception), and before sending the mail it is throwing me the ConnectionRefusedError.

def subscribe(request):
print(f"checking if this works {settings.EMAIL_HOST_USER}")
if request.method == "POST":
    subs_form = SubsForm(request.POST)
    if subs_form.is_valid():
        nombre_subs = request.POST["nombre"]
        apellidos_subs = request.POST["apellidos"]
        correo_subs = request.POST["correo"]
        try:
            subs_guy = Correo.objects.get(nombre = nombre_subs, apellidos = apellidos_subs, correo = correo_subs)
            if subs_guy.conf_mail_sent == True and subs_guy.confirmado == False:
                messages.warning(request, f"Ya se envió un correo de confirmación a {subs_guy.correo}, pero no se ha activado, revisar en inbox o spam")
            elif subs_guy.conf_mail_sent == True and subs_guy.confirmado == True:
                messages.warning(request, f"{subs_guy.nombre} {subs_guy.apellidos} está registrado con el correo {subs_guy.correo}")
        except ObjectDoesNotExist:
            messages.warning(request, f"Correo inexistente, se acaba de mandar un correo de confirmación a {correo_subs}, por favor, revise su correo.")
            send_mail(
                "subject_test",
                "mssg_test",
                settings.EMAIL_HOST_USER,
                [correo_subs],
                fail_silently = False
            )
    else:
        messages.warning(request, "Hay un problema con los datos")
        subs_form = SubsForm()
    return render(request, "vibramur/subscribe.html", {
        "form": subs_form
    })
else:
    subs_form = SubsForm()
return render(request, "vibramur/subscribe.html", {
    "form": subs_form
})

This is my first time trying to send an e-mail though Django, I don't know if I'm messing up somewhere, any help would be appreciated

If you're getting that error in a paid account, and it was upgraded a while back so there's no chance that anything is still running in "free mode" (which is the case for you) then my best guess would be that the remote server is rejecting the incoming connection; perhaps you could get in touch with Hostinger to ask if they block incoming connections from AWS netblocks?

Thank you @giles, I'll try to get in contact with them, and will update this post (I'm assuming, I'm not doing something wrong in my code) will be back

Thanks!

I was "speaking" to hostinger yesterday, and after a while of not reaching a solution, I decided to make a test with a gmail account. Today I did this (doing the two step verification), mainly following these instructions) and changing my settings to:

(edit.- same typo as bf: EMAIL_BACKEND should be EMAIL_BACKEND)

EMAIL_BAKEND = "django.core.mail.backeneds.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"#"smtp.hostinger.com"
EMAIL_FROM = "myemail@gmail.com"
EMAIL_HOST_USER = "myemail@gmail.com"
EMAIL_HOST_PASSWORD = "mypassgenerated in google app codes"
EMAIL_PORT = 587 # according to hostinger mail (465) (587 in other examples)
EMAIL_USE_TLS = True

I git pulled, I reload my web, I tried it again on a browser, and I got the server error (500) If I check on the error log it tells me the same thing about the ConnectionRefusedError... so I guess, I'm doing something wrong.

Ok, I solved it.

Problem appears to be me doing the settings.py in my app, and it should be in the main project, just below the settings for django

(Same typo as bf, EMAIL_BACKEND should be EMAIL_BACKEND)

# Email settings, from pylessons/django-email-confirm and some AI help from hostinger, and a video from codemy https://www.youtube.com/watch?v=xNqnHmXIuzU
EMAIL_BAKEND = "django.core.mail.backeneds.smtp.EmailBackend"
EMAIL_HOST = "smtp.hostinger.com"     
EMAIL_FROM = "hostinger@mail.com" 
EMAIL_HOST_USER = "hostinger@mail.com"
EMAIL_HOST_PASSWORD = "hostinger_mail_pass" 
EMAIL_PORT = 587 # (587 works, 465 just keeps on reloading the page)
EMAIL_USE_TLS = True

This also requires (in my views)

from django.conf import settings

This works with gmail, and hostinger (same port) Thanks for the help, I still need to make lots of new changes now :-)

I just noticed, you have EMAIL_BAKEND in both of your code samples -- should that not be EMAIL_BACKEND?

Assuming that was just a typo, perhaps you could put something in your code just before the call to send_mail to make sure that you have the right stuff in the settings at that point, and that something else isn't overriding what you provided above. Something like this:

print(f"EMAIL_BACKEND={settings.EMAIL_BACKEND}", flush=True)
print(f"EMAIL_HOST={settings. EMAIL_HOST}", flush=True)

...and so on for all of the other settings. This output would be written to the end of your website's server log (linked from the "Web" page) and you could double-check everything there.

Thank you very much @giles, that was a typo, but somehow I never got problems (I think) with it, I was sending some test e-mails with that error :-S but thanks for noticing. And thanks for the suggestion, I will take that into account.

Glad we could help!