Menu
×
Γ—
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS PAYMENT INTEGRATION API BASICS NUMPY FOR ML Roadmap
     β―   

HOW TO

Send Emails from Django Server

×

Share this Topic

Share Via:

Thank you for sharing!


πŸ“§ How to Send Email from Your Django Server Using a Gmail Account

Sending emails from your Django application is a common task—whether for password resets, welcome messages, or admin alerts. This guide walks you through sending emails using your Gmail account, and also offers tips for generalizing your email setup for production environments.

πŸš€ Use Cases

Before diving in, here’s why you might want to send email from your Django server:

  • Welcome emails after registration
  • Password reset or account verification links
  • Contact form messages
  • Admin notifications

πŸ› οΈ Step 1: Update Django Email Settings (Gmail SMTP)

In your Django project’s settings.py, add the following:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your_gmail@gmail.com'  # Your Gmail address
EMAIL_HOST_PASSWORD = 'your_app_password'  # Use an App Password, not your actual password!
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

βœ… Important: Do not use your regular Gmail password. You must create an App Password after enabling 2FA on your Google account.

πŸ” Step 2: Enable App Password on Gmail

  1. Visit your Google Account → Security.
  2. Enable 2-Step Verification.
  3. Scroll to “App passwords.”
  4. Choose "Mail" and "Other", enter "Django App".
  5. Generate the password and copy it.
  6. Paste it into your EMAIL_HOST_PASSWORD field in settings.py.

πŸ”’ Tip: Never hardcode credentials in your codebase. Use django-environ or .env files to store sensitive values.

πŸ“¨ Step 3: Sending Email in Django

Django provides a simple function for sending emails:

from django.core.mail import send_mail

send_mail(
    subject='Welcome to My Site!',
    message='Thanks for signing up.',
    from_email='your_gmail@gmail.com',
    recipient_list=['user@example.com'],
    fail_silently=False,
)

πŸ“ Step 4: Sending HTML Emails

You can also send formatted HTML emails using EmailMessage or EmailMultiAlternatives:

from django.core.mail import EmailMultiAlternatives

subject = 'Welcome!'
text = 'This is a plain text message.'
html = '<h1>Welcome!</h1><p>Thanks for joining our platform.</p>'

email = EmailMultiAlternatives(subject, text, EMAIL_HOST_USER, ['user@example.com'])
email.attach_alternative(html, "text/html")
email.send()

πŸ§ͺ Step 5: Test It Out

You can trigger this from your view or Django shell:

python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('Hello', 'This is a test email.', 'your_gmail@gmail.com', ['test@example.com'])

πŸ› οΈ If it doesn’t work: check your Gmail account’s “Recent Activity” to confirm if something was blocked.

🧰 Tips for Production-Ready Email Configuration

βœ… Use a Transactional Email Provider

Instead of Gmail (which is rate-limited and can block outgoing traffic), use:

  • SendGrid
  • Mailgun
  • Amazon SES
  • Postmark

Each provider offers SMTP settings similar to Gmail.

πŸ” Secure Email Credentials

Use environment variables or the django-environ package:

import environ

env = environ.Env()
EMAIL_HOST_USER = env('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD')

πŸ•΅οΈ‍♂️ Fail-Safe Sending

Use fail_silently=True only when you’re sure email failure isn’t critical (e.g., logging):

send_mail(..., fail_silently=True)

πŸ§ͺ Test With Console Backend First

During local development, switch to:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

This prints the email in your terminal instead of sending it.

βœ… Conclusion

Using Gmail to send emails from Django is perfect for development and light usage. But as your app scales, switch to a transactional email service and use environment variables for security. Django makes the process simple with its built-in email utilities and backend flexibility.


Django-tutorial.dev is dedicated to providing beginner-friendly tutorials on Django development. Examples are simplified to enhance readability and ease of learning. Tutorials, references, and examples are continuously reviewed to ensure accuracy, but we cannot guarantee complete correctness of all content. By using Django-tutorial.dev, you agree to have read and accepted our terms of use , cookie policy and privacy policy.

Β© 2025 Django-tutorial.dev .All Rights Reserved.
Django-tutorial.dev is styled using Bootstrap 5.
And W3.CSS.
This Platform is not affiliated with or directly endorsed by Django Software Foundation (DSF) or the Django web framework. This Project is solely maintained by nischal lamichhane who happens to be an individual member of the DSF