0

I'm trying to make an SMTP live server as a separate app in django. Whenever I try to run the smtp file python smtp.py to listen to incoming messages and store them in models, I get ImproperlyConfigured error.

smtp.py

import os
import smtpd
import asyncore
import datetime
from models import IncomingEmail
from email.parser import Parser

class MessageServer():
    def __call__(self, peer, mailfrom, recipients, data):

        email = Parser().parsestr(data)

        incomingDb = IncomingEmail(
                sender=mailfrom, recipient=recipients,
                message=data, subject=email['subject'],
                recevied=datetime.datetime.now()
                )
        incomingDb.save()

class customSMTPServer(smtpd.SMTPServer):
    process_message = MessageServer()

if __name__ ==  "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings")
    server = customSMTPServer(('localhost', 1025), None)
    print "Started listening at port 1025"
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        server.close()

The project structure:

hello/
 manage.py
 hello/
  __init__.py
  settings.py
  urls.py
  wsgi.py
 smtp/
  smtp.py
  models.py
  __init__.py
 api/
  admin.py
  apps.py
  urls.py
  views.py

The error:

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environ
ment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
user1123975
  • 165
  • 4
  • 14

1 Answers1

1

Looks like you're getting that error because DB engin is missing some important settings. What if you edit your code like this?

import sys, os
sys.path.append('/path/to/your/app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'hello.settings'
from django.conf import settings
print "Started listening at port 1025"
...

Or another way is try to move os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings") on the top of your file (but after importing os, of course) Another a little bit dirty hack is:

if __name__ == "__main__":
    from django.core.management import execute_from_command_line    
    execute_from_command_line(sys.argv)

Next thing you could try:

$ ./manage.py shell
...
>>> execfile('your_script.py')

Also, which line throws and exception?

Paul
  • 6,641
  • 8
  • 41
  • 56
  • It didn't work and I got the same exception. I've also tried to set the environment variable to hello.settings and I get `ImportError: No module named hello.settings` – user1123975 Feb 20 '16 at 15:03
  • Is it even possible to make an SMTP server as a separate app in django? I've updated the question and hopefully this will give you a better insight into the project. – user1123975 Feb 20 '16 at 15:13
  • And wht do you need to make smtp server as a single app? – Paul Feb 20 '16 at 15:15
  • I'm trying to implement this project [link](https://github.com/kevinjqiu/mailchute) in django. It's my first big project in django, and i've learned a lot so far by do this. How would you suggest to build the smtp server in django? – user1123975 Feb 20 '16 at 15:31