0

I used the following python code to send out emails using Django:

from django.core.mail import EmailMessage

 csvfile=StringIO.StringIO()
          csvwriter=csv.writer(csvfile)
          for l in my_list:
                csvwriter.writerow(l)

          now=datetime.datetime.now()
          datestr=now.strftime("%Y%m%d")
          message=EmailMessage( subject="good codes", body="your codes are all good",from_email="natasha.che@fox.com",to=["natasha.che@fox.com"])
          message.attach("good_codes_{}.csv".format(datestr),csvfile.getvalue(),'text/csv')
          message.send()

And I keep getting the error:

Traceback (most recent call last):
  File "dece2.py", line 166, in <module>
    csv_dict_test(my_list)
  File "dece2.py", line 145, in csv_dict_test
    message.send()
  File "/home/ubuntu/django-trunk/django/core/mail/message.py", line 283, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/ubuntu/django-trunk/django/core/mail/message.py", line 244, in get_connection
    self.connection = get_connection(fail_silently=fail_silently)
  File "/home/ubuntu/django-trunk/django/core/mail/__init__.py", line 37, in     get_connection
    klass = import_string(backend or settings.EMAIL_BACKEND)
  File "/home/ubuntu/django-trunk/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/home/ubuntu/django-trunk/django/conf/__init__.py", line 40, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_BACKEND, but settings are not configured. You must either define the environment variable     DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I am wondering how to fix it, if I want to setup settings, which name of the settings I should use here. I mean the real name. Thanks

user3634601
  • 127
  • 1
  • 2
  • 5
  • Is this something wrong with my code or something wrong with the Django package I installed. I tried for both windows and linux, same code, and package, neither of them work. – user3634601 Sep 24 '14 at 00:23
  • https://docs.djangoproject.com/en/dev/topics/email/#topic-email-backends Personally I've used the console backend and also gmail using something like this: http://stackoverflow.com/a/23402208/253127 – nmz787 Sep 24 '14 at 00:25
  • I found these two pages: http://stackoverflow.com/questions/13848938/django-test-framework-with-file-based-email-backend-server http://stackoverflow.com/questions/15556499/django-db-settings-improperly-configured-error. do not know how to apply them to this case – user3634601 Sep 24 '14 at 00:27
  • Did you read that second one? – Ignacio Vazquez-Abrams Sep 24 '14 at 00:28
  • sorry, I was adding comments before I saw the second one. – user3634601 Sep 24 '14 at 00:30
  • And I do not know how to find out the setting file... – user3634601 Sep 24 '14 at 00:33
  • it's the settings.py in your project (aka app) – nmz787 Sep 24 '14 at 02:05

0 Answers0