Problems Sending Email From Django SIte

782 views
Skip to first unread message

Mark Phillips

unread,
Jan 31, 2014, 8:06:44 PM1/31/14
to django users
I have my django site setup on my production server, a Debian machine with exim4 and django 1.6. I use smtp.cox.net to relay my emails from my server. 

I can send emails from the server using mutt. 

I can also send an email from a python prompt in my virtual environment on the server using this (with a different to and from address) after importing my projects settings.py file:

>>> from django.core.mail import send_mail
>>> send_mail('Subject here', 'Here is the message.', 'fr...@example.com', ['t...@example.com'], fail_silently=False)

However, when I get an error on the site (ie self-induced), I do not get any emails from the site. I have ADMINS setup correctly per the docs. In settings.py I just have the basics - localhost and port 25 for email.

Perhaps the error I introduced was to severe (an import error is settings.py). I also tried accessing a page that does not exist, and no email.

My first question: How can I simulate/force an error on the site to get an email so I can make sure the site can send an email to the admins?

Thanks,

Mark

Mark Phillips

unread,
Jan 31, 2014, 8:15:36 PM1/31/14
to django users
A follow up. After hitting send, I realized how to simulate an error - I asked for a page with a nonexistent id. Like the polls example in the django tutorial, I used an url with a poll_id that does not exist in my site. I got a Server Error (500) page, but no email. I looked in the exim4 reject log and found:

2014-01-31 18:07:04 unqualified recipient rejected: <a> H=localhost [127.0.0.1]

I googled this error and have not had any luck figuring it out. Since I can send email from the command line and a python script with the same settings.py file as my site, could I be missing something in the django setup for sending emails to admins when there is a site error? 

All I have in settings.py for email is localhost, port 25, and admins email addresses. Do I need something else? Some module or middleware thing-a-ma-bob? I just have the stock modules and middleware that django created with a new project. I haven't found anything in the django docs that point to a module that I am missing.

Thanks!

Mark

m1chael

unread,
Jan 31, 2014, 9:26:53 PM1/31/14
to django...@googlegroups.com
Your mail server needs configuring.

On Fri, Jan 31, 2014 at 8:15 PM, Mark Phillips
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAEqej2MLB-b7_cRZn7pEnCP1bTTm-zWuxyGC%3DK9UcZgMZq1pvw%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/groups/opt_out.

Mark Phillips

unread,
Jan 31, 2014, 10:35:54 PM1/31/14
to django users
Thank-you for your response...However, 

1. I can send email from the command line and from mutt.

2. I can send email from a python prompt using the settings.py in my app.

These two facts tell me the mail server is configured properly.

3. Only my django app cannot send email.

This fact leads me to believe my django app is mis-configured for sending email. In all other aspects, my app works as designed.

Do you have any other helpful suggestions?

Mark


m1chael

unread,
Jan 31, 2014, 10:49:42 PM1/31/14
to django...@googlegroups.com
can you tail a mail server log file, while trying to send a django
e-mail, and see what happens? also take a look at a log file for
command line and mutt.... do a comparison of all. maybe that will help

On Fri, Jan 31, 2014 at 10:35 PM, Mark Phillips
> https://groups.google.com/d/msgid/django-users/CAEqej2Oh2UKAic%3DS1FTNpsXaO474JrYzEH3Co%3DBPvpAMifEc5w%40mail.gmail.com.

Mark Phillips

unread,
Jan 31, 2014, 11:51:32 PM1/31/14
to django users
Exim4 has two logs - main and reject.

Message from python prompt and mutt - shows activity in main, but not reject.

Message from Server 500 error with django app - no activity in main, and a message in reject - 

2014-01-31 21:30:55 unqualified recipient rejected: <a> H=localhost [127.0.0.1]

That is all I have. 

My settings.py files...maybe you will see something missing or mis-configured for email from a django site (I removed some sensitive information).

The first file base.py:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'south',
    'inventory',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

DEBUG = False

TEMPLATE_DEBUG = False

ROOT_URLCONF = 'inventory_project.urls'

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Phoenix'

USE_I18N = True

