Search code examples
pythondjangogoogle-app-enginedjangoappengine

appengine sending mail getting no error for hardcoded emails, but errors when using the data sent in form


EDIT: In case you want to test i have deployed the code to this github repo

The basic feature i want to implement is sending mails from a form. Almost what anyother Contact Us form on your website does. The demo url is takemailer.appspot.com

The form as can be seen in url, sends a post request to the server. The views where i handle the request is as follows:

def post_data(request):   
    logging.info(request.POST)
    frm_name = request.POST['name']    frm_mail = request.POST['email']
    frm = frm_name + " <" + frm_mail + ">"
    frm = '"%s"' % frm #above two lines # done to produce a format like "Name <[email protected]>"
    sub = request.POST['subject']
    cmnt = request.POST['comment']
    extra = str(frm + sub +  cmnt)  
    logging.info(frm)
    a = mail.send_mail(sender=frm,
              to="Albert Johnson <du***@gmail.com>",
              subject=sub,
              body=cmnt) 
    logging.info(a)
    return http.HttpResponse("1")

The above version of the code does not work and raises

<class 'django.core.exceptions.ImproperlyConfigured'>: You haven't set the DATABASE_ENGINE setting yet.

Stacktrace attached at bottom.

However if i modify the views function to hardcode a from email as shown below it works seamlessly:

def post_data(request):   
        logging.info(request.POST)
        sub = request.POST['subject']
        cmnt = request.POST['comment']
        a = mail.send_mail(sender="Albert Johnson <du***@gmail.com>",
                  to="Albert Johnson <du***@gmail.com>",
                  subject=sub,
                  body=cmnt) 
        logging.info(a)
        return http.HttpResponse("1")

Any idea of why the above is working and the one above this is not working, and raising such an error?

Stacktrace:

Traceback (most recent call last):
  File "/base/data/home/apps/s~takemailer/1.357442066172211834/django_bootstrap.py", line 65, in <module>
    main()
  File "/base/data/home/apps/s~takemailer/1.357442066172211834/django_bootstrap.py", line 62, in main
    util.run_wsgi_app(application)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/util.py", line 98, in run_wsgi_app
    run_bare_wsgi_app(add_wsgi_middleware(application))
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/util.py", line 116, in run_bare_wsgi_app
    result = application(env, _start_response)
  File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/core/handlers/wsgi.py", line 189, in __call__
    response = self.get_response(request)
  File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/core/handlers/base.py", line 115, in get_response
    receivers = dispatcher.send(signal=signals.got_request_exception)
  File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/dispatch/dispatcher.py", line 360, in send
    **named
  File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/dispatch/robustapply.py", line 47, in robustApply
    return receiver(*arguments, **named)
  File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/db/__init__.py", line 47, in _rollback_on_exception
    transaction.rollback_unless_managed()
  File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/db/transaction.py", line 145, in rollback_unless_managed
    connection._rollback()
  File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/db/backends/dummy/base.py", line 13, in complain
    raise ImproperlyConfigured, "You haven't set the DATABASE_ENGINE setting yet."

this question is a follow up question from here since that question did not get any answer, i kept trying further, the problem has boiled down to this small measure:


Solution

  • Well the error does not lie in code, it lies i google's policy :) When you read through it, through your app the one who can send email should have either:

    1. an email with same domain name.
    2. The admin of the app.

    Hope you get your answer.