Отправка электронной почты по EmailMultiAlternatives sudenly перестала работать
Я столкнулся со странной ошибкой. Отправка писем перестала работать со вчерашнего дня, до этого все было нормально. Я вообще ничего не менял.
Сообщение об ошибке:
2024-07-11 09:35:01,147 share.email.service ERROR Traceback (most recent call last):
File "/srv/deduu/./share/email/service.py", line 318, in send_email_from_template
cls.send_admin_email(subject, recipient_list, html_message=message)
File "/srv/deduu/./share/email/service.py", line 330, in send_admin_email
cls.send_email(subject, from_mail, recipient_list, message, html_message)
File "/srv/deduu/./share/email/service.py", line 279, in send_email
return email_wrapper.send_mail()
File "/srv/deduu/./share/email/service.py", line 91, in send_mail
return_value = bool(self.mail.send())
File "/srv/deduu/.venv/lib/python3.8/site-packages/django/core/mail/message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "/srv/deduu/.venv/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 109, in send_messages
sent = self._send(message)
File "/srv/deduu/.venv/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 125, in _send
self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
File "/usr/lib/python3.8/smtplib.py", line 874, in sendmail
(code, resp) = self.mail(from_addr, esmtp_opts)
File "/usr/lib/python3.8/smtplib.py", line 539, in mail
return self.getreply()
File "/usr/lib/python3.8/smtplib.py", line 398, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
Странно, но отправка почты работает с тестовым скриптом с теми же учетными данными и настройками, но с использованием этого фрагмента
from email.message import EmailMessage
import smtplib
username = "info@test.de"
password = r"*****"
smtp_server = "mail.test.de"
smtp_port = 47999
receiver_email = "user@test.de"
subject = 'Test Email'
body = 'This is a test email sent using smtplib in Python.'
# Create message
em = EmailMessage()
em['From'] = username
em['To'] = receiver_email
em['Subject'] = subject
em.set_content(body)
try:
# Connect to the SMTP server
with smtplib.SMTP(smtp_server, smtp_port) as smtp:
smtp.starttls()
smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(username, receiver_email, em.as_string())
smtp.quit()
except Exception as e:
print(f"Unexpected error: {e}")
Электронная почта используется следующим образом (выдержка):
email_connection = get_connection()
email_connection.host = "mail.test.de"
email_connection.port = 47999
email_connection.username = info@test.de
email_connection.password = r"*****"
email_connection.use_tls = True
mail = EmailMultiAlternatives(subject, message, from_email, recipient_list)
mail.connection = email_connection
if html_message:
mail.attach_alternative(html_message, "text/html")
mail.send()
У кого-нибудь есть идея?