{"id":8031,"date":"2020-08-25T19:13:50","date_gmt":"2020-08-25T19:13:50","guid":{"rendered":"https:\/\/www.askpython.com\/?p=8031"},"modified":"2023-01-29T09:28:47","modified_gmt":"2023-01-29T09:28:47","slug":"send-emails-using-django","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/django\/send-emails-using-django","title":{"rendered":"Send Emails Using Django"},"content":{"rendered":"\n<p>In this article, we will learn to send emails using Django to users automatically via the Django mail library, an extension of the <strong>smtplib<\/strong> module of Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is SMTP?<\/strong><\/h2>\n\n\n\n<p>SMTP server stands for <strong>Simple Mail Transfer Protocol<\/strong>, which is a community protocol for electronic mail transmission. SMTP consists of a set of community guidelines that allows, softwares to transmit Mail over the internet. It is a program that sends mail to other users using email addresses.<\/p>\n\n\n\n<p><strong>SMTP server<\/strong>: is the application that sends\/receives or relays outgoing mails from one client to another.<\/p>\n\n\n\n<p>For Example, Google\u2019s SMTP server address is <strong>smtp.gmail.com. <\/strong>Similarly, Apple&#8217;s SMT server is <strong>smtp.apple.com, <\/strong>etc. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Send Emails using Django?<\/strong><\/h2>\n\n\n\n<p>Now that we know about the SMTP server is and how emails are generated, let us now make an app to <a href=\"https:\/\/mailtrap.io\/blog\/python-send-email\/\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">send emails using Python<\/a> Django.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Additions to Settings.py<\/h3>\n\n\n\n<p>In the settings.py file, we need to add the following email <a href=\"https:\/\/www.askpython.com\/python\/dictionary\/python-dictionary-dict-tutorial\" class=\"rank-math-link\">dictionary variable<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEMAILS = &#x5B;\n    {\n        EMAIL_BACKEND = &#039;django.core.mail.backends.smtp.EmailBackend&#039;,\n        EMAIL_HOST = &#039;smtp.gmail.com&#039;,\n        EMAIL_PORT = 8000,\n        EMAIL_HOST_USER = &#039;xyz@gmail.com&#039;,\n        EMAIL_HOST_PASSWORD = &#039;xyz&#039;,\n        EMAIL_USE_TLS = True,\n        EMAIL_USE_SSL = False,\n        }\n]\n<\/pre><\/div>\n\n\n<p>Make sure you change the SMTP_HOST with the SMTP server from your provider. Also, change the email and the password with your credentials.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Difference between TLS and SSL<\/h3>\n\n\n\n<p>The functioning of both <strong>TLS <\/strong>and<strong> SSL<\/strong> is the same; they are used to encrypt data and information between servers\/clients or systems. <strong>TLS (Transport Layer Security) <\/strong>is the successor protocol to <strong>SSL(Secure Sockets Layer)<\/strong>. <\/p>\n\n\n\n<p><strong>TLS<\/strong> or <strong>SSL<\/strong> depends on the server you are using, in the case of Gmail, it is <strong>TLS<\/strong>. <\/p>\n\n\n\n<p>In this article, I will be working with Gmail itself and hence will select<strong> TLS. <\/strong>You could use anything of the two based on your server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Writing Code to send Emails<\/strong><\/h2>\n\n\n\n<p>Now that we have our settings in place, let\u2019s write the code to send emails.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Send emails individually using send_mail()<\/h3>\n\n\n\n<p>This section of the tutorial talks about the ways to send individual emails to your recepients. We&#8217;ll make this happen with the send_mail() function. The syntax for send_mail is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsend_mail(\n    subject = &#039;This is Subject Message&#039;,\n    message = &#039;This is email Message&#039;,\n    from_email = &#039;Sender Email ID&#039;,\n    recipient_list = &#039;Recipient&#039;s Email&#039;,\n    fail_silently = False,\n    html_messages = &#039;HTML messages&#039;,\n)\n<\/pre><\/div>\n\n\n<p>In Views.py, add the code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom django.core.mail import send_mail\n\nsend_mail(\n    subject = &#039;Test Mail&#039;,\n    message = &#039;Kindly Ignore&#039;,\n    from_email = &#039;noreply@gmail.com&#039;,\n    recipient_list = &#x5B;&#039;recepient.email@gmail.com&#039;,],\n    fail_silently = False,\n)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Sending Multiple Emails using send_mass_mail()<\/h3>\n\n\n\n<p>In this section, we&#8217;ll go over the steps to send out bulk emails. We&#8217;ll use the send_mass_mail() method here. The syntax for <strong>send_mass_mails<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsend_mass_mail(\n    (datatuple),\n    fail_silently = False,\n)\n<\/pre><\/div>\n\n\n<p>Here the datatuple is the <a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" class=\"rank-math-link\">tuple<\/a> containing the information about individual emails.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmessage1 = (subject, message, from_email, recipient_list)\nmessage2 = (subject, message, from_email, recipient_list)\nmessage3 = (subject, message, from_email, recipient_list)\n\nsend_mass_mail((message1,message2,message),fail_silently =False)\n<\/pre><\/div>\n\n\n<p>In views.py the code will look like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom django.core.mail import send_mail\n\nmessage1 = (&#039;Subject Here&#039;, &#039;This is Message&#039;,&#039;noreply@gmail.com&#039;,&#x5B;&#039;s.ask1@gmail.com&#039;,&#039;s.ask2@gmail.com&#039;])\nmessage2 = (&#039;Subject Here&#039;, &#039;This is Message&#039;,&#039;noreply@gmail.com&#039;,&#x5B;&#039;s.ask1@gmail.com&#039;,&#039;s.ask2@gmail.com&#039;])\n\n\nsend_mass_mail(\n    (message1,message2),\n    fail_silently = False,\n)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Send Emails using Django Email<\/strong>Message() Method<\/h3>\n\n\n\n<p>This method is used to send advanced mails, having features like BCC, CC, or attachments. This Django Method is handled by Email Backend.<\/p>\n\n\n\n<p>The Email Backend class requires three steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>connection.open<\/strong>(): Ensures a long-term connection for sending mails. <\/li><li><strong>connection.close()<\/strong>: Stops the established connection<\/li><li><strong>send_message<\/strong>():<strong> <\/strong>Sends the mails; if the connection was not already open, then it temporarily opens the connection to send the mail.<\/li><\/ol>\n\n\n\n<p>The placeholder syntax is :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nemail1 = EmailMessage(\n    subject = &#039;This is subject&#039;,\n    message = &#039;This is message&#039;,\n    from_email = &#039;from@gmail.com&#039;,\n    to = &#x5B;&#039;mail1@gmail.com&#039;,],\n    bcc = &#x5B;&#039;mail2@gmail.com&#039;],\n    cc = &#x5B;&#039;mail3@gmail.com&#039;],\n)\n<\/pre><\/div>\n\n\n<p>Therefore add the following code in view.py:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom django.core import mail\nconnection = mail.get.connection()\n\nconnection.open()\n\nemail = mail.EmailMessage(\n    subject = &#039;Test Mail&#039;,\n    message = &#039;Kindly Ignore&#039;,\n    from_email = &#039;noreply@gmail.com&#039;,\n    to = &#x5B;&#039;vn.ask1@gmail.com&#039;,],\n    bcc = &#x5B;&#039;vn.ask2@gmail.com&#039;],\n)\n\nconnection.send_messages(email)\nconnection.close()\n<\/pre><\/div>\n\n\n<p><strong>File Attachment<\/strong>:<\/p>\n\n\n\n<p>EmailMessages() method provides .<strong>attach_file(&#8216;path to the file&#8217;) <\/strong>method to send attachments along with your email message. You can add the following code to attach the file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nemail1.attach_file(&#039;home\/Desktop\/books.jpg&#039;)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Implementing the Code<\/h2>\n\n\n\n<p>Now that we&#8217;ve discussed the individual sections of code let&#8217;s put them all together and figure out how it works. Therefore add a sample script (combining all the codes from the above section) into your <strong>views.py<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom django.core.mail import send_mail\n\n#Sending mails individualy\nsend_mail(\n    subject = &#039;Test Send Mail #1&#039;,\n    message = &#039;Kindly Ignore&#039;,\n    from_email = &#039;noreply@gmail.com&#039;,\n    recipient_list = &#x5B;&#039;recepient.email@gmail.com&#039;,],\n    fail_silently = False,\n)\n\n#Sending mass mails\nmessage1 = (&#039;Test Subject message1 #2&#039;, &#039;This is Message&#039;,&#039;noreply@gmail.com&#039;,&#x5B;&#039;s.ask1@gmail.com&#039;,&#039;s.ask2@gmail.com&#039;])\nmessage2 = (&#039;Test Subject message2 #2&#039;, &#039;This is Message&#039;,&#039;noreply@gmail.com&#039;,&#x5B;&#039;s.ask1@gmail.com&#039;,&#039;s.ask2@gmail.com&#039;])\n\nsend_mass_mail(\n    (message1,message2),\n    fail_silently = False,\n)\n\n#Sending mails using EmailMessage along with attachments\nfrom django.core import mail\nconnection = mail.get.connection()\n\nconnection.open()\nemail = mail.EmailMessage(\n    subject = &#039;Test Mail&#039;,\n    message = &#039;Kindly Ignore&#039;,\n    from_email = &#039;noreply@gmail.com&#039;,\n    to = &#x5B;&#039;vn.ask1@gmail.com&#039;,],\n    bcc = &#x5B;&#039;vn.ask2@gmail.com&#039;],\n)\nemail.attach_file(&#039;home\/Desktop\/books.jpg&#039;)\nconnection.send_messages(email)\nconnection.close()\n<\/pre><\/div>\n\n\n<p>Note the first time you use Gmail to send emails; you might get an SMTP  error due to the default security settings.<\/p>\n\n\n\n<p>For that, go to <strong>manage your account <\/strong>in your sender gmail account<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"920\" height=\"561\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/manage-you-account-edited.png\" alt=\"Manage You Account Edited\" class=\"wp-image-8103\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/manage-you-account-edited.png 920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/manage-you-account-edited-300x183.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/manage-you-account-edited-768x468.png 768w\" sizes=\"auto, (max-width: 920px) 100vw, 920px\" \/><figcaption>Manage You Account Edited<\/figcaption><\/figure><\/div>\n\n\n\n<p>Then go to <strong>Security<\/strong> present on the top panel and turn on <strong>Less Secure access.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"924\" height=\"682\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Less-secure-your-account.png\" alt=\"Less Secure Your Account\" class=\"wp-image-8038\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Less-secure-your-account.png 924w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Less-secure-your-account-300x221.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Less-secure-your-account-768x567.png 768w\" sizes=\"auto, (max-width: 924px) 100vw, 924px\" \/><figcaption>Less Secure Your Account<\/figcaption><\/figure><\/div>\n\n\n\n<p>That&#8217;s it, now try and run your code! It will start sending emails. Run the server in the terminal:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\npython manage.py runserver\n<\/pre><\/div>\n\n\n<p>That&#8217;s it, All the emails are sent now! The emails will look like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"546\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Email.jpg\" alt=\"Email\" class=\"wp-image-8107\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Email.jpg 903w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Email-300x181.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Email-768x464.jpg 768w\" sizes=\"auto, (max-width: 903px) 100vw, 903px\" \/><figcaption>Email<\/figcaption><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"546\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Attachment-Email.jpg\" alt=\"Attachment Email\" class=\"wp-image-8108\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Attachment-Email.jpg 903w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Attachment-Email-300x181.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Attachment-Email-768x464.jpg 768w\" sizes=\"auto, (max-width: 903px) 100vw, 903px\" \/><figcaption>Attachment Email<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion <\/strong><\/h2>\n\n\n\n<p>That\u2019s it, Coders! We can now successfully send emails from our Web Application using Django.<\/p>\n\n\n\n<p>You can try implementing the above codes as practice. See you guys in the next article !! Keep practicing!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn to send emails using Django to users automatically via the Django mail library, an extension of the smtplib module of Python. What is SMTP? SMTP server stands for Simple Mail Transfer Protocol, which is a community protocol for electronic mail transmission. SMTP consists of a set of community guidelines [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":8035,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-8031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=8031"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8031\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/8035"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=8031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=8031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=8031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}