{"id":138,"date":"2016-03-21T10:18:28","date_gmt":"2016-03-21T02:18:28","guid":{"rendered":"http:\/\/www.shenmdyw.com\/?p=138"},"modified":"2016-03-21T10:19:09","modified_gmt":"2016-03-21T02:19:09","slug":"python-sendmail","status":"publish","type":"post","link":"http:\/\/www.shenmdyw.com\/index.php\/2016\/03\/21\/python-sendmail\/","title":{"rendered":"liunx \u53d1\u9001\u90ae\u4ef6 python sendmail"},"content":{"rendered":"<p>python\u4f7f\u7528liunx\u53d1\u9001sendmail ,\u9996\u5148\u8981\u914d\u7f6eliunx sendmail\u53d1\u90ae\u4ef6\u7cfb\u7edf<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\n#!\/usr\/bin\/env python3\r\n\r\nimport smtplib\r\n\r\nfrom email.mime.multipart import MIMEMultipart\r\nfrom email.mime.text import MIMEText\r\n\r\n# me == my email address\r\n# you == recipient's email address\r\nme = \"my@email.com\"\r\nyou = \"your@email.com\"\r\n\r\n# Create message container - the correct MIME type is multipart\/alternative.\r\nmsg = MIMEMultipart('alternative')\r\nmsg['Subject'] = \"Link\"\r\nmsg['From'] = me\r\nmsg['To'] = you\r\n\r\n# Create the body of the message (a plain-text and an HTML version).\r\ntext = \"Hi!\\nHow are you?\\nHere is the link you wanted:\\nhttps:\/\/www.python.org\"\r\nhtml = \"\"\"\\\r\n<html>\r\n  <head><\/head>\r\n  <body>\r\n    <p>Hi!<br>\r\n       How are you?<br>\r\n       Here is the <a href=\"https:\/\/www.python.org\">link<\/a> you wanted.\r\n    <\/p>\r\n  <\/body>\r\n<\/html>\r\n\"\"\"\r\n\r\n# Record the MIME types of both parts - text\/plain and text\/html.\r\npart1 = MIMEText(text, 'plain')\r\npart2 = MIMEText(html, 'html')\r\n\r\n# Attach parts into message container.\r\n# According to RFC 2046, the last part of a multipart message, in this case\r\n# the HTML message, is best and preferred.\r\nmsg.attach(part1)\r\nmsg.attach(part2)\r\n\r\n# Send the message via local SMTP server.\r\ns = smtplib.SMTP('localhost')\r\n# sendmail function takes 3 arguments: sender's address, recipient's address\r\n# and message to send - here it is sent as one string.\r\ns.sendmail(me, you, msg.as_string())\r\ns.quit()\r\n<\/pre>\n<p>python: https:\/\/docs.python.org\/3\/library\/email-examples.html#email-examples<\/p>\n","protected":false},"excerpt":{"rendered":"<p>python\u4f7f\u7528liunx\u53d1\u9001sendmail ,\u9996\u5148\u8981\u914d\u7f6eliunx sendmail\u53d1\u90ae\u4ef6\u7cfb\u7edf #!\/us [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[76],"class_list":["post-138","post","type-post","status-publish","format-standard","hentry","category-python","tag-python-sendmail","clear"],"_links":{"self":[{"href":"http:\/\/www.shenmdyw.com\/index.php\/wp-json\/wp\/v2\/posts\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.shenmdyw.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.shenmdyw.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.shenmdyw.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.shenmdyw.com\/index.php\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":1,"href":"http:\/\/www.shenmdyw.com\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":139,"href":"http:\/\/www.shenmdyw.com\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions\/139"}],"wp:attachment":[{"href":"http:\/\/www.shenmdyw.com\/index.php\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.shenmdyw.com\/index.php\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.shenmdyw.com\/index.php\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}