Django:发送邮件失败

时间:2015-11-13 19:23:43

标签: python django smtp django-email

我在终端写这个用于测试在Django中发送的消息,但是我有一个错误,我找不到解决方案......:

from le_site.models import *
import smtplib
send_mail('Subject here', 'Here is the message.','name@yahoo.fr',['name@yahoo.fr'],fail_silently=False)

错误(例外):

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.

我的 SETTINGS.PY (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/'

所以我认为与发送邮件相匹配的价值是好的,我忘记了什么?

感谢的

2 个答案:

答案 0 :(得分:2)

您尚未配置所有电子邮件设置,如果您只是测试消息,则可以使用Console backend并将电子邮件发送到标准输出。尝试覆盖EMAIL_BACKEND中的默认settings.py

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

如果您打算使用默认后端发送电子邮件,则不需要导入smtplib,因为Django在django.core.mail内提供了帮助函数:

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ['to@example.com'], fail_silently=False)

虽然像我之前说的那样,你需要事先配置一些东西。有关详细信息,请查看SMTP backend

示例设置:

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

答案 1 :(得分:0)

如果您在启用了WHM的主机上,比如Media Temple,我会为您节省一些诅咒,头发等。

这些计算机上存在系统级SMTP限制配置。进入WHM并找到并禁用它。像魔术一样,你的SMTP可以工作。

启用SMTP限制后,您将收到535个身份验证错误。