How to configure email sending in Django 1.8 using Gmail's SMTP server?

I’m stuck trying to set up email functionality in my Django 1.8 project using Gmail’s SMTP server. Here’s what I’ve done so far:

In my settings.py, I’ve added:

EMAIL_BACKEND = 'custom_ssl_email.SSLEmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'mypassword'
DEFAULT_FROM_EMAIL = '[email protected]'

Then, I tried to send an email like this:

from django.core import mail
mail.send_mail('Test Subject', 'Test Message', '[email protected]', ['[email protected]'])

But I keep getting an SMTPAuthenticationError. I’ve already tried enabling less secure apps and unlocking the captcha, but no luck.

Does anyone know what I’m doing wrong? How can I properly set up Django 1.8 to send emails through Gmail? Any help would be appreciated!

hey mate, i had similar troubles. try using port 587 instead of 465 and set EMAIL_USE_TLS = True in ur settings. also, make sure ur using the latest django version cuz older ones can be finicky with gmail. good luck!

I encountered similar issues when configuring Gmail SMTP for Django. In my experience, the resolution was to stop using the regular Gmail password and instead generate an app-specific password. First, enable 2-Step Verification on your Google account and then create an app-specific password for your Django project. I also switched to using the default SMTP email backend by setting EMAIL_BACKEND to ‘django.core.mail.backends.smtp.EmailBackend’. It is important to verify that your Gmail account is not blocking these login attempts, as Google often flags them as suspicious activity.

I’ve been down this road before, and it can be frustrating. One thing that worked for me was using environment variables for sensitive info like passwords. It’s more secure and flexible. Also, double-check your firewall settings - sometimes they can block outgoing SMTP connections. If all else fails, consider using a third-party email service like SendGrid or Mailgun. They often have better documentation and easier setup processes for Django projects. Just remember to thoroughly test your email functionality in a staging environment before pushing to production. Good luck with your project!