

{"id":62129,"date":"2019-09-05T11:31:20","date_gmt":"2019-09-05T06:01:20","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=62129"},"modified":"2021-06-21T12:19:22","modified_gmt":"2021-06-21T06:49:22","slug":"django-send-email","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/django-send-email\/","title":{"rendered":"How to Send Emails in Django &#8211; Elite Guidelines for Beginners"},"content":{"rendered":"<p><strong>Django Emails Tutorial &#8211;\u00a0<\/strong>We all have used the email feature of websites, whenever we forget our password or subscribe to any website, etc. Every time an email is sent to our inbox by the website including the link to reset the password.<\/p>\n<p>Sending emails via websites is a great feature to have as it enhances the User Experience. We can achieve greater user communication. See, amazon.com, flipkart.com which send emails with their personalized offers. It&#8217;s all about emails generated from a server.<\/p>\n<p>So, now let\u2019s learn how to add emails to our website and send emails in Django.<\/p>\n<h3>Settings for Sending\u00a0Emails in Django<\/h3>\n<p>The value changes according to the type of email services. These settings are to be added in the <strong>settings.py<\/strong> file of the project.<\/p>\n<p>These are the settings for sending email in Django via Gmail.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#DataFlair\r\nEMAIL_BACKEND =\r\n\r\n\u2018django.core.mail.backends.smtp.EmailBackend\u2019\r\nEMAIL_HOST = \u2018smtp.gmail.com\u2019\r\nEMAIL_USE_TLS = True\r\nEMAIL_PORT = 587\r\nEMAIL_HOST_USER = \u2018your_account@gmail.com\u2019\r\nEMAIL_HOST_PASSWORD = \u2018your account\u2019s password\u2019<\/pre>\n<p><b>Code Display:<\/b><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-Settings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68257\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-Settings.png\" alt=\"Email Settings - Django Emails\" width=\"1084\" height=\"422\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-Settings.png 1084w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-Settings-150x58.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-Settings-300x117.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-Settings-768x299.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-Settings-1024x399.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-Settings-520x202.png 520w\" sizes=\"auto, (max-width: 1084px) 100vw, 1084px\" \/><\/a><\/p>\n<p><strong>Understanding the code:<\/strong><\/p>\n<p>The settings are pretty straight forward as:<\/p>\n<ul>\n<li>\n<h4><strong>EMAIL_BACKEND<\/strong><\/h4>\n<\/li>\n<\/ul>\n<p>This setting specifies the backend that we are going to use for sending an email in Django. There are multiple backends that Django provides.<\/p>\n<ul>\n<li>\n<h4><strong>EMAIL_HOST<\/strong><\/h4>\n<\/li>\n<\/ul>\n<p>This setting is to specify your email service provider. We are using the setting for Gmail.<\/p>\n<ul>\n<li>\n<h4><strong>EMAIL_HOST_USER<\/strong><\/h4>\n<\/li>\n<\/ul>\n<p>It is the account name from which we will send an email.<\/p>\n<ul>\n<li>\n<h4><strong>EMAIL_HOST_PASSWORD<\/strong><\/h4>\n<\/li>\n<\/ul>\n<p>The password of the email account that we are using.<\/p>\n<ul>\n<li>\n<h4><strong>EMAIL_PORT<\/strong><\/h4>\n<\/li>\n<\/ul>\n<p>This is the setting for Gmail, you can get yours on the web. It is the port used by the SMTP server.<\/p>\n<ul>\n<li><strong>EMAIL_USE_TLS<\/strong><\/li>\n<\/ul>\n<p>This setting specifies whether the Email uses a TLS connection or not. It is <strong>True<\/strong> for Gmail.<\/p>\n<ul>\n<li>\n<h4><strong>EMAIL_USE_SSL<\/strong><\/h4>\n<\/li>\n<\/ul>\n<p>False for Gmail.<\/p>\n<p>There are some more settings for email but for sending an email with Gmail, this much setup is enough.<\/p>\n<h3>Setting up Gmail for Django Mail API<\/h3>\n<p>We need to make some changes in our Gmail account to send an Email. Visit this link with your account signed in:<\/p>\n<p><a href=\"https:\/\/myaccount.google.com\/security?pli=1\">Gmail Account Settings<\/a><\/p>\n<p>The link contains a specific setting that allows access via Django mail API. Since our web application is not a google registered service, it is a less secured app. Therefore, we allow less secured apps to our settings.<\/p>\n<p>Then scroll down to this section of the page.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Gmail-Security-Section.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68258\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Gmail-Security-Section.png\" alt=\"Gmail Security Section - Django Emails\" width=\"1600\" height=\"786\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Gmail-Security-Section.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Gmail-Security-Section-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Gmail-Security-Section-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Gmail-Security-Section-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Gmail-Security-Section-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Gmail-Security-Section-520x255.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p><strong>Less secure app access<\/strong> option will be present. Turn on the access.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Less-Secure-App-Access-Option.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68260\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Less-Secure-App-Access-Option.png\" alt=\"Less Secure App Access Option - Django Emails\" width=\"1600\" height=\"786\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Less-Secure-App-Access-Option.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Less-Secure-App-Access-Option-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Less-Secure-App-Access-Option-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Less-Secure-App-Access-Option-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Less-Secure-App-Access-Option-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Less-Secure-App-Access-Option-520x255.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p style=\"text-align: left;\">========================================================<br \/>\n<strong>Note:<\/strong><\/p>\n<p style=\"text-align: left;\">If you are just testing, then you can temporarily do this setting. For a permanent setup, I would recommend a new account. Since, using your main account will be risky.<br \/>\n========================================================<br \/>\nNow, we are ready to send emails with Django.<\/p>\n<h3 style=\"text-align: left;\">Sending Email with Gmail<\/h3>\n<p>We will be creating a subscribe page for our website. It will do these tasks:<\/p>\n<ul>\n<li>Take user email from the form.<\/li>\n<li>Send a welcome email to the user.<\/li>\n<\/ul>\n<h3>Making Subscribe Button in Django<\/h3>\n<h4>1. Create a New Application<\/h4>\n<p>Open your terminal\/ PowerShell and make a new Django application named subscribe.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py start-app subscribe<\/pre>\n<p>This command will add a new directory in your project.<\/p>\n<p>Install the application in settings.py file.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Installing-Application.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68261\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Installing-Application.png\" alt=\"Installing Application - Django Emails\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Installing-Application.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Installing-Application-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Installing-Application-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Installing-Application-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Installing-Application-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Installing-Application-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>We have successfully done the above steps.<\/p>\n<h4>2. Create Forms<\/h4>\n<p>Make a new file<strong> forms.py<\/strong> in the subscribe directory. Paste this code in it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from Django import forms\r\n\r\nclass Subscribe(forms.Form):\r\n    Email = forms.EmailField()\r\n\r\n    def __str__(self):\r\n        return self.Email<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/forms-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68262\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/forms-py.png\" alt=\"forms.py - Django Emails\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/forms-py.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/forms-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/forms-py-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/forms-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/forms-py-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/forms-py-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>We have made a form with a single field which is an Email field. The<strong> __str__<\/strong> method is optional. When the subscribe class is returned as a string, it will return its Email field.<\/p>\n<h4>3. Create Views<\/h4>\n<p>Paste this code in the <strong>subscribe\/views.py<\/strong> file.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.shortcuts import render\r\nfrom myproject.settings import EMAIL_HOST_USER\r\nfrom . import forms\r\nfrom django.core.mail import send_mail\r\n\r\n# Create your views here.\r\n#DataFlair #Send Email\r\ndef subscribe(request):\r\n    sub = forms.Subscribe()\r\n\u00a0 \u00a0 if request.method == 'POST':\r\n        sub = forms.Subscribe(request.POST)\r\n\u00a0 \u00a0 \u00a0 \u00a0 subject = 'Welcome to DataFlair'\r\n\u00a0 \u00a0 \u00a0 \u00a0 message = 'Hope you are enjoying your Django Tutorials'\r\n\u00a0 \u00a0 \u00a0 \u00a0 recepient = str(sub['Email'].value())\r\n\u00a0 \u00a0 \u00a0 \u00a0 send_mail(subject, \r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 message, EMAIL_HOST_USER, [recepient], fail_silently = False)\r\n\u00a0 \u00a0 \u00a0 \u00a0 return render(request, 'subscribe\/success.html', {'recepient': recepient})\r\n    return render(request, 'subscribe\/index.html', {'form':sub})<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/views-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68263\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/views-py.png\" alt=\"views.py - Django Emails\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/views-py.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/views-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/views-py-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/views-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/views-py-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/views-py-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<h3><strong>Understanding the code:<\/strong><\/h3>\n<p>This code is pretty straightforward. We have created a simple view function <strong>subscribe<\/strong>.<\/p>\n<p>Note that we have imported two new things other than form:<\/p>\n<p>The <strong>EMAIL_HOST_USER<\/strong> and <strong>send_mail()<\/strong> method.<\/p>\n<p>We defined <strong>EMAIL_HOST_USER<\/strong> in <strong>settings.py.<\/strong>\u00a0We are importing that value in the view function. Although sender email can be specified here, this approach saves us from errors.<\/p>\n<p>Then inside <strong>subscribe<\/strong>\u00a0its simple form display.<\/p>\n<p>A<strong> sub<\/strong> is a form object. We are checking whether the data received is GET or POST. If the method is GET, it will give an empty form, otherwise, it will execute the code in <strong>if<\/strong> statement.<\/p>\n<p><strong>send_mail() <\/strong>method is imported from <strong>django.core.mail<\/strong> module. The <strong>send_mail()<\/strong> method handles the transmission of Emails. There are some important parameters of <strong>send_mail()<\/strong>, which we have defined here:<\/p>\n<ul>\n<li><strong>subject: <\/strong>Contains the subject part of the Email.<\/li>\n<li><strong>message: <\/strong>Contains the Email body.<\/li>\n<li><strong>from_email:\u00a0<\/strong>It is the sender\u2019s mail address. Remember? we defined it in our settings.py file. This should be the same as defined in settings.py. therefore, we have imported it.<\/li>\n<li><strong>recipient_list:\u00a0<\/strong>It is the receivers email address. Apparently, we are taking this from our Form\u2019s Email field. This field should always be a list of strings.<\/li>\n<li><strong>fail_silently:\u00a0\u00a0<\/strong>It is True by default. We specify it false so that when the mail is not sent, we get an Exception. In this case, it will be\u00a0<strong>smtplib.SMTPException<\/strong>. These are the required fields and can not be empty.<\/li>\n<\/ul>\n<p>If the send_mail() is successful then render() is called. It will send the data to a <strong>success.html<\/strong> template else the request is not POST. It will directly render <strong>index.html<\/strong>\u00a0and will contain the form.<\/p>\n<h4>4. Create Templates<\/h4>\n<p>Now, we will create templates. Make a new directory named <strong>templates<\/strong> in the <strong>subscribe directory<\/strong>. Then inside templates, make a new directory <strong>subscribe<\/strong>.<\/p>\n<p>Inside this directory: <strong>subscribe\/templates\/subscribe<\/strong><\/p>\n<p>Make two files: <strong>index.html<\/strong> and <strong>success.html<\/strong><\/p>\n<p>Paste this code in <strong>index.html<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;title&gt;DataFlair send email&lt;\/title&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;link rel=\"stylesheet\"\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.7\/css\/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz\/K68vbdEjh4u\" crossorigin=\"anonymous\"&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;center&gt;\r\n        &lt;h1 class = 'alert alert-success'&gt;Subscribe Form&lt;\/h1&gt;\r\n        &lt;h3 class=\"p-3 mb-2 bg-primary text-white\" style = 'font-size: 50px;'&gt;DataFlair Django Tutorials\r\n    &lt;\/h3&gt;\r\n    &lt;form method=\"POST\"&gt;\r\n        &lt;!-- Very Important csrf Token --&gt;\r\n        {% csrf_token %}\r\n        &lt;div class = \"form-group\"&gt;\r\n            &lt;p&gt;\r\n            &lt;h3&gt;{{ form.as_p }}&lt;\/h3&gt;\r\n        &lt;\/p&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;br&gt;\r\n            &lt;input type=\"submit\" name=\"Subscribe\" class = 'btn btn-primary btn-lg'&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0         &lt;\/div&gt;\r\n\u00a0 \u00a0         &lt;\/form&gt;\r\n        &lt;\/center&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/index-html.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68264\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/index-html.png\" alt=\"index.html - Django Emails\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/index-html.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/index-html-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/index-html-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/index-html-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/index-html-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/index-html-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>It\u2019s a simple template using some bootstrap. You can skip the CSS part if you want. We are just displaying the form in this template.<\/p>\n<p>Paste this code in <strong>success.html<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n\u00a0 \u00a0 &lt;title&gt;Success&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;h1&gt;email sent successfully to: {{ recepient \r\n    }}&lt;\/h1&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/success-html.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68265\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/success-html.png\" alt=\"success html\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/success-html.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/success-html-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/success-html-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/success-html-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/success-html-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/success-html-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>It&#8217;s also a simple template. It will be loaded when email was sent successfully.<\/p>\n<h4>5. Configuring URLs<\/h4>\n<p>Now, we can configure our URLs in Django.<\/p>\n<p>In <strong>urls.py<\/strong> or main urls-config, paste this code inside urlpatterns:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">path('subscribe\/', include('subscribe.urls')),<\/pre>\n<p style=\"text-align: left;\"><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/urls-py-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-68267 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/urls-py-1.png\" alt=\"urls.py\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/urls-py-1.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/urls-py-1-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/urls-py-1-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/urls-py-1-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/urls-py-1-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/urls-py-1-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>Then make a new file<strong> urls.py<\/strong> in<strong> subscribe directory<\/strong>.<\/p>\n<p>Paste this code in subscribe\/urls.py file.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.urls import path\r\nfrom . import views\r\n\r\nurlpatterns = [\r\n    path('', views.subscribe, name = 'subscribe'),\r\n]<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p style=\"text-align: left;\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-urls-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68268\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-urls-py.png\" alt=\"subscribe\/urls.py\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-urls-py.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-urls-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-urls-py-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-urls-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-urls-py-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-urls-py-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>When subscribe is searched from URL-bar, Django will call subscribe function.<\/p>\n<h4>6. Testing<\/h4>\n<p>Start your development server. Now, search for <strong>URL<\/strong>:<em> subscribe\/<\/em> in the browser.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-form.png\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-68269 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-form.png\" alt=\"subscribe form\" width=\"1600\" height=\"786\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-form.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-form-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-form-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-form-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-form-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/subscribe-form-520x255.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>You will get this. Fill in the email field and click on submit.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-field.png\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-68270 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-field.png\" alt=\"Email field\" width=\"1600\" height=\"858\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-field.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-field-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-field-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-field-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-field-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Email-field-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>It is successfully sent.<\/p>\n<p>To be sure, we can check in our sent box.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/mail-list.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68271\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/mail-list.png\" alt=\"Gmail mail list\" width=\"1600\" height=\"786\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/mail-list.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/mail-list-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/mail-list-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/mail-list-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/mail-list-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/mail-list-520x255.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>There the mail was sent successfully. That was how we can send emails via Gmail. Only the settings will change for another email service provider.<\/p>\n<p>Any queries in Django Email tutorial till now? Mention them in the comment section<\/p>\n<h3>Sending Mass Emails in Django<\/h3>\n<p>We used send_mail() in the above example. You can very easily replace the same with the send_mass_mail() method.<\/p>\n<p>There is a difference in sending mass mail though.<\/p>\n<p>Rather than taking parameters like the<strong> subject, message, from_mail, recipient list<\/strong>, it takes in one data tuple parameter.<\/p>\n<p>The data tuple is to be in this format:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">datatuple = (Subject, message, from_email, recipient_list)<\/pre>\n<p>Other functions and parameters work the same way as <strong>send_mail().<\/strong><\/p>\n<p><strong>Send_mass_mail()<\/strong> is a more popular method by choice for sending multiple emails at a time. It is more efficient then send_mail().<\/p>\n<p>When we call <strong>send_mail()<\/strong>, it will always make a new connection for each call. For <strong>send_mass_mail()<\/strong>, the connection once started, will send multiple emails.<\/p>\n<p>It depends on the use cases. As with a subscribe button, we don\u2019t need multiple emails.<\/p>\n<h3>Some Other Methods<\/h3>\n<ul>\n<li>\n<h4>mail_admin()<\/h4>\n<\/li>\n<\/ul>\n<p>It is one of the special features of Django. We can send emails directly to site admins. ADMIN is a list in settings.py. It is a Python list of tuples. The tuples contain the name of the admin and mail of admin.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">ADMIN = [\r\n    (\u2018ADMIN-NAME\u2019, \u2018ADMIN-MAIL\u2019),\r\n]<\/pre>\n<p>This method is used when the site goes online and Django DEBUG is False.<\/p>\n<p>This method requires some settings and lists to be defined.<\/p>\n<ul>\n<li>\n<h4>mail_managers()<\/h4>\n<p>It is the same method as mail_admin(). It sends the mail to managers instead of admin. This requires MANAGER setting, the same as the ADMIN list.<\/li>\n<\/ul>\n<h3>Prevents Header Injection<\/h3>\n<p>Django by default prevents Header Injection in sending mail. When any malicious hacker tries to edit the header list in the mail, Django will raise <strong>BadHeaderError<\/strong>. This exception will temporarily stop mail transmission.<\/p>\n<p>Now, no one will be able to change the header of your Emails. It\u2019s a very important feature. Your emails like reset passwords cannot be hijacked. These emails contain very secure and private data. You have to keep it safe from hackers. This will ensure your user&#8217;s trust.<\/p>\n<h3>EmailMessage Class<\/h3>\n<p>We just learned about <strong>send_mail()<\/strong> and <strong>send_mass_mail()<\/strong> in previous sections. Those are methods of EmailMessage Class. EmailMessage Class provides more advanced features for sending emails. You can send attachments, multi-part emails, etc..<\/p>\n<p>The EmailMessage is responsible for making email itself. It is then the responsibility of Email backend to transmit the same. There are some important parameters of the EmailMessage class.<\/p>\n<p>All of these parameters are optional and can be defined in any order. It is necessary though to define them before calling any<strong> send()<\/strong> method.<\/p>\n<p>These are the parameters or data-members of EmailMessage Class:<\/p>\n<ul>\n<li><strong>subject:<\/strong>\u00a0The subject of the email.<\/li>\n<li><strong>body:<\/strong>\u00a0The body of the email. It is the plain text.<\/li>\n<li><strong>from_email<\/strong>: If omitted, it will go to DEFAULT_FROM_EMAIL defined in settings.py.<\/li>\n<li><strong>to:<\/strong> It is a list of recipient&#8217;s emails.<\/li>\n<li><strong>bcc:<\/strong>\u00a0It is also a list of emails used in the Bcc category.<\/li>\n<li><strong>attachments:<\/strong> A list of attachments to put in a message. This can be a PDF, plain-text file, etc.<\/li>\n<li><strong>cc:<\/strong>\u00a0Contains a list of addresses similar to bcc and recipient list.<\/li>\n<li><strong>reply_to:<\/strong>\u00a0Contains a list of addresses for Reply-to field.<\/li>\n<\/ul>\n<h4>Some Important Methods of\u00a0EmailMessage Class<\/h4>\n<p>Suppose, we have:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Msg = EmailMessage()<\/pre>\n<p>We have defined all the important parameters of <strong>Msg<\/strong>. Now, we will apply these methods one by one.<\/p>\n<ul>\n<li>\n<h5>send()<\/h5>\n<\/li>\n<\/ul>\n<p>This method will send the message.<\/p>\n<p><strong>Syntax :<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Msg.send(failed_silently = False)<\/pre>\n<p>If any connection was specified in the <strong>Msg<\/strong>, <strong>send()<\/strong> will use that. Otherwise, it will go for the default backend connection.<\/p>\n<p>It is important to note that empty recipients list will not raise an exception.<\/p>\n<ul>\n<li>\n<h5>message()<\/h5>\n<\/li>\n<\/ul>\n<p>This method is used to construct a subclass object of django.core.mail. It will create an object of <em>django.core.mail.SafeMIMEText<\/em> which holds the message to be sent.<\/p>\n<ul>\n<li>\n<h5>recipients()<\/h5>\n<\/li>\n<\/ul>\n<p>This method will return a list of recipients. It is as specified in the object.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Msg.recipients()<\/pre>\n<p>It will return all types of recipients, whether they are in cc, bcc, to all the fields.<\/p>\n<ul>\n<li>\n<h5>attach()<\/h5>\n<\/li>\n<\/ul>\n<p>This method is the reason we have to use the EmailMessage Class. When we need any attachments to be sent with the email, this method is called.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Msg.attach( \u2018file_name.type\u2019, content, \u2018MIME\/ type\u2019)<\/pre>\n<p>It will create a new attachment and add the same with the <strong>Msg<\/strong>. You can skip the MIME type parameter. It will then guess the MIME type with the file name.<\/p>\n<p>The content is the information inside the file.<\/p>\n<p>It\u2019s an awesome feature of Django.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>In\u00a0<em>MIMEType = message\/rfc822,\u00a0<\/em>we can attach the EmailMessage class as an object.<\/p>\n<p>If the MIME Type is text\/, the content should be a string.<\/p>\n<p>The binary data is also acceptable in content. It will be decoded using UTF-8. If this fails, then data will be sent unchanged.<\/p>\n<ul>\n<li>\n<h5>attach_file()<\/h5>\n<\/li>\n<\/ul>\n<p>This method is a bit different from the attach() method. It is used to attach files from the file system.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Msg.attach_file(\u2018path to the file\u2019)<\/pre>\n<p>The file stored at that path will be attached to the email.<\/p>\n<p>The binary data is handled similarly as attach().<\/p>\n<h3>Email Backends in Django<\/h3>\n<p>An Email backend is responsible for actually transmitting Email. The Email backend class has three important methods:<\/p>\n<ul>\n<li><strong>open()<\/strong> will start a connection. This connection will be a long one and will not terminate with interruptions.<\/li>\n<li><strong>close()<\/strong> will terminate the connection.<\/li>\n<li><strong>send_message(email_messages)<\/strong> returns a list of EmailMessage objects. This method will open and close connection implicitly.<\/li>\n<\/ul>\n<p>Django provides multiple backends for sending Emails. You can totally make your own Email backends. It&#8217;s so customizable and yet so secure and powerful.<\/p>\n<p>These are some of the default backends provided by Django out of the box.<\/p>\n<p>You can get the backend from the <strong>get_connection()<\/strong> method. This can be imported from<strong> django.core.mail<\/strong> module.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/print-email-backend.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68272\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/print-email-backend.png\" alt=\"print email backend\" width=\"1600\" height=\"843\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/print-email-backend.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/print-email-backend-150x79.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/print-email-backend-300x158.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/print-email-backend-768x405.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/print-email-backend-1024x540.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/print-email-backend-520x274.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>In the above shell, we have printed our Email backend. Django has multiple backends as mentioned above. Only the SMTP backend is mostly used. Other backends are more development-oriented.<\/p>\n<ul>\n<li>\n<h4>SMTP backend<\/h4>\n<\/li>\n<\/ul>\n<p>It is used to transmit Emails on an SMTP connection. We have used this backend to send Emails. We have defined these settings in our settings.py file. It is the default email backend if not specified.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">django.core.mail.backends.smtp.EmailBackend<\/pre>\n<p>The settings we defined in our project in the previous section are for this.<\/p>\n<p>It is the utility backend that is actually used to send Emails.<\/p>\n<ul>\n<li>\n<h4>Console backend<\/h4>\n<\/li>\n<\/ul>\n<p>If you just want to test the Email being sent, this backend comes in handy. As its name, the backend will print the Email you sent. It will not send the Email, but just print it in the console.<\/p>\n<p>Make your settings like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">EMAIL_BACKEND = \u2018django.core.mail.backends.console.EmailBackend\u2019<\/pre>\n<p>This backend is not intended to be used in a production environment.<\/p>\n<ul>\n<li>\n<h4>File backend<\/h4>\n<\/li>\n<\/ul>\n<p>The file backend is similar to the console backend. It is also useful for development purposes.<\/p>\n<p>The file backend writes the Email to a file. Every time a new mail is sent it makes a new file.<\/p>\n<p>To use this backend, paste this code in your settings.py<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'\r\nEMAIL_FILE_PATH = '\/file-directory\/\u2019<\/pre>\n<p>Remember to give it a file path. It will store all its Email files there.<\/p>\n<ul>\n<li>\n<h4>In-memory backend<\/h4>\n<\/li>\n<\/ul>\n<p>This backend is also not for a production environment. This backend stores the messages in the outbox attribute of <strong>django.core.mail<\/strong> module. This attribute is created when the first mail is sent.<\/p>\n<p>Outbox attribute is a list of EmailMessage objects to be sent.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'<\/pre>\n<p>This backend is provided to let developers test and develop Email API more easily.<\/p>\n<ul>\n<li>\n<h4>Dummy backend<\/h4>\n<\/li>\n<\/ul>\n<p>This is the Dummy backend. It will literally do nothing with your messages. You don\u2019t need to specify any other setting other then EMAIL_BACKEND for this.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend'<\/pre>\n<p>As you guessed, it is not for a production environment.<\/p>\n<ul>\n<li>\n<h4>Custom backends<\/h4>\n<\/li>\n<\/ul>\n<p>Django also gives you the power to make your own backends. That does require a more in-depth knowledge of <strong>django.core.email<\/strong>.<\/p>\n<p>It should be noted that Custom Email backends are subclasses of <strong>BaseEmailBackend<\/strong>. It is\u00a0located\u00a0in\u00a0<em>django.core.mail.backends.base<\/em> module. You can take some reference from <em>smtp.EmailBackend<\/em>.<\/p>\n<h3>Summary<\/h3>\n<p>Sending Emails in Django is easy. The best part is that Default backends provided by Django are all secure. They raise proper errors when something is wrong with the header.<\/p>\n<p>Email is a great utility for websites. It comes in handy whether we are subscribing to a website or resetting the password.<\/p>\n<p>Hope you enjoyed this article. Do share your feedback regarding Django Emails article in the comment section.<span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1420,&quot;href&quot;:&quot;https:\\\/\\\/myaccount.google.com\\\/security?pli=1&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;https:\\\/\\\/myaccount.google.com\\\/intro\\\/security?pli=1&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django Emails Tutorial &#8211;\u00a0We all have used the email feature of websites, whenever we forget our password or subscribe to any website, etc. Every time an email is sent to our inbox by the&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":69159,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19149],"tags":[20920,20916,20917,20918,20919],"class_list":["post-62129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","tag-django-email-backends","tag-django-emails","tag-django-mail-api","tag-django-subscribe-button","tag-emailmessage-class"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Send Emails in Django - Elite Guidelines for Beginners - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn to send Emails in Django by understanding its settings, designing subscription button, sending mass Emails, EmailMessage class and Email backends in Django.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/django-send-email\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Send Emails in Django - Elite Guidelines for Beginners - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn to send Emails in Django by understanding its settings, designing subscription button, sending mass Emails, EmailMessage class and Email backends in Django.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/django-send-email\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-05T06:01:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-21T06:49:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-Emails-Tutorial.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Send Emails in Django - Elite Guidelines for Beginners - DataFlair","description":"Learn to send Emails in Django by understanding its settings, designing subscription button, sending mass Emails, EmailMessage class and Email backends in Django.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/django-send-email\/","og_locale":"en_US","og_type":"article","og_title":"How to Send Emails in Django - Elite Guidelines for Beginners - DataFlair","og_description":"Learn to send Emails in Django by understanding its settings, designing subscription button, sending mass Emails, EmailMessage class and Email backends in Django.","og_url":"https:\/\/data-flair.training\/blogs\/django-send-email\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-09-05T06:01:20+00:00","article_modified_time":"2021-06-21T06:49:22+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-Emails-Tutorial.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/django-send-email\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/django-send-email\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"How to Send Emails in Django &#8211; Elite Guidelines for Beginners","datePublished":"2019-09-05T06:01:20+00:00","dateModified":"2021-06-21T06:49:22+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-send-email\/"},"wordCount":2494,"commentCount":13,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-send-email\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-Emails-Tutorial.jpg","keywords":["Django Email Backends","Django Emails","Django Mail API","Django Subscribe Button","EmailMessage Class"],"articleSection":["Django Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/django-send-email\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/django-send-email\/","url":"https:\/\/data-flair.training\/blogs\/django-send-email\/","name":"How to Send Emails in Django - Elite Guidelines for Beginners - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-send-email\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-send-email\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-Emails-Tutorial.jpg","datePublished":"2019-09-05T06:01:20+00:00","dateModified":"2021-06-21T06:49:22+00:00","description":"Learn to send Emails in Django by understanding its settings, designing subscription button, sending mass Emails, EmailMessage class and Email backends in Django.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/django-send-email\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/django-send-email\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/django-send-email\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-Emails-Tutorial.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-Emails-Tutorial.jpg","width":802,"height":420,"caption":"Django Emails Tutorial - How to send Emails in Django"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/django-send-email\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Django Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/django\/"},{"@type":"ListItem","position":3,"name":"How to Send Emails in Django &#8211; Elite Guidelines for Beginners"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/62129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=62129"}],"version-history":[{"count":13,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/62129\/revisions"}],"predecessor-version":[{"id":97686,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/62129\/revisions\/97686"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/69159"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=62129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=62129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=62129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}