mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-10 06:48:19 +02:00
changes main directory to mailbox2
This commit is contained in:
parent
010611dc55
commit
5177ea5c65
83 changed files with 4 additions and 53025 deletions
0
django_mailbox2/management/commands/__init__.py
Normal file
0
django_mailbox2/management/commands/__init__.py
Normal file
23
django_mailbox2/management/commands/getmail.py
Normal file
23
django_mailbox2/management/commands/getmail.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import logging
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from django_mailbox.models import Mailbox
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
mailboxes = Mailbox.active_mailboxes.all()
|
||||
if args:
|
||||
mailboxes = mailboxes.filter(name=" ".join(args))
|
||||
for mailbox in mailboxes:
|
||||
logger.info("Gathering messages for %s", mailbox.name)
|
||||
messages = mailbox.get_new_mail()
|
||||
for message in messages:
|
||||
logger.info(
|
||||
"Received %s (from %s)", message.subject, message.from_address
|
||||
)
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
import email
|
||||
import logging
|
||||
import sys
|
||||
|
||||
try:
|
||||
from email import utils
|
||||
except ImportError:
|
||||
import rfc822 as utils
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from django_mailbox.models import Mailbox
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
args = "<[Mailbox Name (optional)]>"
|
||||
help = "Receive incoming mail via stdin"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
"mailbox_name",
|
||||
nargs="?",
|
||||
help="The name of the mailbox that will receive the message",
|
||||
)
|
||||
|
||||
def handle(self, mailbox_name=None, *args, **options):
|
||||
message = email.message_from_string(sys.stdin.read())
|
||||
if message:
|
||||
if mailbox_name:
|
||||
mailbox = self.get_mailbox_by_name(mailbox_name)
|
||||
else:
|
||||
mailbox = self.get_mailbox_for_message(message)
|
||||
mailbox.process_incoming_message(message)
|
||||
logger.info("Message received from %s", message["from"])
|
||||
else:
|
||||
logger.warning("Message not processable.")
|
||||
|
||||
def get_mailbox_by_name(self, name):
|
||||
mailbox, created = Mailbox.objects.get_or_create(
|
||||
name=name,
|
||||
)
|
||||
return mailbox
|
||||
|
||||
def get_mailbox_for_message(self, message):
|
||||
email_address = utils.parseaddr(message["to"])[1][0:255]
|
||||
return self.get_mailbox_by_name(email_address)
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
import email
|
||||
import hashlib
|
||||
import logging
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from django_mailbox.models import MessageAttachment, Message
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""Briefly, a bug existed in a migration that may have caused message
|
||||
attachments to become disassociated with their messages. This management
|
||||
command will read through existing message attachments and attempt to
|
||||
re-associate them with their original message.
|
||||
|
||||
This isn't foolproof, I'm afraid. If an attachment exists twice, it will
|
||||
be associated only with the most recent e-mail message. That said,
|
||||
I'm quite sure that the bug in the migration is gone (and you'd have to
|
||||
have been quite unlucky to have ran the bad migration).
|
||||
|
||||
"""
|
||||
|
||||
def handle(self, *args, **options):
|
||||
attachment_hash_map = {}
|
||||
|
||||
attachments_without_messages = MessageAttachment.objects.filter(
|
||||
message=None
|
||||
).order_by("id")
|
||||
|
||||
if attachments_without_messages.count() < 1:
|
||||
return
|
||||
|
||||
for attachment in attachments_without_messages:
|
||||
md5 = hashlib.md5()
|
||||
for chunk in attachment.document.file.chunks():
|
||||
md5.update(chunk)
|
||||
attachment_hash_map[md5.hexdigest()] = attachment.pk
|
||||
|
||||
for message_record in Message.objects.all().order_by("id"):
|
||||
message = email.message_from_string(message_record.body)
|
||||
if message.is_multipart():
|
||||
for part in message.walk():
|
||||
if part.get_content_maintype() == "multipart":
|
||||
continue
|
||||
if part.get("Content-Disposition") is None:
|
||||
continue
|
||||
md5 = hashlib.md5()
|
||||
md5.update(part.get_payload(decode=True))
|
||||
digest = md5.hexdigest()
|
||||
if digest in attachment_hash_map:
|
||||
attachment = MessageAttachment.objects.get(
|
||||
pk=attachment_hash_map[digest]
|
||||
)
|
||||
attachment.message = message_record
|
||||
attachment.save()
|
||||
logger.info(
|
||||
"Associated message %s with attachment %s (%s)",
|
||||
message_record.pk,
|
||||
attachment.pk,
|
||||
digest,
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
"%s(%s) not found in currently-stored attachments",
|
||||
part.get_filename(),
|
||||
digest,
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue