user7965805
user7965805

Reputation:

Django error : smtplib.SMTPDataError

>>> from django.core.mail import send_mail>>> send_mail("allo", 'here is the message', '[email protected]', ['[email protected]'], fail_silently=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jeremie/Desktop/email_test/myvenv/lib/python3.5/site-packages/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/home/jeremie/Desktop/email_test/myvenv/lib/python3.5/site-packages/django/core/mail/message.py", line 348, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/jeremie/Desktop/email_test/myvenv/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 111, in send_messages
    sent = self._send(message)
  File "/home/jeremie/Desktop/email_test/myvenv/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 127, in _send
    self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
  File "/usr/lib/python3.5/smtplib.py", line 883, in sendmail
    raise SMTPDataError(code, resp)
smtplib.SMTPDataError: (421, b'4.3.0 collect: Cannot write ./dfv4IKaDvq007500 (bfcommit, uid=0, gid=133): No such file or directory')

I did export DJANGO_SETTINGS_MODULE=email_name.settings in bash where email_name is the name of my django app. Because as you could see I am working with interactive python.

I am trying to send an email, and I obtain this error. How could I fix this issue?

Thanks!

P.S. If the question is unclear, please let me know.


The settings.py :

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*^-s1-k$lz4noyi0yhz89&bmw1pz5g^pufkswal7@efva(_-fj@'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
]

ROOT_URLCONF = 'email_name.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'email_name.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

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


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

Notice that I am not looking for to create a django project, but rather test out the method send_mail(). That's why the code settings.py is standard.

Upvotes: 0

Views: 6117

Answers (1)

Matt Cremeens
Matt Cremeens

Reputation: 5151

I believe in your settings.py you are going to need to add some email configuration, like this

EMAIL_FROM = '[email protected]'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True

Then you could create a view that calls the send_mail function. Something like this

def send_an_email(request):
    from django.core.mail import send_mail
    send_mail('Subject here', 'Here is the message.', '[email protected]', [[email protected]'], fail_silently=False,)
    return render(request, '/email_sent.html')

Reference

Of course you'll need a url to call this view. So in your urls.py (I assume you have this already) have something like this

url(r'^send_my_mail/', 'Project_name.views.mail.send_an_email', name='mail')

And then visit the url to see if the email got sent properly.

Upvotes: 1

Related Questions