Django 1.8 sending mail using gmail SMTP
Stefan Bogdanescu
Founder & Senior Architect
Mastering SMTP in Django 1.8: Solving the Gmail Authentication Nightmare
As a senior developer, I've seen countless times how simple tasks—like sending an email—can turn into frustrating debugging sessions. When dealing with external services like Gmail and older frameworks like Django 1.8, authentication issues are extremely common.
I recently encountered a very specific problem while trying to configure Django 1.8 to send emails via Gmail SMTP using the django_smtp_ssl backend. The error was a classic SMTPAuthenticationError, which, while seemingly simple, points to deep-seated security changes implemented by Google.
This post will walk you through exactly why this error occurs and provide the definitive, modern solution for successfully connecting Django applications to Gmail’s SMTP server.
The Setup and The Roadblock
Many developers attempt to use standard service credentials (username and password) directly for SMTP connections. Let's review the configuration that led to the issue:
# settings.py
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'sarath4coding'
EMAIL_HOST_PASSWORD = '*********' # This password caused the failure
DEFAULT_EMAIL_FROM = 'sarath4coding@gmail.com'
And the attempt to send mail:
from django.core import mail
mail.send_mail('subject','message','sarath4coding@gmail.com',['sarath4coding@gmail.com'])
Despite setting up the host, port, and credentials correctly in settings.py, the application failed at runtime with an SMTPAuthenticationError. The traceback clearly indicated that the server rejected the login attempt, suggesting a problem with how Google handles authentication for external applications.
Why Authentication Fails: The Security Shift
The error message you received—Please log in via your web browser and then try again—is not an error in your Django code; it is a security prompt from Google. This happens because Google has made it significantly harder to use standard account passwords for third-party applications, especially when Two-Factor Authentication (2FA) is enabled on the Gmail account.
When you enable 2FA, simply using your main account password is no longer sufficient for SMTP logins. To maintain security, Google requires a special type of password specifically generated for external apps.
The Definitive Solution: Using App Passwords
The only reliable way to resolve this issue is by generating an App Password within your Google Account settings. This creates a unique, restricted password that you can use in place of your regular account password for applications like Django.
Step-by-Step Configuration Guide
- Enable 2FA: Ensure Two-Factor Authentication is enabled on the Gmail account you are using.
- Access Google Security Settings: Log into your Google Account and navigate to the security settings section.
- Generate App Password: Look for the section related to "App Passwords" (sometimes under "Less secure app access," which is now deprecated). Follow the prompts to generate a new password specifically for an application.
- Use the Generated Password: Take the 16-character password that Google provides and use it in your Django
settings.py.
Here is how your updated configuration should look:
# settings.py (Updated)
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'sarath4coding@gmail.com' # Use your full Gmail address
EMAIL_HOST_PASSWORD = 'YOUR_GENERATED_APP_PASSWORD' # Use the special App Password here
DEFAULT_EMAIL_FROM = 'sarath4coding@gmail.com'
By substituting the standard password with this unique App Password, you satisfy Google’s security requirements, allowing the smtplib connection to authenticate successfully. This approach ensures that your application adheres to modern security standards, much like how robust systems are designed when building large-scale solutions, similar to principles seen in frameworks like Laravel where system integration is paramount.
Conclusion
Dealing with external service authentication often requires more than just correct syntax; it demands an understanding of the target service's security policies. The SMTPAuthenticationError you faced is a common hurdle for developers moving into modern secure environments. By switching from standard passwords to App Passwords, you bypass these restrictions and establish a secure, functional connection between your Django application and Gmail's SMTP server. Happy coding!
Note: Blog content is currently available in English.