{"id":265,"date":"2017-07-22T17:58:18","date_gmt":"2017-07-22T16:58:18","guid":{"rendered":"http:\/\/192.168.1.18\/?page_id=265"},"modified":"2017-07-24T22:47:47","modified_gmt":"2017-07-24T21:47:47","slug":"python-code-snippets-send-email-smtp","status":"publish","type":"page","link":"http:\/\/www.bulis.co.uk\/?page_id=265","title":{"rendered":"Python: Code Snippets &#8211; Send Email (SMTP)"},"content":{"rendered":"<p>This script is used to send emails using an SMTP (Simple Mail Transfer Protocol). To send emails you will need to supply the following information:<\/p>\n<ul>\n<li>Email Addres to Send To<\/li>\n<li>Email Addres to be Sent From<\/li>\n<li>Email Account Username<\/li>\n<li>Email Account Password<\/li>\n<li>SMTP Server<\/li>\n<li>SMTP Port Number<\/li>\n<\/ul>\n<p>To run this script copy the code below and save to a file called send_mail.py<\/p>\n<p>send_mail.py:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python\r\n\r\n# SendEmail\r\n# A quick way to send emails\r\n\r\n# Date: 07 March 2016\r\n# Written By: Phantom Raspberry Blower\r\n\r\nimport smtplib\r\n\r\nfrom email.mime.multipart import MIMEMultipart\r\nfrom email.mime.text import MIMEText\r\nfrom email.mime.image import MIMEImage\r\n\r\n\r\nclass SendEmail():\r\n\r\n    # Initialize object\r\n    def __init__(self, verbose):\r\n        self.verbose = verbose\r\n\r\n    # Used to send email\r\n    def send_email(self,\r\n                   send_to,\r\n                   sent_from,\r\n                   username,\r\n                   password,\r\n                   smtp_server,\r\n                   port,\r\n                   subject,\r\n                   message,\r\n                   output_path='',\r\n                   filename='',\r\n                   image_full_path='',\r\n                   image_type='jpg',\r\n                   hyper_txt=''):\r\n        if len(send_to) &gt; 0:\r\n            # Send email with images as attachments\r\n            try:\r\n                msg = MIMEMultipart('related')\r\n                msg&#x5B;&quot;From&quot;] = sent_from\r\n                msg&#x5B;&quot;To&quot;] = send_to\r\n                msg&#x5B;&quot;Subject&quot;] = subject\r\n                msg.preamble = &quot;The Phantom Raspberry Blower has struck again!&quot;\r\n\r\n                # Encapsulate the plain and HTML versions of the message\r\n                # body in an 'alternative' part, so message agents can\r\n                # decide which they want to display.\r\n                msgAlternative = MIMEMultipart('alternative')\r\n                msg.attach(msgAlternative)\r\n                msgText = MIMEText(message, 'plain')\r\n                msgAlternative.attach(msgText)\r\n\r\n                if len(image_full_path) &gt; 0:\r\n                    fp = open(image_full_path)\r\n                    msgImage = MIMEImage(fp.read(), image_type)\r\n                    fp.close()\r\n                    # We reference the image in the IMG SRC attribute by\r\n                    # the ID we give it below\r\n                    msgText = MIMEText('&lt;img src=&quot;cid:image1&quot;&gt;%s' % hyper_txt,\r\n                                       'html')\r\n                    msgAlternative.attach(msgText)\r\n\r\n                    # Define the image's ID as referenced above\r\n                    msgImage.add_header('Content-Type', 'image\/jpg',\r\n                                        filename=filename + '.jpg')\r\n                    msgImage.add_header('Content-ID', 'image1')\r\n                    msgImage.add_header('Content-Disposition',\r\n                                        'inline',\r\n                                        filename=filename + '.jpg')\r\n                    msg.attach(msgImage)\r\n\r\n                # Send email and image attachment\r\n                smtpObj = smtplib.SMTP(smtp_server, port)\r\n                smtpObj.login(username, password)\r\n                smtpObj.sendmail(sent_from, send_to, msg.as_string())\r\n                smtpObj.quit()\r\n                if self.verbose:\r\n                    print(&quot;Email sent successfully :)&quot;)\r\n                    return True\r\n            except smtplib.SMTPException:\r\n                print(&quot;Something wicked happened :(&quot;)\r\n                return False\r\n        else:\r\n            print(&quot;No email settings!&quot;)\r\n            return False\r\n\r\n# Check if running stand-alone or imported\r\nif __name__ == '__main__':\r\n    from send_mail import SendEmail\r\n    se = SendEmail(True)\r\n    send_to = raw_input(&quot;Type email address to send to: &quot;)\r\n    sent_from = raw_input(&quot;Type email address to be sent from: &quot;)\r\n    username = raw_input(&quot;Type email account username: &quot;)\r\n    password = raw_input(&quot;Type email account password: &quot;)\r\n    smtp_server = raw_input(&quot;Type SMTP server: &quot;)\r\n    port = raw_input(&quot;Type SMTP port number: &quot;)\r\n    subject = 'Test Email sent using Python'\r\n    message = 'A message to you Rudy!'\r\n    info = se.send_email(send_to,\r\n                         sent_from,\r\n                         username,\r\n                         password,\r\n                         smtp_server,\r\n                         int(port),\r\n                         subject,\r\n                         message)\r\n\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython send_mail.py\r\nType email address to send to:\r\nType email address to be sent from:\r\nType email account username: \r\nType email account password:\r\nType SMTP server: \r\nType SMTP port number:\r\nEmail sent successfully : )\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This script is used to send emails using an SMTP (Simple Mail Transfer Protocol). To send emails you will need to supply the following information: Email Addres to Send To Email Addres to be Sent From Email Account Username Email Account Password SMTP Server SMTP Port Number To run this script copy the code below [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":239,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python: Code Snippets - Send Email (SMTP) - Phantom Raspberry Blower<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.bulis.co.uk\/?page_id=265\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: Code Snippets - Send Email (SMTP) - Phantom Raspberry Blower\" \/>\n<meta property=\"og:description\" content=\"This script is used to send emails using an SMTP (Simple Mail Transfer Protocol). To send emails you will need to supply the following information: Email Addres to Send To Email Addres to be Sent From Email Account Username Email Account Password SMTP Server SMTP Port Number To run this script copy the code below [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.bulis.co.uk\/?page_id=265\" \/>\n<meta property=\"og:site_name\" content=\"Phantom Raspberry Blower\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-24T21:47:47+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=265\",\"url\":\"http:\/\/www.bulis.co.uk\/?page_id=265\",\"name\":\"Python: Code Snippets - Send Email (SMTP) - Phantom Raspberry Blower\",\"isPartOf\":{\"@id\":\"http:\/\/www.bulis.co.uk\/#website\"},\"datePublished\":\"2017-07-22T16:58:18+00:00\",\"dateModified\":\"2017-07-24T21:47:47+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=265#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.bulis.co.uk\/?page_id=265\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=265#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.bulis.co.uk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=57\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=73\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Python: Code Snippets\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=239\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Python: Code Snippets &#8211; Send Email (SMTP)\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.bulis.co.uk\/#website\",\"url\":\"http:\/\/www.bulis.co.uk\/\",\"name\":\"Phantom Raspberry Blower\",\"description\":\"Blowing Raspberrys since 1989\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.bulis.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: Code Snippets - Send Email (SMTP) - Phantom Raspberry Blower","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":"http:\/\/www.bulis.co.uk\/?page_id=265","og_locale":"en_GB","og_type":"article","og_title":"Python: Code Snippets - Send Email (SMTP) - Phantom Raspberry Blower","og_description":"This script is used to send emails using an SMTP (Simple Mail Transfer Protocol). To send emails you will need to supply the following information: Email Addres to Send To Email Addres to be Sent From Email Account Username Email Account Password SMTP Server SMTP Port Number To run this script copy the code below [&hellip;]","og_url":"http:\/\/www.bulis.co.uk\/?page_id=265","og_site_name":"Phantom Raspberry Blower","article_modified_time":"2017-07-24T21:47:47+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.bulis.co.uk\/?page_id=265","url":"http:\/\/www.bulis.co.uk\/?page_id=265","name":"Python: Code Snippets - Send Email (SMTP) - Phantom Raspberry Blower","isPartOf":{"@id":"http:\/\/www.bulis.co.uk\/#website"},"datePublished":"2017-07-22T16:58:18+00:00","dateModified":"2017-07-24T21:47:47+00:00","breadcrumb":{"@id":"http:\/\/www.bulis.co.uk\/?page_id=265#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.bulis.co.uk\/?page_id=265"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.bulis.co.uk\/?page_id=265#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.bulis.co.uk\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=57"},{"@type":"ListItem","position":3,"name":"Python Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=73"},{"@type":"ListItem","position":4,"name":"Python: Code Snippets","item":"http:\/\/www.bulis.co.uk\/?page_id=239"},{"@type":"ListItem","position":5,"name":"Python: Code Snippets &#8211; Send Email (SMTP)"}]},{"@type":"WebSite","@id":"http:\/\/www.bulis.co.uk\/#website","url":"http:\/\/www.bulis.co.uk\/","name":"Phantom Raspberry Blower","description":"Blowing Raspberrys since 1989","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.bulis.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"}]}},"_links":{"self":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/265"}],"collection":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=265"}],"version-history":[{"count":0,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/265\/revisions"}],"up":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/239"}],"wp:attachment":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}