Welcome to Qi-U Community for programmer and developer-Open, Learning and Share


0 votes
in Technique[技术] by (31.9m points)

python - django with postfix can't connect to localhost smtp-server

i have configured postfix as a null client on my machine and the configuration also seems to be working, as

$ echo "test" | mail -s "test" -r "[email protected]" [email protected]

successfully delivers the email. however if i try to use it with django with the following settings:

EMAIL_HOST = "localhost"
DEFAULT_FROM_EMAIL = "[email protected]"
SERVER_EMAIL = "[email protected]"

the process get's stuck without an error being raised:

from django.core.mail import send_mail
from django.conf import settings

send_mail(subject="test", message="test", from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=["[email protected]"])

i can't figure out why this function get's stuck, the /var/log/mail.log file is also empty.

Edit: Note, that [email protected] and [email protected] are both valid adresses but i replaced them in this question.

Edit: When setting EMAIL_TIMEOUT = 10 in settings.py, i get the following error:

Traceback (most recent call last):
  File "/usr/lib/python3.8/smtplib.py", line 391, in getreply
    line = self.file.readline(_MAXLINE + 1)
  File "/usr/lib/python3.8/socket.py", line 669, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "venvpath/lib/python3.8/site-packages/django/core/mail/__init__.py", line 61, in send_mail
    return mail.send()
  File "venvpath/lib/python3.8/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "venvpath/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "venvpath/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 62, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "/usr/lib/python3.8/smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.8/smtplib.py", line 341, in connect
    (code, msg) = self.getreply()
  File "/usr/lib/python3.8/smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed: "
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: timed out

Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (31.9m points)

Welcome to Qi-U Community for programmer and developer-Open, Learning and Share