2026-08-26 14:33:30 +02:00
|
|
|
"""Service layer for the Mail plugin — pure re-export facade.
|
|
|
|
|
|
|
|
|
|
All implementation lives in focused sub-modules:
|
|
|
|
|
accounts.py — create/update mail account service
|
|
|
|
|
crypto.py — AES-256 password encryption (Fernet)
|
|
|
|
|
drafts_sync.py — draft save/update, auto-sync, sync queue processing
|
|
|
|
|
imap_ops.py — IMAP flag sync, delete, move, folder create/delete
|
|
|
|
|
imap_sync.py — folder sync, account sync, thread-id, quota parser
|
|
|
|
|
pgp.py — PGP key import, encrypt/decrypt messages
|
|
|
|
|
sanitize.py — nh3 HTML sanitization
|
|
|
|
|
serializers.py — response dict builders for all ORM models
|
|
|
|
|
smtp_send.py — send via SMTP, reply, forward
|
|
|
|
|
text_utils.py — extract email addresses, strip HTML
|
|
|
|
|
"""
|
2026-07-01 15:41:27 +02:00
|
|
|
|
2026-07-16 23:08:37 +02:00
|
|
|
import logging
|
2026-07-01 15:41:27 +02:00
|
|
|
|
2026-08-26 14:33:30 +02:00
|
|
|
from app.plugins.builtins.mail.accounts import (
|
|
|
|
|
create_mail_account,
|
|
|
|
|
update_mail_account,
|
|
|
|
|
)
|
|
|
|
|
from app.plugins.builtins.mail.attachments import ( # noqa: F401 — routes.py re-exports
|
|
|
|
|
MAX_ATTACHMENT_SIZE,
|
|
|
|
|
_attachment_storage_path,
|
|
|
|
|
_decode_mime_filename,
|
|
|
|
|
_sanitize_filename,
|
|
|
|
|
_save_attachment_to_storage,
|
|
|
|
|
)
|
|
|
|
|
from app.plugins.builtins.mail.crypto import (
|
2026-08-26 09:29:47 +02:00
|
|
|
decrypt_password,
|
|
|
|
|
encrypt_password,
|
|
|
|
|
generate_salt,
|
|
|
|
|
)
|
2026-08-26 14:33:30 +02:00
|
|
|
from app.plugins.builtins.mail.drafts_sync import (
|
|
|
|
|
auto_sync_all_accounts,
|
|
|
|
|
imap_create_folder, # noqa: F401 — routes.py re-export
|
|
|
|
|
imap_delete_folder, # noqa: F401 — routes.py re-export
|
|
|
|
|
process_sync_queue,
|
|
|
|
|
save_draft,
|
|
|
|
|
update_draft,
|
|
|
|
|
)
|
|
|
|
|
from app.plugins.builtins.mail.imap_ops import (
|
|
|
|
|
imap_delete_mail,
|
|
|
|
|
imap_move_mail,
|
|
|
|
|
imap_sync_mail_flags,
|
|
|
|
|
)
|
|
|
|
|
from app.plugins.builtins.mail.imap_sync import (
|
2026-08-26 10:58:35 +02:00
|
|
|
_compute_thread_id,
|
|
|
|
|
_get_german_folder_name,
|
|
|
|
|
_parse_imap_list_response,
|
|
|
|
|
_parse_imap_quota_response,
|
|
|
|
|
get_account_password,
|
|
|
|
|
imap_sync_account,
|
|
|
|
|
imap_sync_folder,
|
|
|
|
|
)
|
2026-07-01 15:41:27 +02:00
|
|
|
from app.plugins.builtins.mail.models import (
|
|
|
|
|
Mail,
|
|
|
|
|
MailAccount,
|
2026-08-26 14:33:30 +02:00
|
|
|
MailAttachment,
|
2026-07-01 15:41:27 +02:00
|
|
|
MailFolder,
|
2026-08-26 14:33:30 +02:00
|
|
|
MailLabel,
|
2026-07-01 15:41:27 +02:00
|
|
|
MailLabelAssignment,
|
|
|
|
|
MailRule,
|
2026-08-26 14:33:30 +02:00
|
|
|
MailSignature,
|
|
|
|
|
MailTemplate,
|
2026-07-01 15:41:27 +02:00
|
|
|
VacationSentLog,
|
|
|
|
|
)
|
2026-08-26 14:33:30 +02:00
|
|
|
from app.plugins.builtins.mail.pgp import (
|
2026-08-26 09:38:34 +02:00
|
|
|
import_pgp_private_key,
|
|
|
|
|
import_pgp_public_key,
|
|
|
|
|
pgp_decrypt_message,
|
|
|
|
|
pgp_encrypt_message,
|
|
|
|
|
)
|
2026-08-26 14:33:30 +02:00
|
|
|
from app.plugins.builtins.mail.rules_vacation import (
|
|
|
|
|
apply_rules_to_mail,
|
|
|
|
|
execute_rule_actions,
|
|
|
|
|
log_vacation_sent,
|
|
|
|
|
matches_condition,
|
|
|
|
|
should_send_vacation_reply,
|
|
|
|
|
substitute_template_vars,
|
|
|
|
|
)
|
|
|
|
|
from app.plugins.builtins.mail.sanitize import sanitize_html
|
|
|
|
|
from app.plugins.builtins.mail.serializers import (
|
2026-08-26 09:38:34 +02:00
|
|
|
account_to_response,
|
|
|
|
|
attachment_to_response,
|
|
|
|
|
folder_to_response,
|
|
|
|
|
label_to_response,
|
2026-08-26 14:33:30 +02:00
|
|
|
mail_to_response, # noqa: F401
|
2026-08-26 09:38:34 +02:00
|
|
|
rule_to_response,
|
|
|
|
|
signature_to_response,
|
|
|
|
|
template_to_response,
|
|
|
|
|
)
|
2026-08-26 14:33:30 +02:00
|
|
|
from app.plugins.builtins.mail.smtp_send import (
|
2026-08-26 13:06:46 +02:00
|
|
|
forward_mail,
|
|
|
|
|
reply_to_mail,
|
|
|
|
|
send_mail_via_smtp,
|
|
|
|
|
)
|
2026-08-26 14:33:30 +02:00
|
|
|
from app.plugins.builtins.mail.text_utils import (
|
2026-08-26 09:38:34 +02:00
|
|
|
_strip_html,
|
|
|
|
|
extract_email_addresses,
|
|
|
|
|
)
|
2026-07-01 15:41:27 +02:00
|
|
|
|
2026-07-16 23:08:37 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2026-08-26 14:33:30 +02:00
|
|
|
__all__ = [
|
|
|
|
|
# accounts
|
|
|
|
|
"create_mail_account",
|
|
|
|
|
"update_mail_account",
|
|
|
|
|
"account_to_response",
|
|
|
|
|
# crypto
|
|
|
|
|
"decrypt_password",
|
|
|
|
|
"encrypt_password",
|
|
|
|
|
"generate_salt",
|
|
|
|
|
# drafts & sync
|
|
|
|
|
"auto_sync_all_accounts",
|
|
|
|
|
"process_sync_queue",
|
|
|
|
|
"save_draft",
|
|
|
|
|
"update_draft",
|
|
|
|
|
# imap ops
|
|
|
|
|
"imap_delete_mail",
|
|
|
|
|
"imap_move_mail",
|
|
|
|
|
"imap_sync_mail_flags",
|
|
|
|
|
# imap sync
|
|
|
|
|
"_compute_thread_id",
|
|
|
|
|
"_get_german_folder_name",
|
|
|
|
|
"imap_sync_account",
|
|
|
|
|
"imap_sync_folder",
|
|
|
|
|
"get_account_password",
|
|
|
|
|
"_parse_imap_list_response",
|
|
|
|
|
"_parse_imap_quota_response",
|
|
|
|
|
# models
|
|
|
|
|
"Mail",
|
|
|
|
|
"MailAccount",
|
|
|
|
|
"MailAttachment",
|
|
|
|
|
"MailFolder",
|
|
|
|
|
"MailLabel",
|
|
|
|
|
"MailLabelAssignment",
|
|
|
|
|
"MailRule",
|
|
|
|
|
"MailSignature",
|
|
|
|
|
"MailTemplate",
|
|
|
|
|
"VacationSentLog",
|
|
|
|
|
# pgp
|
|
|
|
|
"import_pgp_private_key",
|
|
|
|
|
"import_pgp_public_key",
|
|
|
|
|
"pgp_decrypt_message",
|
|
|
|
|
"pgp_encrypt_message",
|
|
|
|
|
# rules & vacation
|
|
|
|
|
"apply_rules_to_mail",
|
|
|
|
|
"execute_rule_actions",
|
|
|
|
|
"log_vacation_sent",
|
|
|
|
|
"matches_condition",
|
|
|
|
|
"should_send_vacation_reply",
|
|
|
|
|
"substitute_template_vars",
|
|
|
|
|
# sanitize
|
|
|
|
|
"sanitize_html",
|
|
|
|
|
# serializers
|
|
|
|
|
"attachment_to_response",
|
|
|
|
|
"folder_to_response",
|
|
|
|
|
"label_to_response",
|
|
|
|
|
"rule_to_response",
|
|
|
|
|
"signature_to_response",
|
|
|
|
|
"template_to_response",
|
|
|
|
|
# smtp send
|
|
|
|
|
"forward_mail",
|
|
|
|
|
"reply_to_mail",
|
|
|
|
|
"send_mail_via_smtp",
|
|
|
|
|
# text utils
|
|
|
|
|
"_strip_html",
|
|
|
|
|
"extract_email_addresses",
|
|
|
|
|
]
|