# Path: dju_common/settings.py
# DJU_EMAIL_DEBUG_PATH = getattr(settings, 'DJU_EMAIL_DEBUG_PATH',
#                                os.path.join(LOG_DIR, 'debug_email').replace('\\', '/'))
# 
# DJU_EMAIL_DEBUG_IN_CONSOLE = getattr(settings, 'DJU_EMAIL_DEBUG_IN_CONSOLE', True)
# 
# DJU_EMAIL_DEBUG_IN_FILES = getattr(settings, 'DJU_EMAIL_DEBUG_IN_FILES', True)


import codecs
import sys
import os
import base64
import re
import datetime
import subprocess
import traceback
from smtpd import SMTPServer
from django.core.management.base import BaseCommand
from django.utils.encoding import force_unicode, force_str
from dju_common.settings import DJU_EMAIL_DEBUG_PATH, DJU_EMAIL_DEBUG_IN_CONSOLE, DJU_EMAIL_DEBUG_IN_FILES

    @staticmethod
    def _get_fn(fn_base, n=None):
        if n is None:
            return os.path.join(DJU_EMAIL_DEBUG_PATH, '{}.eml'.format(fn_base)).replace('\\', '/')
        else:
            return os.path.join(DJU_EMAIL_DEBUG_PATH, '{}_{}.eml'.format(fn_base, n)).replace('\\', '/')

    def process_message(self, peer, mailfrom, rcpttos, data):
        try:
            if DJU_EMAIL_DEBUG_IN_FILES:
                if not os.path.exists(DJU_EMAIL_DEBUG_PATH):
                    os.makedirs(DJU_EMAIL_DEBUG_PATH)
                fn_base = u'{}_{}_{}_{}'.format(
                    u'_'.join(rcpttos),
                    self._get_subject(data),
                    mailfrom,
                    datetime.datetime.now().strftime(u'%Y-%m-%d_%H-%M-%S')
                )
                fn_base = re.sub(ur'[:\*\?"<>\| ]+', '_', fn_base, re.U)
                fn_base = force_str(fn_base)
                fn = self._get_fn(fn_base)
                n = 1
                while os.path.exists(fn):
                    fn = self._get_fn(fn_base, n)
                    n += 1
                f = codecs.open(fn, 'w', encoding='utf-8')
            inheaders = 1
            for line in data.split('\n'):
                if inheaders and not line:

# Path: dju_common/settings.py
# DJU_EMAIL_DEBUG_PATH = getattr(settings, 'DJU_EMAIL_DEBUG_PATH',
#                                os.path.join(LOG_DIR, 'debug_email').replace('\\', '/'))
# 
# DJU_EMAIL_DEBUG_IN_CONSOLE = getattr(settings, 'DJU_EMAIL_DEBUG_IN_CONSOLE', True)
# 
# DJU_EMAIL_DEBUG_IN_FILES = getattr(settings, 'DJU_EMAIL_DEBUG_IN_FILES', True)


# Path: dju_common/management/commands/dju_debug_email_server.py
import codecs
import sys
import os
import base64
import re
import datetime
import subprocess
import traceback
from smtpd import SMTPServer
from django.core.management.base import BaseCommand
from django.utils.encoding import force_unicode, force_str
from dju_common.settings import DJU_EMAIL_DEBUG_PATH, DJU_EMAIL_DEBUG_IN_CONSOLE, DJU_EMAIL_DEBUG_IN_FILES


    @staticmethod
    def _get_fn(fn_base, n=None):
        if n is None:
            return os.path.join(DJU_EMAIL_DEBUG_PATH, '{}.eml'.format(fn_base)).replace('\\', '/')
        else:
            return os.path.join(DJU_EMAIL_DEBUG_PATH, '{}_{}.eml'.format(fn_base, n)).replace('\\', '/')

    def process_message(self, peer, mailfrom, rcpttos, data):
        try:
            if DJU_EMAIL_DEBUG_IN_FILES:
                if not os.path.exists(DJU_EMAIL_DEBUG_PATH):
                    os.makedirs(DJU_EMAIL_DEBUG_PATH)
                fn_base = u'{}_{}_{}_{}'.format(
                    u'_'.join(rcpttos),
                    self._get_subject(data),
                    mailfrom,
                    datetime.datetime.now().strftime(u'%Y-%m-%d_%H-%M-%S')
                )
                fn_base = re.sub(ur'[:\*\?"<>\| ]+', '_', fn_base, re.U)
                fn_base = force_str(fn_base)
                fn = self._get_fn(fn_base)
                n = 1
                while os.path.exists(fn):
                    fn = self._get_fn(fn_base, n)
                    n += 1
                f = codecs.open(fn, 'w', encoding='utf-8')
            inheaders = 1
            for line in data.split('\n'):
                if inheaders and not line:

                    if DJU_EMAIL_DEBUG_IN_FILES: