Setup Django Application Form to send emails to your Gmail Inbox

sayemmh

Sayem Hoque

Posted on September 16, 2024

Setup Django Application Form to send emails to your Gmail Inbox

For September 2024 none of the guides I found online were up to date and comprehensive for this so just writing the steps to make it work here:

  1. https://myaccount.google.com/
  2. Under "Recent Security Activity" make sure 2FA is on
  3. Go to the search bar above, and search "App Passwords"

You'll be prompted as such - "To create a new app specific password, type a name for it below..."

  1. Name it something like "my-django-site-form"

It will generate a password you can use in your application. Store this securely somewhere. This can be used in your Django settings, but I opt for saving as an .env variable.

To test to make sure this setup is working, you can run the following code in the Django shell:

python manage.py shell
Enter fullscreen mode Exit fullscreen mode
from django.core.mail import send_mail
send_mail(
    'Test Email',
    'This is a test email.',
    'sender@email.com',
    ['recipient@example.com'],
    fail_silently=False,
)

Enter fullscreen mode Exit fullscreen mode

Replace the two emails, and you should see an email sent to recipient@example.com

💖 💪 🙅 🚩
sayemmh
Sayem Hoque

Posted on September 16, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related