π§ 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
- Visit your Google Account → Security.
- Enable 2-Step Verification.
- Scroll to “App passwords.”
- Choose "Mail" and "Other", enter "Django App".
- Generate the password and copy it.
- Paste it into your
EMAIL_HOST_PASSWORD
field insettings.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.