Django how to send_mail via local sendmail

I would like to send out email via local sendmail. my email settings in settings.py is below

EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = 'default_from_email@email.com'

the error is [Errno 61] Connection refused

What are you using as your local MTA? Is it listening on port 25?

thanks Ken. I use default FreeBSD sendmail(/usr/sbin/sendmail).

It is listening on port 25. “220 domain_name ESMTP Sendmail 8.15.2/8.15.2; Tue, 22 Dec 2020 10:51:52”

So I’m guessing you got that last line by doing something like telnet localhost 25?

I’m afraid I’m not going to be much help here - I switched from sendmail to postfix about 10 years ago. But I’m willing to try and walk through this with you.

Thank you for the reply Ken.
Yes the telnet localhost 25’s result is below.

“220 domain_name ESMTP Sendmail 8.15.2/8.15.2; Tue, 22 Dec 2020 10:51:52”

Unfortunately, I can’t recreate your symptoms.

I installed sendmail in a server. I added the following lines to the settings.py file of a test project:

EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = 'default_from_email@email.com'

I then ran the following lines from a shell_plus session:

from django.core.mail import send_mail
send_mail('Subject', 'This is a message', 'ken@localhost', ['root@localhost'],fail_silently=False,)

And it all worked. No errors were thrown by the code and the emails were put in the /var/spool/mqueue directory.

All this was done on an Ubuntu 20.04 system, which I know has some (significant) differences regarding network controls and many other security/stability items that are in BSD and not in Linux.

I guess all I can say here then is that I think the issues are environmental and not necessarily Django related.

Does FreeBSD support the sendmail command-line client where you can try to do the following?

$ sendmail -v someone@email.com
From: you@yourdomain.com
Subject: This is the subject field of the email
This is the message to be sent.
.

Note: For all lines after the sendmail command, there will be no prompt. The line consisting of only a period - . followed by a new line marks the end of the entered data, and then you’ll see sendmail sending the email.

Thanks again Ken.FreeBSD does support command-line and It works perfectly fine. I don’t understand why when I try from Django with same setting as you, I get an connection refuse error…

Sorry, I don’t have any help to offer here. I haven’t used FreeBSD in 15 - 20 years, and don’t have a system available to do any experimentation with.
Not that I would ever recommend doing this for real, but I might try running the code as root to see if it’s some type of permissions issue involved.
It wouldn’t be an answer, but might give you a clue as to where to continue looking.