Haven’t you observed, when you register on some websites, you get a mail from that company or institution? The email would be, verification email or welcome email, account creation successful email or thanks-regard email, etc. For example, when you create a Google account, the first mail you get would be something like, “Hi Xyz, Welcome to Google. Your new account comes with access to Google products, apps, and services…..” Sending these types of emails from your Django application is quite easy.
Although you can refer to the documentation for knowing more about sending emails in Django, but this is remarkably condensed and made easier.
How to send simple emails to the registered users of your Django application
Illustration of Django emails using an example. Consider a project named geeksforgeeks having an app named geeks. Refer this to create Django project and apps. Now let’s demonstrate this in geeksforgeeks project. In your “geeks” app’s settings.py file, enter the following,
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'EMAIL_HOST = 'smtp.gmail.com'EMAIL_USE_TLS = TrueEMAIL_PORT = 587EMAIL_HOST_USER = #sender's email-id EMAIL_HOST_PASSWORD = #password associated with above email-id |

In the above code, EMAIL_HOST_USER = ‘xabc6457@gmail.com’ and EMAIL_HOST_PASSWORD = ‘xyz123abc@’ are the lines where you need to add the sender’s mail id and password. xabc6457@gmail.com and xyz123abc@ are just examples.
Now to use this in our application, move to views.py and add these lines at the top section as below.

from django.conf import settings from django.core.mail import send_mail |
Generally, emails are sent to the users who signup right? So, in the signup view function, add these lines.
subject = 'welcome to GFG world'message = f'Hi {user.username}, thank you for registering in geeksforgeeks.'email_from = settings.EMAIL_HOST_USER recipient_list = [user.email, ] send_mail( subject, message, email_from, recipient_list ) |
Now we will understand what exactly is happening. Here,
subjectrefers to the email subject.messagerefers to the email message, the body of the email.email_fromrefers to the sender’s details.This takes the EMAIL_HOST_USER from settings.py file, where you added those lines of code earlier.recipient_listis the list of recipients to whom the mail has to be sent that is, whoever registers to your application they receive the email.send_mailis an inbuilt Django function that takes subject, message, email_from, and recipient’s list as arguments, this is responsible to send emails.
After these extra lines of code has been added to your project, you can send emails now. But if you are using Gmail, then the first time you make these changes in your project and run, you might get SMTP error.
To correct that-
1-Go to the Google account registered with the sender’s mail address and select Manage your account

2-Go to security section at the left nav and scroll down. In Less secure app access, turn on the access. By default, it would be turned off.

Finally run the application.
Now, register any user to your application, and they will receive mail from the email account you had mentioned.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Connect Django Project to MongoDB using Django
- Adding Tags Using Django-Taggit in Django Project
- Python | Django-allauth setup and Configuration
- Django Sign Up and login with confirmation Email | Python
- Project Idea | Voice Based Email for Visually Challenged
- Project Idea | Innovate Email
- Sending Emails Using API in Flask-Mail
- Automatic Birthday mail sending with Python
- Styling Django Forms with django-crispy-forms
- Django Introduction | Set 2 (Creating a Project)
- Django Project MVT Structure
- How to Create a Basic Project using MVT in Django ?
- Voting System Project Using Django Framework
- Django project to create a Comments System
- Top 10 Reasons to Choose Django Framework For Your Project
- How to add Site Header, Site Title, Index Title in a Django Project?
- How to add RSS Feed and Sitemap to Django project?
- Adding WYSIWYG editor to Django Project
- How to add AMP to Django Project?
- Adding CSP headers in Django Project
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

