mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-09 22:38:19 +02:00
new setting to delete email from server; don't store duplicate emails;
This commit is contained in:
parent
201f568750
commit
5bdd3703f1
3 changed files with 21 additions and 4 deletions
|
|
@ -2,13 +2,18 @@ import logging
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
from django_mailbox.models import Mailbox
|
from django_mailbox.models import Mailbox, Message
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
|
def message_not_in_database(message):
|
||||||
|
if Message.objects.filter(message_id__iexact=message['message-id']).exists():
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
mailboxes = Mailbox.active_mailboxes.all()
|
mailboxes = Mailbox.active_mailboxes.all()
|
||||||
|
|
@ -21,7 +26,8 @@ class Command(BaseCommand):
|
||||||
'Gathering messages for %s',
|
'Gathering messages for %s',
|
||||||
mailbox.name
|
mailbox.name
|
||||||
)
|
)
|
||||||
messages = mailbox.get_new_mail()
|
|
||||||
|
messages = mailbox.get_new_mail(condition=message_not_in_database)
|
||||||
for message in messages:
|
for message in messages:
|
||||||
logger.info(
|
logger.info(
|
||||||
'Received %s (from %s)',
|
'Received %s (from %s)',
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ import six
|
||||||
|
|
||||||
from poplib import POP3, POP3_SSL
|
from poplib import POP3, POP3_SSL
|
||||||
|
|
||||||
|
from django_mailbox import utils
|
||||||
|
|
||||||
from .base import EmailTransport, MessageParseError
|
from .base import EmailTransport, MessageParseError
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -29,6 +31,8 @@ class Pop3Transport(EmailTransport):
|
||||||
return '\r\n'.join(message_lines)
|
return '\r\n'.join(message_lines)
|
||||||
|
|
||||||
def get_message(self, condition=None):
|
def get_message(self, condition=None):
|
||||||
|
settings = utils.get_settings()
|
||||||
|
|
||||||
message_count = len(self.server.list()[1])
|
message_count = len(self.server.list()[1])
|
||||||
for i in range(message_count):
|
for i in range(message_count):
|
||||||
try:
|
try:
|
||||||
|
|
@ -43,6 +47,8 @@ class Pop3Transport(EmailTransport):
|
||||||
yield message
|
yield message
|
||||||
except MessageParseError:
|
except MessageParseError:
|
||||||
continue
|
continue
|
||||||
self.server.dele(i + 1)
|
|
||||||
|
if settings['delete_original_message']:
|
||||||
|
self.server.dele(i + 1)
|
||||||
self.server.quit()
|
self.server.quit()
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,11 @@ def get_settings():
|
||||||
settings,
|
settings,
|
||||||
'DJANGO_MAILBOX_default_charset',
|
'DJANGO_MAILBOX_default_charset',
|
||||||
'iso8859-1',
|
'iso8859-1',
|
||||||
|
),
|
||||||
|
'delete_original_message': getattr(
|
||||||
|
settings,
|
||||||
|
'DJANGO_MAILBOX_DELETE_ORIGINAL_MESSAGE',
|
||||||
|
True
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue