Zoulou
Zoulou

Reputation: 403

Django : Send mail failed

I wrote this in terminal for test the messages sending in Django but I have a error and I don't find the solution... :

from le_site.models import *
import smtplib
send_mail('Subject here', 'Here is the message.','[email protected]',['[email protected]'],fail_silently=False)

The error (a exception) :

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 56, in send_mail
fail_silently=fail_silently)
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 37, in get_connection
klass = import_string(backend or settings.EMAIL_BACKEND)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/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.

My SETTINGS.PY (of Django) :

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
#AUTHENTICATION_BACKENDS = ('backend.EmailAuthBackend',)

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

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

ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

LANGUAGE_CODE = 'fr-FR'
TIME_ZONE = 'Europe/Paris'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
LOGIN_URL = '/mysite/'

So I think the value matched to messages sending are good, What I forget ?

Thank's

Upvotes: 3

Views: 5570

Answers (2)

clay1023w
clay1023w

Reputation: 1

If you are on a host with WHM enabled, like say Media Temple, i will save you some cursing, hair, etc.

There is a system level SMTP restriction configuration on these machines. Go in to WHM and find it and disable it. Like magic your SMTP works.

When the SMTP restriction is enabled, you will get 535 auth errors.

Upvotes: 0

C&#233;sar
C&#233;sar

Reputation: 10119

You haven't configured all the email settings, if you are just testing the messages you can use the Console backend instead and send the email to standard output. Try overriding the default EMAIL_BACKEND in you settings.py:

# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

If you are planning on sending emails with the default backend, you don't need to import smtplib, for that Django provides helper functions inside django.core.mail:

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', '[email protected]',
    ['[email protected]'], fail_silently=False)

Although, like I said before, you need to configure some things before hand. Take a look at the SMTP backend for more information.

Example settings:

# Email settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_SUBJECT_PREFIX = '[Test mail]'

Upvotes: 5

Related Questions