Sending emails from a Django server is a crucial aspect of many web applications. It allows you to communicate with users, send notifications, and perform various other tasks. In this guide, I'll walk you through the process of setting up email functionality in your Django project using SMTP and specifically, using smtp.gmail.com
.
To a Master like me, SMTP stands for Send Mail To Peasants. But to a peasant like you, SMTP should stand for Simple Mail Transfer Protocol. It is the standard Protocol followed by emailing systems. You will be using Gmail's SMTP server to send the mail as it doesn't require your project to be hosted.
Setting up Django for Email
1. Install Django & decouple
If you haven't already, install Django and decouple using the following commands:
pip install Django
pip install python-decouple
2. Setting up Variables
You will need an App Password for your gmail account to use gmail's smtp. For this:
Goto your Google Accounts Security settings here
Turn on 2 Factor Authentication (2FA)
Goto App Passwords and create an app password
Copy the Password and securely store it somewhere.
After You have the App password, create an .env file that will have your environment variables for development stage. I have explained what this does in this Blog. This is what .env file should look like:
EMAIL_HOST_USER='django@master.com'
EMAIL_HOST_PASSWORD'xxxx xxxx xxxx xxxx'
3. Configure Email Settings
In your Django project settings (settings.py
), find the EMAIL_BACKEND
setting and set it to 'django.core.mail.backends.smtp.EmailBackend'
. Also, configure the SMTP settings for Gmail:
# settings.py
from decouple import config
# Email settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = config('EMAIL_HOST_USER') # Your Gmail email address
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD') # Your Gmail email password or app password
Values in EMAIL_HOST_USER and EMAIL_HOST_PASSWORD will be replaced by decouple with your actual Gmail credentials from '.env' file. Note: It's recommended to use an app password for better security.
Writing the Django View
Now, let's create a simple Django view that sends an email when accessed.
1. Import Required Modules
In your views.py
file, import the necessary modules:
# views.py
from django.core.mail import send_mail
from django.http import HttpResponse
2. Create the Email-Sending View
Add a function in your views.py
to handle email sending:
# views.py
from decouple import config
from django.core.mail import send_mail
def send_email_view(request):
to_email = request.user.email
subject = 'Hello from Your Django App'
message = 'This is a test email sent from your Django app.'
from_email = config('EMAIL_HOST_USER') # Sender's email address
recipient_list = [to_email] # Recipient's email address
send_mail(subject, message, from_email, recipient_list)
return HttpResponse('Email sent successfully!')
You can also research on sending html files as the email message that will render on the receiver's end to prettify your emails.
URLs and Testing
Finally, configure a URL for your new view in your urls.py
:
# urls.py
from django.urls import path
from .views import send_email_view
urlpatterns = [
path('send_email/', send_email_view, name='send_email'),
]
Now, run your Django development server and navigate to http://localhost:8000/send_email/
to trigger the email-sending view. Check the console for any errors, and if everything is set up correctly, you should see the 'Email sent successfully!' message.
Conclusion
Congratulations! You've successfully set up email functionality in your Django project using Gmail's SMTP server. This is a basic example, and you can expand upon it by customizing the email content, handling attachments, or incorporating email templates.
Additionally, you may want to explore Django's built-in support for sending HTML emails and managing email templates for a more professional and customizable approach. Suffer Coding!