{"id":210,"date":"2023-12-09T15:10:28","date_gmt":"2023-12-09T15:10:28","guid":{"rendered":"https:\/\/mailmug.net\/blog\/?p=210"},"modified":"2023-12-13T03:23:33","modified_gmt":"2023-12-13T03:23:33","slug":"python-send-email","status":"publish","type":"post","link":"https:\/\/mailmug.net\/blog\/python-send-email\/","title":{"rendered":"Python Send Email Using SMTP Library"},"content":{"rendered":"\n<p>Discover the power of Python&#8217;s SMTP library for sending emails (Python send email tutorial). Learn how to effortlessly send HTML or plain-text emails to multiple recipients with seamless attachment support. The <strong>SMTPlib<\/strong> is the most popular <strong><a href=\"https:\/\/docs.python.org\/3\/library\/smtplib.html\">Python SMTP<\/a><\/strong> library. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Navigate to specific topics instantly by clicking on the entries in the table of contents below.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#setup-smtp\">Python Email SMTP Library<\/a><\/li>\n\n\n\n<li><a href=\"#sandbox-account\">SMTP Sandbox Account<\/a><\/li>\n\n\n\n<li><a href=\"#smtp-gmail\">Sending Email with Python via Gmail<\/a><\/li>\n\n\n\n<li><a href=\"#email-example\">Send Plain-Text Python Email Example<\/a><\/li>\n\n\n\n<li><a href=\"#python-html-email\">Send an HTML Email With Python<\/a><\/li>\n\n\n\n<li><a href=\"#python-email-attachment\">Send Email With Attachments<\/a><\/li>\n\n\n\n<li><a href=\"#multiple-recipients\">Send Email to Multiple Recipients<\/a><\/li>\n\n\n\n<li><a href=\"#bcc-cc\">SMTPlib Bcc , Cc, Reply-to<\/a><\/li>\n\n\n\n<li><a href=\"#variable\">Use a Variable on smtplib<\/a><\/li>\n\n\n\n<li>Errors<\/li>\n<\/ol>\n\n\n\n<div style=\"height:76px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"setup-smtp\">1. Python Email SMTP Library<\/h2>\n\n\n\n<p>The SMTPlib is most popular Python SMTP (Simple Mail Transfer Protocol) library. The <code>smtplib<\/code> is an in-built library in Python. It is part of the standard library, which means <strong>you don&#8217;t need to install any additional packages <\/strong>to use it.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sandbox-account\">2. SMTP Sandbox Account<\/h2>\n\n\n\n<p>Unlock the benefits of <a href=\"http:\/\/mailmug.net\">MailMug&#8217;s<\/a> complimentary SMTP sandbox account by signing up. Once your account is created, navigate to the Settings section, where you can conveniently copy and paste the provided SMTP credentials from your MailMug account for seamless integration.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/credentials.png\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"262\" src=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/credentials.png\" alt=\"SMPT Sandbox account for php flight\" class=\"wp-image-75\" srcset=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/credentials.png 631w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/credentials-300x125.png 300w\" sizes=\"(max-width: 631px) 100vw, 631px\" \/><\/a><\/figure>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"smtp-gmail\">3. Sending Email with Python via Gmail<\/h2>\n\n\n\n<p>Google (gmail)  is providing free SMTP server. First, we need to enable <strong><a href=\"https:\/\/www.google.com\/landing\/2step\/\">two step verification<\/a><\/strong> for your gmail account. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/gmail-app-password.png\"><img loading=\"lazy\" decoding=\"async\" width=\"713\" height=\"246\" src=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/gmail-app-password.png\" alt=\"gmail app password - SMTP\" class=\"wp-image-215\" srcset=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/gmail-app-password.png 713w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/gmail-app-password-300x104.png 300w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/a><\/figure>\n\n\n\n<p>Next, generate an app password by following these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Visit your <strong><a href=\"https:\/\/myaccount.google.com\/\">Google account<\/a><\/strong> settings.<\/li>\n\n\n\n<li>Navigate to the <strong>Security<\/strong> section.<\/li>\n\n\n\n<li>Select <strong>2-Step Verification<\/strong> >  <strong>App passwords <\/strong>(bottom)<strong>.<\/strong><\/li>\n\n\n\n<li>Create a new app password.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"854\" height=\"224\" src=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/app-password-gmail.png\" alt=\"smtp gmail password\" class=\"wp-image-216\" srcset=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/app-password-gmail.png 854w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/app-password-gmail-300x79.png 300w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/app-password-gmail-768x201.png 768w\" sizes=\"(max-width: 854px) 100vw, 854px\" \/><\/figure>\n\n\n\n<p>Now, input the credentials as outlined below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nHost: smtp.gmail.com\n\nUsername: youremail@gmail.com\n\nPassword: {app password}\n\nSecure(SSL)\n\nPort(465)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport smtplib, ssl\n\nport = 465  \npassword = &quot;app password here&quot;\ncontext = ssl.create_default_context()\n\nwith smtplib.SMTP_SSL(&quot;smtp.gmail.com&quot;, port, context=context) as server:\n    server.login(&quot;your-email@gmail.com&quot;, password)\n<\/pre><\/div>\n\n\n<div style=\"height:53px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"email-example\">4. Send Plain-Text Python Email Example<\/h2>\n\n\n\n<p>Create a\u00a0<strong>main.py<\/strong>\u00a0file then add the following code. Import <strong>smtplib<\/strong>, <strong>MIMEText<\/strong> and <strong>MIMEMultipart<\/strong> packages.  The smtplib is helping to communicate with SMTP server.  The<strong> <code>email.mime.text<\/code><\/strong> is a submodule that provides classes for creating <strong>MIME<\/strong> (Multipurpose Internet Mail Extensions) objects. The <code><strong>MIMEMultipart<\/strong><\/code> is a class that represents a MIME multipart message.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport smtplib\nfrom email.mime.text import MIMEText\nfrom email.mime.multipart import MIMEMultipart\n  \n  \nport = 2525\nsmtp_server = &quot;smtp.mailmug.net&quot;\nlogin = &quot;username here&quot; # paste your login generated by mailmug\npassword = &quot;pass here&quot; # paste your password generated by mailmug\n  \nsender_email = &quot;mailmug@example.com&quot;\nreceiver_email = &quot;new@example.com&quot;\nmessage = MIMEMultipart(&quot;alternative&quot;)\nmessage&#x5B;&quot;Subject&quot;] = &quot;Test subject&quot;\nmessage&#x5B;&quot;From&quot;] = sender_email\nmessage&#x5B;&quot;To&quot;] = receiver_email\n  \n  \n  \ntext = &quot;&quot;&quot;\\\n    hi, How are you?\n    &quot;&quot;&quot;\npart = MIMEText(text, &quot;plain&quot;)\nmessage.attach(part)\n\nserver = smtplib.SMTP(smtp_server, port)\nserver.set_debuglevel(1)\nserver.esmtp_features&#x5B;&#039;auth&#039;] = &#039;LOGIN DIGEST-MD5 PLAIN&#039;\nserver.login(login, password)\nserver.sendmail(\n    sender_email, receiver_email, message.as_string()\n)\n  \n  \n \n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<div style=\"height:68px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-html-email\">Python Send HTML Email<\/h2>\n\n\n\n<p>The Python HTML email template by MIMEText class. <code>MIMEText(html, \"html\")<\/code><\/p>\n\n\n\n<p>Change style by <strong>css<\/strong> codes in head section of the email html.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport smtplib\nfrom email.mime.text import MIMEText\nfrom email.mime.multipart import MIMEMultipart\n  \n  \nport = 2525\nsmtp_server = &quot;smtp.mailmug.net&quot;\nlogin = &quot;username here&quot; # paste your login generated by mailmug\npassword = &quot;pass here &quot; # paste your password generated by mailmug\n  \nsender_email = &quot;mailmug@example.com&quot;\nreceiver_email = &quot;new@example.com&quot;\nmessage = MIMEMultipart(&quot;alternative&quot;)\nmessage&#x5B;&quot;Subject&quot;] = &quot;Test subject&quot;\nmessage&#x5B;&quot;From&quot;] = sender_email\nmessage&#x5B;&quot;To&quot;] = receiver_email\n  \n  \n  \nhtml = &quot;&quot;&quot;\\\n    &lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;Your Email Title&lt;\/title&gt;\n    &lt;style&gt;\n        \/* Add your styles here *\/\n        body {\n            font-family: Arial, sans-serif;\n            background-color: #f4f4f4;\n            margin: 0;\n            padding: 50px;\n        }\n\n        .container {\n            max-width: 600px;\n            margin: 0 auto;\n            padding: 20px;\n            background-color: #ffffff;\n        }\n\n        h1 {\n            color: #333333;\n        }\n\n        p {\n            color: #555555;\n        }\n\n        \/* Add more styles as needed *\/\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div class=&quot;container&quot;&gt;\n        &lt;h1&gt;Your Email Heading&lt;\/h1&gt;\n        &lt;p&gt;This is a basic HTML email template. You can customize it to suit your needs.&lt;\/p&gt;\n\n        &lt;!-- Add your email content here --&gt;\n\n        &lt;p&gt;Thank you!&lt;\/p&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n    &quot;&quot;&quot;\npart = MIMEText(html, &quot;html&quot;)\nmessage.attach(part)\n\nserver = smtplib.SMTP(smtp_server, port)\nserver.set_debuglevel(1)\nserver.esmtp_features&#x5B;&#039;auth&#039;] = &#039;LOGIN DIGEST-MD5 PLAIN&#039;\nserver.login(login, password)\nserver.sendmail(\n    sender_email, receiver_email, message.as_string()\n)\n  \n  \n \n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-html-email.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"519\" src=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-html-email-1024x519.png\" alt=\"Python HTML email template\" class=\"wp-image-220\" srcset=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-html-email-1024x519.png 1024w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-html-email-300x152.png 300w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-html-email-768x390.png 768w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-html-email.png 1108w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<div style=\"height:63px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-email-attachment\">Send Email With Attachments<\/h2>\n\n\n\n<p>Sending emails with file attachments is facilitated through the <strong>MIMEApplication<\/strong> class, specifically designed for encoding non-text characters. Utilizing <strong>base64<\/strong> encoding, this class allows for the inclusion of file data. Retrieve the file data, store it within the <strong>MIMEApplication<\/strong> object, and seamlessly attach multiple files by iterating through the file array.<\/p>\n\n\n\n<p>Import following libraries<\/p>\n\n\n\n<p><code>from email.mime.application import MIMEApplication<\/code><\/p>\n\n\n\n<p><code>from os.path import basename<\/code><\/p>\n\n\n\n<p>The base <code>basename<\/code> is using to get filename from file path.<\/p>\n\n\n\n<p><strong>The Python Send Email with Attachemnts Example<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport smtplib\nfrom email.mime.text import MIMEText\nfrom email.mime.multipart import MIMEMultipart\nfrom email.mime.application import MIMEApplication\nfrom os.path import basename\n\n  \nport = 2525\nsmtp_server = &quot;smtp.mailmug.net&quot;\nlogin = &quot;user name&quot; # paste your login generated by mailmug\npassword = &quot;pass &quot; # paste your password generated by mailmug\n  \nsender_email = &quot;mailmug@example.com&quot;\nreceiver_email = &quot;new@example.com&quot;\nmessage = MIMEMultipart(&quot;alternative&quot;)\nmessage&#x5B;&quot;Subject&quot;] = &quot;Test subject&quot;\nmessage&#x5B;&quot;From&quot;] = sender_email\nmessage&#x5B;&quot;To&quot;] = receiver_email\n  \n  \n  \nhtml = &quot;&quot;&quot;\\\n    &lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;Your Email Title&lt;\/title&gt;\n    &lt;style&gt;\n        \/* Add your styles here *\/\n        body {\n            font-family: Arial, sans-serif;\n            background-color: #f4f4f4;\n            margin: 0;\n            padding: 50px;\n        }\n\n        .container {\n            max-width: 600px;\n            margin: 0 auto;\n            padding: 20px;\n            background-color: #ffffff;\n        }\n\n        h1 {\n            color: #333333;\n        }\n\n        p {\n            color: #555555;\n        }\n\n        \/* Add more styles as needed *\/\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div class=&quot;container&quot;&gt;\n        &lt;h1&gt;Your Email Heading&lt;\/h1&gt;\n        &lt;p&gt;This is a basic HTML email template. You can customize it to suit your needs.&lt;\/p&gt;\n\n        &lt;!-- Add your email content here --&gt;\n\n        &lt;p&gt;Thank you!&lt;\/p&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n    &quot;&quot;&quot;\npart = MIMEText(html, &quot;html&quot;)\nmessage.attach(part)\nfiles = &#x5B;&#039;my.jpg&#039;]\nfor f in files:\n    with open(f, &quot;rb&quot;) as fil:\n        file_part = MIMEApplication(\n            fil.read(),\n            Name=basename(f)\n        )\n    file_part&#x5B;&#039;Content-Disposition&#039;] = &#039;attachment; filename=&quot;%s&quot;&#039; % basename(f)\n    message.attach(file_part)\n\n\nserver = smtplib.SMTP(smtp_server, port)\nserver.set_debuglevel(1)\nserver.esmtp_features&#x5B;&#039;auth&#039;] = &#039;LOGIN DIGEST-MD5 PLAIN&#039;\nserver.login(login, password)\nserver.sendmail(\n    sender_email, receiver_email, message.as_string()\n)\n  \n  \n \n<\/pre><\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"multiple-recipients\">Send Email to Multiple Recipients<\/h2>\n\n\n\n<p>Sending emails to multiple recipients can be achieved by incorporating a list of email addresses into the <code>receiver_email<\/code> parameter within the <code>smtplib<\/code> library.<\/p>\n\n\n\n<p><code>receiver_email = [\"new@example.com\", \"new1@example.com\"]<\/code><\/p>\n\n\n\n<p><strong>Complete code:<\/strong>&#8211;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport smtplib\nfrom email.mime.text import MIMEText\nfrom email.mime.multipart import MIMEMultipart\nfrom email.mime.application import MIMEApplication\nfrom os.path import basename\n\n  \nport = 2525\nsmtp_server = &quot;smtp.mailmug.net&quot;\nlogin = &quot; &quot; # paste your login generated by mailmug\npassword = &quot; &quot; # paste your password generated by mailmug\n  \nsender_email = &quot;mailmug@example.com&quot;\nreceiver_email =  &#x5B;&quot;new@example.com&quot;, &quot;new1@example.com&quot;]\nmessage = MIMEMultipart(&quot;alternative&quot;)\nmessage&#x5B;&quot;Subject&quot;] = &quot;Test subject&quot;\nmessage&#x5B;&quot;From&quot;] = sender_email\n\nif isinstance(receiver_email, str):\n    message&#x5B;&quot;To&quot;] = receiver_email\nelse:\n    message&#x5B;&quot;To&quot;] = &quot;, &quot;.join(receiver_email)\n  \n  \n  \nhtml = &quot;&quot;&quot;\\\n    &lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;Your Email Title&lt;\/title&gt;\n    &lt;style&gt;\n        \/* Add your styles here *\/\n        body {\n            font-family: Arial, sans-serif;\n            background-color: #f4f4f4;\n            margin: 0;\n            padding: 50px;\n        }\n\n        .container {\n            max-width: 600px;\n            margin: 0 auto;\n            padding: 20px;\n            background-color: #ffffff;\n        }\n\n        h1 {\n            color: #333333;\n        }\n\n        p {\n            color: #555555;\n        }\n\n        \/* Add more styles as needed *\/\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div class=&quot;container&quot;&gt;\n        &lt;h1&gt;Your Email Heading&lt;\/h1&gt;\n        &lt;p&gt;This is a basic HTML email template. You can customize it to suit your needs.&lt;\/p&gt;\n\n        &lt;!-- Add your email content here --&gt;\n\n        &lt;p&gt;Thank you!&lt;\/p&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n    &quot;&quot;&quot;\npart = MIMEText(html, &quot;html&quot;)\nmessage.attach(part)\nfiles = &#x5B;&#039;my.jpg&#039;]\nfor f in files:\n    with open(f, &quot;rb&quot;) as fil:\n        file_part = MIMEApplication(\n            fil.read(),\n            Name=basename(f)\n        )\n    file_part&#x5B;&#039;Content-Disposition&#039;] = &#039;attachment; filename=&quot;%s&quot;&#039; % basename(f)\n    message.attach(file_part)\n\n\nserver = smtplib.SMTP(smtp_server, port)\nserver.set_debuglevel(1)\nserver.esmtp_features&#x5B;&#039;auth&#039;] = &#039;LOGIN DIGEST-MD5 PLAIN&#039;\nserver.login(login, password)\nserver.sendmail(\n    sender_email, receiver_email, message.as_string()\n)\n  \n  \n \n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/Multiple-Recipients-From-Python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"313\" src=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/Multiple-Recipients-From-Python-1024x313.png\" alt=\"Multiple Recipients From Python\" class=\"wp-image-151\" srcset=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/Multiple-Recipients-From-Python-1024x313.png 1024w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/Multiple-Recipients-From-Python-300x92.png 300w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/Multiple-Recipients-From-Python-768x235.png 768w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/Multiple-Recipients-From-Python.png 1084w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SMTPlib Bcc , Cc, Reply-to<\/h2>\n\n\n\n<p>Include &#8216;Cc,&#8217; &#8216;Bcc,&#8217; or &#8216;Reply-To&#8217; information in the MIMEMultipart message, and it will be seamlessly added to the email header section.<\/p>\n\n\n\n<p><strong>Example<\/strong>:-<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><code>message[\"Bcc\"] = \"bcc@example.com\"<br>message[\"Cc\"] = \"cc@example.com\"<\/code><\/p>\n\n\n\n<p><code>message[\"Reply-to\"] = \"cc@example.com\"<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-email-bcc-cc.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"525\" src=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-email-bcc-cc-1024x525.png\" alt=\"python email bcc,cc, reply-to\" class=\"wp-image-164\" srcset=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-email-bcc-cc-1024x525.png 1024w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-email-bcc-cc-300x154.png 300w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-email-bcc-cc-768x394.png 768w, https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-email-bcc-cc.png 1084w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<div style=\"height:73px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"variable\">Use a Variable on smtplib<\/h2>\n\n\n\n<p>Replace variables from HTML message by <code>replace<\/code> function as shortcode.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nname = &#039;Any name here&#039;\nmessage = &quot;Hi &#x5B;name] \\\nHow can I help you?&quot;\n\nmessage = message.replace(&#039;&#x5B;name&#039;], name)\n<\/pre><\/div>\n\n\n<div style=\"height:64px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">TimeoutError Trying To Send an Email with SMTPlib<\/h2>\n\n\n\n<p>The <code><strong>smtplib<\/strong><\/code> <strong>TimeoutError<\/strong> indicates that the connection to the SMTP server timed out.<\/p>\n\n\n\n<p><strong>Check SMTP Server and Port:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Verify that you are using the correct SMTP server address and port in your code.<\/li>\n\n\n\n<li>Check firewall configuration from your server<\/li>\n\n\n\n<li>Confirm that the <a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/opening-a-port-on-linux\">specified port is open<\/a> and accessible.<\/li>\n<\/ul>\n\n\n\n<p><strong>Use the Correct Security Protocol:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If your SMTP server requires a secure connection, make sure to use the appropriate security protocol (e.g., <code>starttls<\/code> or <code>ssl<\/code>).<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nserver.starttls()  # For STARTTLS\n# or\nserver = smtplib.SMTP_SSL(smtp_server, smtp_port)  # For SSL\n<\/pre><\/div>\n\n\n<p><strong>Increase Timeout Value:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the connection is consistently timing out, consider increasing the timeout value to give the server more time to respond.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nserver = smtplib.SMTP(smtp_server, smtp_port, timeout=30)  # Adjust the timeout value as needed\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Python SMTPAuthenticationError SMTPlib<\/h2>\n\n\n\n<p>The error message is <code><strong>SMTPAuthenticationError<\/strong>: 535, 534, or similar<\/code>. It is caused by incorrect username or password. Solution: double-check your email account credentials.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Discover the power of Python&#8217;s SMTP library for sending emails (Python send email tutorial). Learn how to effortlessly send HTML or plain-text emails to multiple recipients with seamless attachment support. The SMTPlib is the most popular Python SMTP library. Navigate to specific topics instantly by clicking on the entries in the table of contents below. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":235,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-210","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Send Email Using SMTP Library - MailMug Blog<\/title>\n<meta name=\"description\" content=\"Python send email using SMTP library. Learn how to effortlessly send HTML or plain-text emails to multiple recipients with attachments.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mailmug.net\/blog\/python-send-email\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Send Email Using SMTP Library - MailMug Blog\" \/>\n<meta property=\"og:description\" content=\"Python send email using SMTP library. Learn how to effortlessly send HTML or plain-text emails to multiple recipients with attachments.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mailmug.net\/blog\/python-send-email\/\" \/>\n<meta property=\"og:site_name\" content=\"MailMug Blog\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-09T15:10:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-13T03:23:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-email.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/mailmug.net\/blog\/python-send-email\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/mailmug.net\/blog\/python-send-email\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/mailmug.net\/blog\/#\/schema\/person\/97fb32aba211755b8d48bdd54df9c66d\"},\"headline\":\"Python Send Email Using SMTP Library\",\"datePublished\":\"2023-12-09T15:10:28+00:00\",\"dateModified\":\"2023-12-13T03:23:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/mailmug.net\/blog\/python-send-email\/\"},\"wordCount\":602,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/mailmug.net\/blog\/#organization\"},\"articleSection\":[\"python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/mailmug.net\/blog\/python-send-email\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/mailmug.net\/blog\/python-send-email\/\",\"url\":\"https:\/\/mailmug.net\/blog\/python-send-email\/\",\"name\":\"Python Send Email Using SMTP Library - MailMug Blog\",\"isPartOf\":{\"@id\":\"https:\/\/mailmug.net\/blog\/#website\"},\"datePublished\":\"2023-12-09T15:10:28+00:00\",\"dateModified\":\"2023-12-13T03:23:33+00:00\",\"description\":\"Python send email using SMTP library. Learn how to effortlessly send HTML or plain-text emails to multiple recipients with attachments.\",\"breadcrumb\":{\"@id\":\"https:\/\/mailmug.net\/blog\/python-send-email\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/mailmug.net\/blog\/python-send-email\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/mailmug.net\/blog\/python-send-email\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/mailmug.net\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Send Email Using SMTP Library\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/mailmug.net\/blog\/#website\",\"url\":\"https:\/\/mailmug.net\/blog\/\",\"name\":\"MailMug\",\"description\":\"SandBox SMTP Email Account\",\"publisher\":{\"@id\":\"https:\/\/mailmug.net\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/mailmug.net\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/mailmug.net\/blog\/#organization\",\"name\":\"MailMug\",\"url\":\"https:\/\/mailmug.net\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/mailmug.net\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/logo.png\",\"contentUrl\":\"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/logo.png\",\"width\":320,\"height\":293,\"caption\":\"MailMug\"},\"image\":{\"@id\":\"https:\/\/mailmug.net\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/mailmug.net\/blog\/#\/schema\/person\/97fb32aba211755b8d48bdd54df9c66d\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/mailmug.net\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1e1bdecb54e39d090acd1ba4b20cb7db?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1e1bdecb54e39d090acd1ba4b20cb7db?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\/\/mailmug.net\/blog\"],\"url\":\"https:\/\/mailmug.net\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Send Email Using SMTP Library - MailMug Blog","description":"Python send email using SMTP library. Learn how to effortlessly send HTML or plain-text emails to multiple recipients with attachments.","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:\/\/mailmug.net\/blog\/python-send-email\/","og_locale":"en_US","og_type":"article","og_title":"Python Send Email Using SMTP Library - MailMug Blog","og_description":"Python send email using SMTP library. Learn how to effortlessly send HTML or plain-text emails to multiple recipients with attachments.","og_url":"https:\/\/mailmug.net\/blog\/python-send-email\/","og_site_name":"MailMug Blog","article_published_time":"2023-12-09T15:10:28+00:00","article_modified_time":"2023-12-13T03:23:33+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/12\/python-email.png","type":"image\/png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mailmug.net\/blog\/python-send-email\/#article","isPartOf":{"@id":"https:\/\/mailmug.net\/blog\/python-send-email\/"},"author":{"name":"admin","@id":"https:\/\/mailmug.net\/blog\/#\/schema\/person\/97fb32aba211755b8d48bdd54df9c66d"},"headline":"Python Send Email Using SMTP Library","datePublished":"2023-12-09T15:10:28+00:00","dateModified":"2023-12-13T03:23:33+00:00","mainEntityOfPage":{"@id":"https:\/\/mailmug.net\/blog\/python-send-email\/"},"wordCount":602,"commentCount":0,"publisher":{"@id":"https:\/\/mailmug.net\/blog\/#organization"},"articleSection":["python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/mailmug.net\/blog\/python-send-email\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/mailmug.net\/blog\/python-send-email\/","url":"https:\/\/mailmug.net\/blog\/python-send-email\/","name":"Python Send Email Using SMTP Library - MailMug Blog","isPartOf":{"@id":"https:\/\/mailmug.net\/blog\/#website"},"datePublished":"2023-12-09T15:10:28+00:00","dateModified":"2023-12-13T03:23:33+00:00","description":"Python send email using SMTP library. Learn how to effortlessly send HTML or plain-text emails to multiple recipients with attachments.","breadcrumb":{"@id":"https:\/\/mailmug.net\/blog\/python-send-email\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mailmug.net\/blog\/python-send-email\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/mailmug.net\/blog\/python-send-email\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mailmug.net\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Send Email Using SMTP Library"}]},{"@type":"WebSite","@id":"https:\/\/mailmug.net\/blog\/#website","url":"https:\/\/mailmug.net\/blog\/","name":"MailMug","description":"SandBox SMTP Email Account","publisher":{"@id":"https:\/\/mailmug.net\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mailmug.net\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/mailmug.net\/blog\/#organization","name":"MailMug","url":"https:\/\/mailmug.net\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/mailmug.net\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/logo.png","contentUrl":"https:\/\/mailmug.net\/blog\/wp-content\/uploads\/2023\/08\/logo.png","width":320,"height":293,"caption":"MailMug"},"image":{"@id":"https:\/\/mailmug.net\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/mailmug.net\/blog\/#\/schema\/person\/97fb32aba211755b8d48bdd54df9c66d","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/mailmug.net\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/1e1bdecb54e39d090acd1ba4b20cb7db?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1e1bdecb54e39d090acd1ba4b20cb7db?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/mailmug.net\/blog"],"url":"https:\/\/mailmug.net\/blog\/author\/admin\/"}]}},"amp_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/posts\/210"}],"collection":[{"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/comments?post=210"}],"version-history":[{"count":16,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":230,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/posts\/210\/revisions\/230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/media\/235"}],"wp:attachment":[{"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/categories?post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailmug.net\/blog\/wp-json\/wp\/v2\/tags?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}