USE_L10N = True

USE_TZ = True

The second file base_beagle.py:

# settings/base_beagle.py
from base import *

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

The third file prod_beagle.py

# settings/prod_beagle.py
from base_beagle import *

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

TEMPLATE_DEBUG = False

ALLOWED_HOSTS = ['192.168.5.126', 'localhost']

EMAIL_HOST = "localhost"
EMAIL_PORT = 25
EMAIL_SUBJECT_PREFIX = "[Django-beagle] "

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'inventory_prod',
        'USER': '********',
        'PASSWORD': '*******',
        }
}

INTERNAL_IPS = ("127.0.0.1","192.168.5.126")

ADMINS = (('mark', 'x...@xxxxxxxxxx.xxx'))

LOGIN_URL = '/mom/inventory/login/'

WSGI_APPLICATION = "inventory_project.wsgi.application"


Thanks,

Mark


m1chael

unread,
Feb 1, 2014, 9:55:46 AM2/1/14
to django...@googlegroups.com
I'm just guessing now.... try adding the setting:

DEFAULT_FROM_EMAIL='x...@xxxxx.xxx'

On Fri, Jan 31, 2014 at 11:51 PM, Mark Phillips
> https://groups.google.com/d/msgid/django-users/CAEqej2Mp-zMFwk7%3DyDX6-0vBcoG%2BJJuKN9o7LUONQcfZqWg2Vg%40mail.gmail.com.

Mark Phillips

unread,
Feb 1, 2014, 10:51:37 AM2/1/14
to django users
Thanks.....what email address...the one in admins?

Mark


m1chael

unread,
Feb 1, 2014, 10:59:35 AM2/1/14
to django...@googlegroups.com

Mark Phillips

unread,
Feb 1, 2014, 11:08:48 AM2/1/14
to django users
No joy.....same reject error and no emall sent.

I am stumped on how to diagnose this problem.

Mark


m1chael

unread,
Feb 1, 2014, 12:52:37 PM2/1/14
to django...@googlegroups.com
check Exim options relating to unqualified addresses

On Sat, Feb 1, 2014 at 11:08 AM, Mark Phillips
> https://groups.google.com/d/msgid/django-users/CAEqej2Od3VzhzsJad%2BETU9JM8eVhNOzd91g_oOqe8C7XwtnaTQ%40mail.gmail.com.

m1chael

unread,
Feb 1, 2014, 12:54:25 PM2/1/14
to django...@googlegroups.com
also, what is "a" ?? are you trying to email a...@something.com ?? or just 'a' ??

m1chael

unread,
Feb 1, 2014, 12:57:01 PM2/1/14
to django...@googlegroups.com
have you tried adding 'localhost' to 'local_domains'?

m1chael

unread,
Feb 1, 2014, 12:58:30 PM2/1/14
to django...@googlegroups.com
maybe try

EMAIL_HOST = "0.0.0.0"

m1chael

unread,
Feb 1, 2014, 5:35:26 PM2/1/14
to django...@googlegroups.com
any luck?

Matt Schinckel

unread,
Feb 1, 2014, 7:15:43 PM2/1/14
to django...@googlegroups.com
Your ADMINS setting is incorrect.

In python, the value ((a,b)) is not a 1-tuple containing a 2-tuple, as you want, 
but a single 2-tuple, which django interprets as a tuple containing _just_ email
addresses.

Try:

ADMINS = (('Mark', 'x...@example.com'),)

Note the trailing comma after the inner tuple.

Matt.

Mark Phillips

unread,
Feb 1, 2014, 8:04:38 PM2/1/14
to django users
Matt,

That fixed it! Now, I get error messages as expected.

Thanks so much for finding my missing comma!!!!!!

Mark


m1chael

unread,
Feb 2, 2014, 8:32:38 AM2/2/14
to django...@googlegroups.com

markmnl

unread,
May 13, 2014, 1:45:33 AM5/13/14
to django...@googlegroups.com
Exact same problems, thanks. Of all the things I love about Python that one has caught me out a few times.
Reply all
Reply to author
Forward
0 new messages