{"id":11148,"date":"2016-02-24T16:15:07","date_gmt":"2016-02-24T14:15:07","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=11148"},"modified":"2018-01-09T09:45:12","modified_gmt":"2018-01-09T07:45:12","slug":"python-send-email-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/","title":{"rendered":"Python Send Email Example"},"content":{"rendered":"<p>In this example, by using Python 3.4, we&#8217;ll learn how to send mails using Python&#8217;s <code>smtplib<\/code> module.<\/p>\n<p>SMTP stands for Simple Mail Transfer Protocol. It&#8217;s a protocol which handles sending e-mails and routing them between mail servers.<\/p>\n<p>To send a mail, we need the host and port of a server, then we send to this server the message with a sender and a list of receivers.<\/p>\n<p>The message is not just any string. This protocol expects the message in a certain format, which defines three headers (<strong>From<\/strong>, <strong>To<\/strong> and <strong>Subject<\/strong>) and the actual message, these separated with a blank line as in:<\/p>\n<pre class=\"brush:python\">\r\nmessage = \"\"\"From: Example Sender &lt;sender@example.com&gt;\r\nTo: Example Receiver &lt;receiver@example.com&gt;\r\nSubject: Example Message\r\n\r\nThis is an example message. Sorry if you receive this by mistake.\"\"\"\r\n<\/pre>\n<p>[ulp id=&#8217;R7QVpFMZmjosABLC&#8217;]<\/p>\n<h2>1. The Basics of smtplib<\/h2>\n<p>The <code>smtplib<\/code> module provides an SMTP client session object that can be used to send mails to any Internet machine with an SMTP or ESMTP listener daemon. Its constructor receives five optional parameters:<\/p>\n<ul>\n<li><strong>host<\/strong>: This is the host running the SMTP server. You can specifiy IP address of the host or a domain name like webcodegeeks.com.<\/li>\n<li><strong>port<\/strong>: When you provide a host argument, you need to specify the port your server is listening.<\/li>\n<li><strong>local_hostname<\/strong>: If your SMTP server is running locally, you can specify <em>&#8220;localhost&#8221;<\/em> here.<\/li>\n<li><strong>timeout<\/strong>: Specifies a timeout in seconds for blocking operations like the connection attempt.<\/li>\n<li><strong>source_address<\/strong>: Allows to bind to some specific source address in a machine with multiple network interfaces, and\/or to some specific source TCP port.<\/li>\n<\/ul>\n<p>An instance of this object encapsulates an SMTP connection. If the optional host and port parameters are given, the SMTP <code>connect()<\/code> method is called with those parameters during initialization. If specified, <code>local_hostname<\/code> is used as the FQDN of the local host in the HELO\/EHLO command. Otherwise, the local hostname is found using <code>socket.getfqdn()<\/code>. If the <code>connect()<\/code> call returns anything other than a success code, an <code>SMTPConnectError<\/code> is raised. If the timeout expires, <code>socket.timeout<\/code> is raised. The optional <code>source_address<\/code> parameter takes a 2-tuple (host, port), for the socket to bind to as its source address before connecting. If omitted (or if host or port are <code>''<\/code> and\/or <code>0<\/code> respectively) the OS default behavior will be used.<\/p>\n<p>Once this session object is created, it will provide a function called <code>sendmail<\/code> which receives three arguments (<strong>sender<\/strong>, <strong>receivers<\/strong> and <strong>message<\/strong>). <code>sender<\/code> is the e-mail address from which this mail will be sent, <code>receivers<\/code> is an array of e-mail addresses which will be the recipients, and <code>message<\/code> is the body of the mail, it should be formatted as we talked before.<\/p>\n<p>Let&#8217;s see an example of how to apply the knowledge we acquired so far.<\/p>\n<p><span style=\"text-decoration: underline\"><em>simple.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nimport smtplib\r\n\r\nsender = \"sender@example.com\"\r\nreceivers = [\"receiver1@example.com\", \"receiver2@example.com\"]\r\n\r\n\r\ndef format_mail(mail):\r\n    return \"{} \".format(mail.split(\"@\")[0], mail)\r\n\r\nmessage = \"\"\"From: {}\r\nTo: {}\r\nSubject: Example Subject\r\n\r\nThis is a test mail example\r\n\"\"\".format(\"{} \".format(sender.split(\"@\")[0], sender), \", \".join(map(format_mail, receivers)))\r\n\r\ntry:\r\n    print(\"sending message: \" + message)\r\n    with smtplib.SMTP('example-smpt.com', 25) as session:\r\n        session.sendmail(sender, receivers, message)\r\n    print(\"message sent\")\r\nexcept smtplib.SMTPException:\r\n    print(\"could not send mail\")\r\n\r\n<\/pre>\n<p>Of course, the SMPT server defined in this example does not exist, so this example won&#8217;t actually work. In fact, as we need passwords to send mails (we&#8217;ll see this below), none of the examples in this article will <em>actually<\/em> work, unless you replace the credentials in them with ones of your own.<\/p>\n<p>Having said that, let&#8217;s talk about this simple example. It&#8217;s just a really simple example which defines a sender, an array of recipients and a message (formatted as it should be, and dynamically inserting the addresses). Then it creates a session and calls <code>sendmail<\/code>. Notice the use of the <code>with<\/code> statement, when used like this, the SMTP <code>QUIT<\/code> command is issued automatically when the <code>with<\/code> statement exits.<\/p>\n<h2>2. SSL and Authentication<\/h2>\n<p>Let&#8217;s talk bout login. Usually we send a mail from an account to which we have access, and it&#8217;s secured (most of the times), so authentication will be required. Also, connections to an SMTP server should be done via SSL from the beginning of the connection. In the next example we&#8217;ll see both login and SSL connections procedures.<\/p>\n<p>Python provides a class called <code>SMTP_SSL<\/code>, which behaves exactly like <code>SMTP<\/code>. It&#8217;s constructor receives almost the same arguments. If the host is not specified, it will use the local host instead. If port is zero, it will use the default for SSL SMTP connections, which is 465. The optional arguments local_hostname, timeout and source_address have the same meaning as they do in the <code>SMTP<\/code> class. There are also three more arguments. The first one is <code>context<\/code>, which is optional, and can contain a SSLContext and allows to configure various aspects of the secure connection. The other ones are <code>keyfile<\/code> and <code>certfile<\/code>, which are a legacy alternative to context, and can point to a PEM formatted private key and certificate chain file for the SSL connection.<\/p>\n<p>Now, both <code>SMTP<\/code> and <code>SMTP_SSL<\/code> provide a function called <code>login<\/code> which receives a user and a password, and is our way through authentication. The next example will show how to use <code>SMTP_SSL<\/code> and the <code>login<\/code> function, but keep in mind that this function (and almost every other) behave the same in both <code>SMTP<\/code> and <code>SMTP_SSL<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>login_ssl.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nimport smtplib\r\n\r\nsender = \"sender@example.com\"\r\nreceivers = [\"receiver@example.com\"]\r\n\r\n\r\ndef format_mail(mail):\r\n    return \"{} \".format(mail.split(\"@\")[0], mail)\r\n\r\nmessage = \"\"\"From: {}\r\nTo: {}\r\nSubject: Example Subject\r\n\r\nThis is a test mail example\r\n\"\"\".format(\"{} \".format(sender.split(\"@\")[0], sender), \", \".join(map(format_mail, receivers)))\r\n\r\ntry:\r\n    print(\"sending message: \" + message)\r\n    with smtplib.SMTP_SSL('smtp.example.com', 465) as session:\r\n        session.login(\"sender@example.com\", \"sender_password\")\r\n        session.sendmail(sender, receivers, message)\r\n    print(\"message sent\")\r\nexcept smtplib.SMTPException:\r\n    print(\"could not send mail\")\r\n\r\n<\/pre>\n<p>The only thing that changed from <code>simple.py<\/code> is that we are now getting an instance of <code>SMTP_SSL<\/code> pointing to the default SMTP SSL port (465), and we added a line which invokes <code>SMTP_SSL.login(user, password)<\/code>.<\/p>\n<p>This example was tested with the GMail SMTP server, via SSL, with my own credentials and worked just fine.<\/p>\n<h2>3. Sending HTML<\/h2>\n<p>Remember how I talked about the headers required for the message to be compliant with the SMTP protocol? Well, as those are the ones required, there are other headers which can be sent that can let us do some pretty awesome stuff.<\/p>\n<p>By using the headers <code>MIME-Version<\/code> and <code>Content-type<\/code> we can tell the mail client how to interpret the information we are sending. We&#8217;ll now see how to set these headers to send HTML in a mail, but keep in mind that this is not just as easy as writing HTML in a browser, there are so so so&#8230; so many mail clients out there, that writing HTML that is compliant to every one of them is a pretty defying task.<\/p>\n<p><span style=\"text-decoration: underline\"><em>html_mail.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nimport smtplib\r\n\r\n\r\ndef format_mail(mail):\r\n    return \"{} \".format(mail.split(\"@\")[0], mail)\r\n\r\nsmtp_server_host = \"smtp.example.com\"\r\nsmtp_server_port = 465\r\nsender = \"sender@example.com\"\r\npwd = \"sender_password\"\r\nreceivers = [\"receiver@example.com\"]\r\nmessage = \"\"\"\r\n&lt;h1&gt;This is a title&lt;\/h1&gt;\r\n&lt;h2&gt;This is a sub title&lt;\/h2&gt;\r\n&lt;p&gt;This is a paragraph &lt;strong&gt;with some bold text&lt;\/strong&gt;.&lt;\/p&gt;\r\n\"\"\"\r\n\r\nformatted_message = \"\"\"From: {}\r\nTo: {}\r\nMIME-Version: 1.0\r\nContent-type: text\/html\r\nSubject: Example Subject\r\n\r\n{}\r\n\"\"\".format(\"{} \".format(sender.split(\"@\")[0], sender), \", \".join(map(format_mail, receivers)), message)\r\n\r\ntry:\r\n    print(\"sending message: \" + message)\r\n    with smtplib.SMTP_SSL(smtp_server_host, smtp_server_port) as session:\r\n        session.login(sender, pwd)\r\n        session.sendmail(sender, receivers, formatted_message)\r\n    print(\"message sent\")\r\nexcept smtplib.SMTPException:\r\n    print(\"could not send mail\")\r\n<\/pre>\n<p>As you see, the actual code for connecting to the SMTP server, logging in and sending the message is the same as before. The only thing we actually changed was the headers of the message. By setting <code>MIME-Version: 1.0<\/code> and <code>Content-type: text\/html<\/code>, the mail client will now know that this is HTML and should be rendered as such.<\/p>\n<h2>4. Sending Attachments<\/h2>\n<p>To send a mail with mixed content you need to send the <code>Content-type<\/code> header to <code>multipart\/mixed<\/code> and then, text and attachment sections can be specified within boundaries. A boundary is started with two hyphens followed by a unique string, which cannot appear in the message part of the e-mail. A final boundary denoting the e-mail&#8217;s final section must also end with two hyphens.<\/p>\n<p>We&#8217;ll now send a mail with HTML in the body and an attachment. It will be a text file containing the following piece of <em>Lorem Ipsum<\/em>:<\/p>\n<p><span style=\"text-decoration: underline\"><em>attachment.txt<\/em><\/span><\/p>\n<p><em>&#8220;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu justo vel tortor hendrerit dignissim et sit amet arcu. Pellentesque in sapien ipsum. Donec vitae neque blandit, placerat leo ac, tincidunt libero. Donec ac sem ut libero volutpat facilisis sit amet ac ante. Interdum et malesuada fames ac ante ipsum primis in faucibus. Duis quis elit porta, bibendum lacus vel, auctor sem. Nulla ut bibendum ipsum. In efficitur mauris sed interdum commodo. Maecenas enim orci, vestibulum et dui id, pretium vestibulum purus. Etiam semper dui ante, convallis volutpat massa convallis ut. Pellentesque at enim quis est bibendum eleifend sit amet eget enim.<\/p>\n<p>Morbi cursus ex ut orci semper viverra. Aenean ornare erat justo. Cras interdum mauris eu mauris aliquet tincidunt. Praesent semper non tellus a vehicula. Suspendisse potenti. Sed blandit tempus quam. In sem massa, volutpat nec augue eu, commodo tempus metus. Fusce laoreet, nunc in bibendum placerat, sem ex malesuada est, nec dictum ex diam nec augue. Aliquam auctor fringilla nulla, vitae laoreet eros laoreet ut. Sed consectetur semper risus non efficitur. Nunc pharetra rhoncus consectetur.<\/p>\n<p>Nam dictum porta velit sit amet ultricies. Praesent eu est vel ex pretium mattis sit amet a ex. Mauris elit est, eleifend et interdum nec, porttitor quis turpis. Sed nisl ligula, tempus ac eleifend nec, faucibus at massa. Nam euismod quam a diam iaculis, luctus convallis neque sollicitudin. Etiam eget blandit magna, non posuere nisi. Aliquam imperdiet, eros nec vestibulum pellentesque, ante dolor tempor libero, in efficitur leo lectus eu ex. Pellentesque sed posuere justo. Etiam vestibulum, urna at varius faucibus, odio eros aliquet tortor, nec rhoncus magna massa eu felis. Phasellus in nulla diam.<\/p>\n<p>Pellentesque blandit sapien orci, sit amet facilisis elit commodo quis. Quisque euismod imperdiet mi eu ultricies. Nunc quis pellentesque felis, aliquam ultrices neque. Duis quis enim non purus viverra molestie eget porttitor orci. Morbi ligula magna, lacinia pulvinar dolor at, vehicula iaculis felis. Sed posuere eget purus sed pharetra. Fusce commodo enim sed nisl mollis, eu pulvinar ligula rutrum.<\/p>\n<p>In mattis posuere fringilla. Mauris bibendum magna volutpat arcu mollis, nec semper lorem tincidunt. Aenean dictum feugiat justo id condimentum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Morbi lobortis, ipsum et condimentum cursus, risus nunc porta enim, nec ultrices velit libero eget dui. Nulla consequat id mi nec hendrerit. Suspendisse et odio at mauris viverra pellentesque. Nunc eget congue nisi.&#8221;<\/em><\/p>\n<p>Here goes the python code to send this file attached to a mail with HTML:<\/p>\n<p><span style=\"text-decoration: underline\"><em>attachment_html_mail.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nimport smtplib\r\n\r\n\r\ndef format_mail(mail):\r\n    return \"{} \".format(mail.split(\"@\")[0], mail)\r\n\r\n\r\ndef message_template(sender, receivers, subject, boundary, body, file_name, attachment):\r\n    return \"\"\"From: {}\r\nTo: {}\r\nSubject: {}\r\nMIME-Version: 1.0\r\nContent-Type: multipart\/mixed; boundary={}\r\n--{}\r\nContent-Type: text\/html\r\nContent-Transfer-Encoding:8bit\r\n\r\n{}\r\n--{}\r\nContent-Type: multipart\/mixed; name=\"{}\"\r\nContent-Transfer-Encoding:base64\r\nContent-Disposition: attachment; filename={}\r\n\r\n{}\r\n--{}--\r\n\"\"\".format(format_mail(sender), \", \".join(map(format_mail, receivers)), subject, boundary, boundary, body,\r\n           boundary, file_name, file_name, attachment, boundary)\r\n\r\n\r\ndef main():\r\n    sender = \"sender@example.com\"\r\n    receivers = [\"receiver@example.com\"]\r\n    subject = \"Test Mail with Attachment and HTML\"\r\n    boundary = \"A_BOUNDARY\"\r\n    msg_body = \"\"\"\r\n    &lt;h1&gt;Hello there!&lt;\/h1&gt;\r\n    &lt;p&gt;You will find &lt;strong&gt;attachment.txt&lt;\/strong&gt; attached to this mail. &lt;strong&gt;Read it pls!&lt;\/strong&gt;&lt;\/p&gt;\r\n    \"\"\"\r\n    file_name = \"attachment.txt\"\r\n    attachment = open(file_name, \"rb\").read().decode()\r\n\r\n    smtp_server_host = \"smtp.example.com\"\r\n    smtp_server_port = 465\r\n    pwd = \"sender_password\"\r\n\r\n    message = message_template(sender, receivers, subject, boundary, msg_body, file_name, attachment)\r\n\r\n    try:\r\n        with smtplib.SMTP_SSL(smtp_server_host, smtp_server_port) as session:\r\n            session.login(sender, pwd)\r\n            session.sendmail(sender, receivers, message)\r\n        print(\"mail was successfully sent\")\r\n    except smtplib.SMTPException:\r\n        print(\"could not send mail\")\r\n\r\n\r\nif __name__ == '__main__':\r\n    main()\r\n\r\n<\/pre>\n<p>Again, the process of connecting to the SMTP server, logging in and sending the mail is untouched. The magic is happening in the message, with its headers and content.<\/p>\n<p>The first headers define <code>From<\/code>, <code>To<\/code>, <code>Subject<\/code>, <code>MIME-Version<\/code>, <code>Content-Type<\/code> and, with it, <code>boundary<\/code>. These are the headers for the whole message.<\/p>\n<p>Then, after using the boundary to separate the definitions of the global headers from the body of the message, we are defining the <code>Content-Type<\/code> and the <code>Content-Transfer-Encoding<\/code> <strong>for the body of the mail only<\/strong>, and then we insert the body of the message.<\/p>\n<p>Separated from that last section, we are defining the <code>Content-Type<\/code>, with the <code>name<\/code> (of the attachment), the <code>Content-Transfer-Encoding<\/code> and the <code>Content-Disposition<\/code> with the <code>filename<\/code>, and then we insert the file content. Then we just end the mail with two hyphens.<\/p>\n<h2>5. Download the Code Project<\/h2>\n<p>This was a basic example on how to send mails with Python&#8217;s <code>smtplib<\/code> module.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/> You can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/02\/mail-example.zip\"><strong>python-mail<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example, by using Python 3.4, we&#8217;ll learn how to send mails using Python&#8217;s smtplib module. SMTP stands for Simple Mail Transfer Protocol. It&#8217;s a protocol which handles sending e-mails and routing them between mail servers. To send a mail, we need the host and port of a server, then we send to this &hellip;<\/p>\n","protected":false},"author":109,"featured_media":1651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[337,338],"class_list":["post-11148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-mail","tag-smtp"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Send Email Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"In this example, by using Python 3.4, we&#039;ll learn how to send mails using Python&#039;s smtplib module. SMTP stands for Simple Mail Transfer Protocol. It&#039;s a\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Send Email Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"In this example, by using Python 3.4, we&#039;ll learn how to send mails using Python&#039;s smtplib module. SMTP stands for Simple Mail Transfer Protocol. It&#039;s a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/sgvinci\" \/>\n<meta property=\"article:published_time\" content=\"2016-02-24T14:15:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T07:45:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sebastian Vinci\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@sebastianvinci_\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sebastian Vinci\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/\"},\"author\":{\"name\":\"Sebastian Vinci\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa\"},\"headline\":\"Python Send Email Example\",\"datePublished\":\"2016-02-24T14:15:07+00:00\",\"dateModified\":\"2018-01-09T07:45:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/\"},\"wordCount\":1665,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"keywords\":[\"mail\",\"smtp\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/\",\"name\":\"Python Send Email Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"datePublished\":\"2016-02-24T14:15:07+00:00\",\"dateModified\":\"2018-01-09T07:45:12+00:00\",\"description\":\"In this example, by using Python 3.4, we'll learn how to send mails using Python's smtplib module. SMTP stands for Simple Mail Transfer Protocol. It's a\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Send Email Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa\",\"name\":\"Sebastian Vinci\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g\",\"caption\":\"Sebastian Vinci\"},\"description\":\"Sebastian is a full stack programmer, who has strong experience in Java and Scala enterprise web applications. He is currently studying Computers Science in UBA (University of Buenos Aires) and working a full time job at a .com company as a Semi-Senior developer, involving architectural design, implementation and monitoring. He also worked in automating processes (such as data base backups, building, deploying and monitoring applications).\",\"sameAs\":[\"http:\/\/www.webcodegeeks.com\/\",\"https:\/\/www.facebook.com\/sgvinci\",\"https:\/\/x.com\/sebastianvinci_\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/sebastian-vinci\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Send Email Example - Web Code Geeks - 2026","description":"In this example, by using Python 3.4, we'll learn how to send mails using Python's smtplib module. SMTP stands for Simple Mail Transfer Protocol. It's a","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:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/","og_locale":"en_US","og_type":"article","og_title":"Python Send Email Example - Web Code Geeks - 2026","og_description":"In this example, by using Python 3.4, we'll learn how to send mails using Python's smtplib module. SMTP stands for Simple Mail Transfer Protocol. It's a","og_url":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_author":"https:\/\/www.facebook.com\/sgvinci","article_published_time":"2016-02-24T14:15:07+00:00","article_modified_time":"2018-01-09T07:45:12+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","type":"image\/jpeg"}],"author":"Sebastian Vinci","twitter_card":"summary_large_image","twitter_creator":"@sebastianvinci_","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Sebastian Vinci","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/"},"author":{"name":"Sebastian Vinci","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa"},"headline":"Python Send Email Example","datePublished":"2016-02-24T14:15:07+00:00","dateModified":"2018-01-09T07:45:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/"},"wordCount":1665,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","keywords":["mail","smtp"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/","url":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/","name":"Python Send Email Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","datePublished":"2016-02-24T14:15:07+00:00","dateModified":"2018-01-09T07:45:12+00:00","description":"In this example, by using Python 3.4, we'll learn how to send mails using Python's smtplib module. SMTP stands for Simple Mail Transfer Protocol. It's a","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/python\/python-send-email-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/www.webcodegeeks.com\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Send Email Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa","name":"Sebastian Vinci","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g","caption":"Sebastian Vinci"},"description":"Sebastian is a full stack programmer, who has strong experience in Java and Scala enterprise web applications. He is currently studying Computers Science in UBA (University of Buenos Aires) and working a full time job at a .com company as a Semi-Senior developer, involving architectural design, implementation and monitoring. He also worked in automating processes (such as data base backups, building, deploying and monitoring applications).","sameAs":["http:\/\/www.webcodegeeks.com\/","https:\/\/www.facebook.com\/sgvinci","https:\/\/x.com\/sebastianvinci_"],"url":"https:\/\/www.webcodegeeks.com\/author\/sebastian-vinci\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/11148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/109"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=11148"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/11148\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/1651"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=11148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=11148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=11148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}