-
Notifications
You must be signed in to change notification settings - Fork 334
Emails with attachments cause server error in Django 5.2 #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Just found the same issue in Django 5.2, python 3.12. Here is my code: from typing import List
import traceback
from django.core.validators import validate_email
from django.core.mail import EmailMultiAlternatives
from django.contrib import messages
from django.utils.translation import gettext as _
from django.template.loader import get_template
def send_email(subject, plain_template, html_template, context, to: List[str], attachments=None, request=None):
try:
plain_text = get_template(plain_template)
html = get_template(html_template)
text_content = plain_text.render(context)
html_content = html.render(context)
msg = EmailMultiAlternatives(subject, text_content, to=to, attachments=attachments)
msg.mixed_subtype = 'related'
msg.attach_alternative(html_content, "text/html")
msg.send(fail_silently=True)
except Exception as e:
if request:
messages.error(request, _('Ocurrió un error al enviar el correo electrónico.'))
print(e)
print(traceback.format_exc()) django-1 | Traceback (most recent call last):
django-1 | File "/app/core/services.py", line 82, in send_email
django-1 | msg.send(fail_silently=True)
django-1 | File "/usr/local/lib/python3.12/site-packages/django/core/mail/message.py", line 307, in send
django-1 | return self.get_connection(fail_silently).send_messages([self])
django-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django-1 | File "/usr/local/lib/python3.12/site-packages/mailer/backend.py", line 12, in send_messages
django-1 | messages = Message.objects.bulk_create([Message(email=email) for email in email_messages], MESSAGES_BATCH_SIZE)
django-1 | ^^^^^^^^^^^^^^^^^^^^
django-1 | File "/usr/local/lib/python3.12/site-packages/django/db/models/base.py", line 558, in __init__
django-1 | _setattr(self, prop, value)
django-1 | File "/usr/local/lib/python3.12/site-packages/mailer/models.py", line 148, in _set_email
django-1 | self.message_data = email_to_db(val)
django-1 | ^^^^^^^^^^^^^^^^
django-1 | File "/usr/local/lib/python3.12/site-packages/mailer/models.py", line 94, in email_to_db
django-1 | return base64_encode(pickle.dumps(email)).decode("ascii")
django-1 | ^^^^^^^^^^^^^^^^^^^
django-1 | _pickle.PicklingError: Can't pickle <class 'django.core.mail.message.Alternative'>: attribute lookup Alternative on django.core.mail.message failed |
Discussion in the Django forum: https://forum.djangoproject.com/t/django-5-2s-usage-of-named-tuples-in-emails-is-a-breaking-change/40586. |
Django 5.2.1 will fix this regression and make emails pickleable again. |
I think I got the same issue when sending html and text emails at the same time |
Since Django 5.2.1 has been released I think we can safely close this issue. |
Uh oh!
There was an error while loading. Please reload this page.
I am sending a
EmailMultiAlternatives
with an attachment, and this causes a server error in Django 5.2.Code:
Error:
The text was updated successfully, but these errors were encountered: