merge mail

This commit is contained in:
shamoon
2025-04-08 16:36:47 -07:00
parent 730636f38e
commit b7b343222c
3 changed files with 2 additions and 2 deletions

38
src/paperless/mail.py Normal file
View File

@@ -0,0 +1,38 @@
from email import message_from_bytes
from pathlib import Path
from django.conf import settings
from django.core.mail import EmailMessage
from filelock import FileLock
def send_email(
subject: str,
body: str,
to: list[str],
attachment: Path | None = None,
attachment_mime_type: str | None = None,
) -> int:
"""
Send an email with an optional attachment.
TODO: re-evaluate this pending https://code.djangoproject.com/ticket/35581 / https://github.com/django/django/pull/18966
"""
email = EmailMessage(
subject=subject,
body=body,
to=to,
)
if attachment:
# Something could be renaming the file concurrently so it can't be attached
with FileLock(settings.MEDIA_LOCK), attachment.open("rb") as f:
content = f.read()
if attachment_mime_type == "message/rfc822":
# See https://forum.djangoproject.com/t/using-emailmessage-with-an-attached-email-file-crashes-due-to-non-ascii/37981
content = message_from_bytes(f.read())
email.attach(
filename=attachment.name,
content=content,
mimetype=attachment_mime_type,
)
return email.send()

View File

@@ -100,7 +100,6 @@ from documents.filters import PaperlessTaskFilterSet
from documents.filters import ShareLinkFilterSet
from documents.filters import StoragePathFilterSet
from documents.filters import TagFilterSet
from documents.mail import send_email
from documents.schema import generate_object_with_permissions_schema
from documents.signals import document_updated
from documents.tasks import consume_file
@@ -138,6 +137,7 @@ from paperless.db import GnuPG
from paperless.filters import GroupFilterSet
from paperless.filters import UserFilterSet
from paperless.index import DelayedQuery
from paperless.mail import send_email
from paperless.matching import match_correspondents
from paperless.matching import match_document_types
from paperless.matching import match_storage_paths