How to debug email rendering in Django
In the case of sending emails, you run into the problem that you don't run the SMTP server needed to send the email on the localhost. Anyone can think of sending emails through their regular email account to test what they need. This solution is quite inappropriate for several reasons:
- Some email providers (Google) require a higher level of communication security and will not allow you to use the classic SMTP AUTH authentication.
- If you send "bad" emails, they may not arrive at all as a result, because they will be blocked by Antispam filters and other mechanisms.
- It will suck to test generating emails for different users because you will have to have a different email address for each user.
For email testing on localhost use filebased EMAIL BACKEND.
- Set Filebased EMAIL_BACKEND in your project settings and let the emails generate to a folder within the project.
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend' EMAIL_FILE_PATH = os.path.join(BASE_DIR, "temp", "emails")
2. Send an email.
from django.core.mail import send_mail send_mail( 'Subject here', 'Here is the message.', 'firstname.lastname@example.org', ['email@example.com'], fail_silently=False, )
3. Go to the folder where the emails are generated.
4. Change the extensions of the generated file
mv 2022090410344444.log 2022090410344444.eml
5. Open the file using an email client such as Thunderbird or Evolution
Now you have exactly the same view of the email as if you had sent it via SMTP.