Send Emails in Django using Gmail SMTP | A Step-by-Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I am a full stack developer and a top Credit Plus freelancer at upwork.com [Music] okay so we can Implement email notifications for when an order was placed whenever an order was created the user who placed the order would receive an email with the details of the order okay so the email notification we can create using Django built-in email functionality okay and it would be triggered by creation of an order so now let's Implement how we can integrate email notification in Django okay first you have to go to your setting dot Pi file okay you have to add some configuration for email notifications first we have to add email backend okay we will use Django building Django dot core Dot mail Dot backends dot SMTP because we are going to use SMTP dot email backend then we have to add email host we are going to use Gmail host okay so we will use smtp.gmail.com if you are using Yahoo or any other service provider so you can replace with Gmail okay so I'm going to use Gmail host in this video okay so next I have to add email port okay for SMTP we have to add email Port 587 okay and then we have to add email host user okay so here you can add your host user like if I use my email which is code with move at the rate gmail.com okay from this email I will send a notifications to the user when order is placed okay then we have to use email host password okay and here we can Define the password okay and next we have to declare email use email use it can be TLS okay it should be to do and then email use SSL we can make it false okay so these configurations you have to First add in your setting dot by file okay you can easily create a new Gmail account first okay then you have to enable uh two Factor authentication okay for your email okay then you have to get the password let me show you how you can get the email host password okay when you create a new Gmail account okay so you have to go to account first then you have to go to the security okay here if you have already enabled two-step verification okay so you can skip it if two-step verification is not enabled you can enable first okay then you have to go to the app passwords then you have to create a new app I will go with custom name I will give name Django backend course okay and it will generate a password for me okay so I will copy my password okay and then I will just go to my Django setting dot Pi file and I will paste this password Here okay so I will delete this password so you can also delete the password after testing okay for security reasons uh so you can see we are almost set up with our configuration of email notification okay now we have to implement email notification in view start Pi file now let's just go to your online shop app and go to view start Pi file okay here you have to go to post method this is our post method okay because in this post method we will Implement email notification okay so before we implement we need to import some packages first you have to import from Django dot code dot mail import send mail okay then you have to import another package which is from Django dot code dot mail import email message next we have to import email host user from our setting dot Pi file so we can import from backend dot settings okay actually this is our folder name which is backend okay and this is the settings file okay from this file we will import email host user okay okay so we have to import these two things okay only okay now let's move to the post method okay this is the post method so here we need to add few lines so when new order placed user should get email okay I'll write so now let's implement okay before serializer dot save you have to implement send mail first we have to enter subject okay and then we have to enter message then we have to enter email host user okay from where the email will be sent then we have the recipient list like the persons those will receive email and then we have to add fail silently to to do okay in case email is not sent okay so we have subject message email host user recipient list fails silently okay so these are the some important configurations okay then we have to know declare the subject okay we can declare subject here I can say New Order is placed okay so we have subject no I can declare message okay it's up to me what kind of message I want to send I want to send dear customer dear customer okay so I want to add the customer name so I can concatenate with string okay here I can say the customer name it should be customer name okay and then I can add another string your order is placed no thanks for your order okay so so this is the message if I show you in details so you can see this is the message and subject okay know what we can do here we can add recipient list as well okay so for recipient list we can just I will say we will have email okay so okay but we need to Define this email I can say we have a customer email when customer place order okay so we can easily get email from customer emails okay so we are now done with our configuration of email notification okay so subject message email host user okay so everything is configured no properly okay so before we test it we need to set up the postman okay and then we will test this email notification as well using Postman Postman is a powerful API testing tool that makes it easy for a developer to trust debug and document apis it provides a user-friendly interface that makes it easy to send HTTP requests and analyze the response with Postman developers can quickly check functionality of their apis identify errors and troubleshoot issues so let's install Postman Postman can be installed on Windows Mac and Linux to install Postman you have to visit Postman official website postman.com okay so on this page you have to download the desktop application as per your operating system so I am using Mac so I will download Mac operating system file Mac Intel shape okay so the file is started downloading so it will take some time to download Postman file okay once the file is downloaded so you can open this file to start installation process so you have to follow on screen instructions to complete the installation when installation is done you have to just launch Postman okay you can just click on Postman icon to launch Postman application move to the application folder if you are using Mac okay when Postman is launched create a new collection give name collection to your apis I will give name backend course okay so collection is created no we can add a new request okay Postman Sports several requests it can be a get post put or delete so you can choose the appropriate method based on the API you are testing okay so first we will test get request okay so we can also give name to our API for better documentation I will give name get orders okay I will just copy my localhost application path okay and I will paste it here and I have to enter the API path Okay so then you have to just click on send button okay here you can see the response so we can see the response data okay so if we want to change the method imagine we have a post okay let's we want to uh add some data in our database okay I will just go to the body and I will go to the raw okay and I will choose the Json format here okay so I know I will post some data here okay so here we have some Fields customer name okay and I will say customer name code with move okay and then I will say customer email okay imagine we have a customer email okay eight two at the rate gmail.com okay and next we have make sure the fields are correct okay next we have quantity field okay so we can give quantity two okay and then we have a field which is a product okay we have products registered in our admin panel okay so we have one product okay so we can give the ID so it is only one so we can give ID of the product okay so if there's a multiple products so you can replace with the ID okay so we have one so I will just give the ID of the product okay so all is fine no let's we need to just send a request request is being sended okay so our request successfully created okay so you can see we have this status code 201 it means request is created okay new order is created in our database okay and also the email is sent now let's go to our admin panel and we can see the order okay so you can see we have added the customer email new uh entry in our database is easily created so we can use apis to post data in our database now let me also show you if we have received email or not let me open my email account okay so I have not received email yet let me confirm my request okay okay the email is not correct I have entered wrong gmails okay so no email is correct now let me try code with me 22 okay so let me try now okay so order is placed no let me check on my inbox wow you can see New Order is placed I have received the email their customer code with move okay your order is placed no thanks for your order okay so we are now receiving the emails as well if you have not subscribed my channel you can subscribe now it helps to create more courses and tutorials for free so let's get started I hope this video has been helpful don't forget to like share and subscribe my YouTube channel if you have any questions or feedback leave a comment below I will get back to you stay tuned for more courses and tutorials and until next time goodbye
Info
Channel: CodeWithMuh
Views: 9,654
Rating: undefined out of 5
Keywords: codewithmuh, code with muh, email, smtp, gmail, django, python, Send Emails in Django using Gmail SMTP, email in django, send emails using python, send email using django, django rest framework
Id: Mezha1p_dTE
Channel Id: undefined
Length: 18min 18sec (1098 seconds)
Published: Wed Mar 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.