Django mails not being saved (File backend)
Asked Answered
D

6

12

I have configured Django to use a file backend for email sending on my local machine. This seemed to work fine earlier on, and all mails were recorded in the directory I had specified in my settings.py file:

EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = '/code/mails/'

However, this suddenly stopped working. I have checked the permissions of the folder, and this seems to be fine. There is no error that I can see. I'm using docker, and when I start the Python server I have the logs shown in my terminal. Normally when there is an error I see it there. But nothing appears. To test things, I have renamed the folder and tried sending a mail. This time, no error appears either. In production, where my settings.py are different but all else is the same, the emails are sent out just fine. So the code seems to be working, but the local filebased backend seems to be a problem.

Anybody any idea?

I have configured these log settings:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/code/logs/debug.log',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

The logs seem to work fine and provide detailed logging; but none of it related to the e-mail error.

Dawkins answered 19/4, 2018 at 7:38 Comment(3)
Can you share more information about your project like Python e Django versions and your complete local settings ?Littrell
Have you recently updated your Django version? Have you tried running locally through manage.py, and if so does it work there?Alburg
Can you also share relevant docker config - e.g. is /code/mails internal to the container or mounted from the host?Fiddlededee
L
3

As you can see in the code there's a lot of raises: https://github.com/django/django/blob/stable/2.1.x/django/core/mail/backends/filebased.py#L13

So if with your settings, and the correct permissions on your folder you cannot see email or errors maybe there's some settings that overwrite the two settings you posted here.

Check again your local setting and be sure there's only one EMAIL_BACKEND declared.

Littrell answered 18/12, 2018 at 17:21 Comment(0)
R
4

I had this issue when trying to get auth working, specifically the password reset. It took me hours to discover that the e-mail address I was using for the reset did not exist for any users in my database, and of course it's good security that Django remains silent about it. Dumb error but I wish someone had posted about it, so here it is.

Radiance answered 3/10, 2021 at 21:1 Comment(2)
I knew I needed to use an existing email address, but I didn't realise I hadn't loaded my fixtures. This is an easy one to miss - thanks for pointing it out!Adumbrate
Also note that the user must have an assigned email address, and that only putting an email address as a username doesn't satisfy this.Dispassionate
L
3

As you can see in the code there's a lot of raises: https://github.com/django/django/blob/stable/2.1.x/django/core/mail/backends/filebased.py#L13

So if with your settings, and the correct permissions on your folder you cannot see email or errors maybe there's some settings that overwrite the two settings you posted here.

Check again your local setting and be sure there's only one EMAIL_BACKEND declared.

Littrell answered 18/12, 2018 at 17:21 Comment(0)
T
2

Because there is no error, the directory exists and there are still no files created in your specified directory, it is possible that the code runs and creates the files but in a different directory.

What is your file_path keyword set to when you create a connection?

File backend

The file backend writes emails to a file. A new file is created for each new session that is opened on this backend. The directory to which the files are written is either taken from the EMAIL_FILE_PATH setting or from the file_path keyword when creating a connection with get_connection().

To specify this backend, put the following in your settings:

EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location

Django Docs File Backend

Tungstite answered 18/12, 2018 at 2:40 Comment(0)
E
0

It seems that EMAIL_FILE_PATH refers to the absolute path. For me this solution works:

EMAIL_FILE_PATH = os.path.join(BASE_DIR, 'app/emailfolder')
Ethylene answered 25/2, 2020 at 21:33 Comment(1)
for some reason this st returned only 'app/emailfolder' for me so I had it saved on C: the solution was EMAIL_FILE_PATH = BASE_DIR / "app/emailfolder"Bani
B
0

This fixed issue for me

EMAIL_BACKEND = "django.core.mail.backends.filebased.EmailBackend"
EMAIL_FILE_PATH = BASE_DIR / "app/emailfolder"
Bani answered 9/1 at 21:43 Comment(0)
A
-1

I spent some time on this myself and after some frustration, I just swapped the two variables, and it worked.

EMAIL_FILE_PATH = '/code/mails/'
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
Andresandresen answered 31/5, 2019 at 19:41 Comment(1)
This didn't work for me.Adumbrate

© 2022 - 2024 — McMap. All rights reserved.