{"id":7321,"date":"2020-07-24T13:25:42","date_gmt":"2020-07-24T13:25:42","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7321"},"modified":"2023-02-16T19:57:02","modified_gmt":"2023-02-16T19:57:02","slug":"sending-emails-using-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/sending-emails-using-python","title":{"rendered":"Python HowTo &#8211; Sending Emails using Python"},"content":{"rendered":"\n<p>Hello everyone! In today&#8217;s article, we&#8217;ll be taking a look at how we can use Python to send emails.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sending Emails &#8211; A brief overview<\/h2>\n\n\n\n<p>Usually, the task of sending emails is done using the MTP (Mail Transfer Protocol). In the present day, there is a separate protocol called SMTP (Simple Mail Transfer Protocol) which is the widely used protocol for sending emails.<\/p>\n\n\n\n<p>This protocol works on a client-server basis, similar to any other. When we want to send an email to a target machine, we (the client) need to send the mail contents to the SMTP Server. The server will now route it to the desired target machine.<\/p>\n\n\n\n<p>So, in order to send an email, you&#8217;ll need to use a SMTP Server. While you may have a custom SMTP Server, we&#8217;ll be using Gmail&#8217;s free SMTP Server to send emails using Gmail!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisite Setup for Sending Emails with Python<\/h2>\n\n\n\n<p>Before going through the rest of this tutorial, I&#8217;d advice you to set up a dummy gmail account that you can use to test sending emails.<\/p>\n\n\n\n<p>After setting up the account, there&#8217;s one more thing you need to do.<\/p>\n\n\n\n<p>By default, your Gmail account is not configured to allow access from less secure applications such as SMTP. We need to enable this access for our account.<\/p>\n\n\n\n<p>You can go to <em><a href=\"https:\/\/myaccount.google.com\/intro\/security\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener nofollow\" class=\"rank-math-link\">your gmail account configuration page<\/a><\/em> and enable access from your Google account.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"841\" height=\"258\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/less_secure_access_gmail.png\" alt=\"Less Secure Access Gmail\" class=\"wp-image-7329\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/less_secure_access_gmail.png 841w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/less_secure_access_gmail-300x92.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/less_secure_access_gmail-768x236.png 768w\" sizes=\"auto, (max-width: 841px) 100vw, 841px\" \/><figcaption>Less Secure Access Gmail<\/figcaption><\/figure><\/div>\n\n\n\n<p>Now, you&#8217;re ready to send emails using Python! Let&#8217;s move on.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Send Emails using Python SMTP<\/h2>\n\n\n\n<p>Python has an SMTP client library (<code>smtplib<\/code>), which it will use to send emails to an SMTP server (Gmail).<\/p>\n\n\n\n<p>This is a part of the standard library, so you can directly import it!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport smtplib\n<\/pre><\/div>\n\n\n<p>Okay, so now let&#8217;s try writing a script to send a test email.<\/p>\n\n\n\n<p>Any email using SMTP must have the following contents:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The Sender address<\/li><li>The receiver address<\/li><li>A subject (Optional)<\/li><li>The body of the mail<\/li><\/ul>\n\n\n\n<p>Let&#8217;s write all of them down.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport smtplib\n\nsender_address = &quot;sender@gmail.com&quot; # Replace this with your Gmail address\n\nreceiver_address = &quot;receiver@gmail.com&quot; # Replace this with any valid email address\n\naccount_password = &quot;xxxxxxxxxx&quot; # Replace this with your Gmail account password\n\nsubject = &quot;Test Email using Python&quot;\n\nbody = &quot;Hello from AskPython!\\n\\nHappy to hear from you!\\nWith regards,\\n\\tDeveloper&quot;\n\n# Endpoint for the SMTP Gmail server (Don&#039;t change this!)\nsmtp_server = smtplib.SMTP_SSL(&quot;smtp.gmail.com&quot;, 465)\n\n# Login with your Gmail account using SMTP\nsmtp_server.login(sender_address, account_password)\n\n# Let&#039;s combine the subject and the body onto a single message\nmessage = f&quot;Subject: {subject}\\n\\n{body}&quot;\n\n# We&#039;ll be sending this message in the above format (Subject:...\\n\\nBody)\nsmtp_server.sendmail(sender_address, receiver_address, message)\n\n# Close our endpoint\nsmtp_server.close()\n<\/pre><\/div>\n\n\n<p><strong>Make sure you replace<\/strong> the <code>sender_address<\/code>, <code>receiver_address<\/code> and <code>account_password<\/code> with your Gmail account information!<\/p>\n\n\n\n<p>What we&#8217;re doing is that we use the SMTP Server to access our Gmail account, using a Secure SMTP (<code>SMTP_SSL<\/code>). After we login, we can send the message to the receiver directly, using <code>smtp_server.sendmail()<\/code>!<\/p>\n\n\n\n<p>Now, if you enter the same account for the sender and receiver, you&#8217;ll get an email similar to mine.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"42\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/python_test_email.png\" alt=\"Python Test Email\" class=\"wp-image-7325\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/python_test_email.png 939w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/python_test_email-300x13.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/python_test_email-768x34.png 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><figcaption>Python Test Email<\/figcaption><\/figure><\/div>\n\n\n\n<p>Let&#8217;s check the contents.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"354\" height=\"274\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/python_test_email_result.png\" alt=\"Python Test Email Result\" class=\"wp-image-7326\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/python_test_email_result.png 354w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/python_test_email_result-300x232.png 300w\" sizes=\"auto, (max-width: 354px) 100vw, 354px\" \/><figcaption>Python Test Email Result<\/figcaption><\/figure><\/div>\n\n\n\n<p>Indeed, we&#8217;ve just sent a proper email using Python!<\/p>\n\n\n\n<p>You can improve the code, to ensure that the resources are always closed, using <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/python-with-context-managers\">context managers<\/a>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport smtplib\n\nsender_address = &quot;sender@gmail.com&quot; # Replace this with your Gmail address\n\nreceiver_address = &quot;receiver@gmail.com&quot; # Replace this with any valid email address\n\naccount_password = &quot;xxxxxxxxxx&quot; # Replace this with your Gmail account password\n\nsubject = &quot;Test Email using Python&quot;\n\nbody = &quot;Hello from AskPython!\\n\\nHappy to hear from you!\\nWith regards,\\n\\tDeveloper&quot;\n\n# We can use a context manager\nwith smtplib.SMTP_SSL(&quot;smtp.gmail.com&quot;, 465) as smtp_server:\n    # Login with your Gmail account using SMTP\n    smtp_server.login(sender_address, account_password)\n\n    # Let&#039;s combine the subject and the body onto a single message\n    message = f&quot;Subject: {subject}\\n\\n{body}&quot;\n\n    # We&#039;ll be sending this message in the above format (Subject:...\\n\\nBody)\n    smtp_server.sendmail(sender_address, receiver_address, message)\n<\/pre><\/div>\n\n\n<p>This will give the same results as before &#8211; another email!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we looked at how we could use Python to send emails easily, using gmail&#8217;s SMTP server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python SMTP <a href=\"https:\/\/docs.python.org\/3\/library\/smtplib.html\" target=\"_blank\" rel=\"noopener\">Documentation<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! In today&#8217;s article, we&#8217;ll be taking a look at how we can use Python to send emails. Sending Emails &#8211; A brief overview Usually, the task of sending emails is done using the MTP (Mail Transfer Protocol). In the present day, there is a separate protocol called SMTP (Simple Mail Transfer Protocol) which [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":7328,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-7321","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7321","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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7321"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7321\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/7328"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}