mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-19 10:19:27 -05:00
add feature to consume imap mail als .eml
This commit is contained in:
parent
5fcf1b5434
commit
cca576f518
@ -56,6 +56,7 @@ class MailRuleAdmin(admin.ModelAdmin):
|
|||||||
"filter_body",
|
"filter_body",
|
||||||
"filter_attachment_filename",
|
"filter_attachment_filename",
|
||||||
"maximum_age",
|
"maximum_age",
|
||||||
|
"consumption_scope",
|
||||||
"attachment_type",
|
"attachment_type",
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
@ -269,8 +269,11 @@ class MailAccountHandler(LoggingMixin):
|
|||||||
|
|
||||||
return total_processed_files
|
return total_processed_files
|
||||||
|
|
||||||
def handle_message(self, message, rule) -> int:
|
def handle_message(self, message, rule: MailRule) -> int:
|
||||||
if not message.attachments:
|
if (
|
||||||
|
not message.attachments
|
||||||
|
and rule.consumption_scope == MailRule.ConsumptionScope.ATTACHMENTS_ONLY
|
||||||
|
):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
self.log(
|
self.log(
|
||||||
@ -286,6 +289,43 @@ class MailAccountHandler(LoggingMixin):
|
|||||||
|
|
||||||
processed_attachments = 0
|
processed_attachments = 0
|
||||||
|
|
||||||
|
if (
|
||||||
|
rule.consumption_scope == MailRule.ConsumptionScope.EML_ONLY
|
||||||
|
or rule.consumption_scope == MailRule.ConsumptionScope.EVERYTHING
|
||||||
|
):
|
||||||
|
os.makedirs(settings.SCRATCH_DIR, exist_ok=True)
|
||||||
|
_, temp_filename = tempfile.mkstemp(
|
||||||
|
prefix="paperless-mail-",
|
||||||
|
dir=settings.SCRATCH_DIR,
|
||||||
|
)
|
||||||
|
with open(temp_filename, "wb") as f:
|
||||||
|
f.write(message.obj.as_bytes())
|
||||||
|
|
||||||
|
self.log(
|
||||||
|
"info",
|
||||||
|
f"Rule {rule}: "
|
||||||
|
f"Consuming eml from mail "
|
||||||
|
f"{message.subject} from {message.from_}",
|
||||||
|
)
|
||||||
|
|
||||||
|
async_task(
|
||||||
|
"documents.tasks.consume_file",
|
||||||
|
path=temp_filename,
|
||||||
|
override_filename=pathvalidate.sanitize_filename(
|
||||||
|
message.subject + ".eml",
|
||||||
|
),
|
||||||
|
override_title=message.subject,
|
||||||
|
override_correspondent_id=correspondent.id if correspondent else None,
|
||||||
|
override_document_type_id=doc_type.id if doc_type else None,
|
||||||
|
override_tag_ids=[tag.id] if tag else None,
|
||||||
|
task_name=message.subject[:100],
|
||||||
|
)
|
||||||
|
processed_attachments += 1
|
||||||
|
|
||||||
|
if (
|
||||||
|
rule.consumption_scope == MailRule.ConsumptionScope.ATTACHMENTS_ONLY
|
||||||
|
or rule.consumption_scope == MailRule.ConsumptionScope.EVERYTHING
|
||||||
|
):
|
||||||
for att in message.attachments:
|
for att in message.attachments:
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
# Generated by Django 4.0.4 on 2022-04-14 22:36
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("paperless_mail", "0009_alter_mailrule_action_alter_mailrule_folder"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="mailrule",
|
||||||
|
name="consumption_scope",
|
||||||
|
field=models.PositiveIntegerField(
|
||||||
|
choices=[
|
||||||
|
(1, "Only process attachments."),
|
||||||
|
(
|
||||||
|
2,
|
||||||
|
"Process full Mail (with embedded attachments in file) as .eml",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
3,
|
||||||
|
"Process full Mail (with embedded attachments in file) as .eml + process attachments as separate documents",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
default=1,
|
||||||
|
verbose_name="consumption scope",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
@ -56,6 +56,14 @@ class MailRule(models.Model):
|
|||||||
verbose_name = _("mail rule")
|
verbose_name = _("mail rule")
|
||||||
verbose_name_plural = _("mail rules")
|
verbose_name_plural = _("mail rules")
|
||||||
|
|
||||||
|
class ConsumptionScope(models.IntegerChoices):
|
||||||
|
ATTACHMENTS_ONLY = 1, _("Only process attachments.")
|
||||||
|
EML_ONLY = 2, _("Process full Mail (with embedded attachments in file) as .eml")
|
||||||
|
EVERYTHING = 3, _(
|
||||||
|
"Process full Mail (with embedded attachments in file) as .eml "
|
||||||
|
"+ process attachments as separate documents",
|
||||||
|
)
|
||||||
|
|
||||||
class AttachmentProcessing(models.IntegerChoices):
|
class AttachmentProcessing(models.IntegerChoices):
|
||||||
ATTACHMENTS_ONLY = 1, _("Only process attachments.")
|
ATTACHMENTS_ONLY = 1, _("Only process attachments.")
|
||||||
EVERYTHING = 2, _("Process all files, including 'inline' " "attachments.")
|
EVERYTHING = 2, _("Process all files, including 'inline' " "attachments.")
|
||||||
@ -144,6 +152,12 @@ class MailRule(models.Model):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
consumption_scope = models.PositiveIntegerField(
|
||||||
|
_("consumption scope"),
|
||||||
|
choices=ConsumptionScope.choices,
|
||||||
|
default=ConsumptionScope.ATTACHMENTS_ONLY,
|
||||||
|
)
|
||||||
|
|
||||||
action = models.PositiveIntegerField(
|
action = models.PositiveIntegerField(
|
||||||
_("action"),
|
_("action"),
|
||||||
choices=AttachmentAction.choices,
|
choices=AttachmentAction.choices,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user