{"id":137968,"date":"2020-07-20T23:17:51","date_gmt":"2020-07-20T15:17:51","guid":{"rendered":"http:\/\/4563.org\/?p=137968"},"modified":"2020-07-20T23:17:51","modified_gmt":"2020-07-20T15:17:51","slug":"python-%e5%8f%91%e9%80%81%e9%82%ae%e4%bb%b6%ef%bc%8c%e9%82%ae%e4%bb%b6%e5%8f%af%e4%bb%a5%e6%ad%a3%e5%b8%b8%e6%8e%a5%e6%94%b6%ef%bc%8c%e4%bd%86%e6%94%b6%e4%bb%b6%e4%ba%ba%e6%a0%8f%e6%98%be%e7%a4%ba-2","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=137968","title":{"rendered":"Python \u53d1\u9001\u90ae\u4ef6\uff0c\u90ae\u4ef6\u53ef\u4ee5\u6b63\u5e38\u63a5\u6536\uff0c\u4f46\u6536\u4ef6\u4eba\u680f\u663e\u793a\u5f02\u5e38"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python \u53d1\u9001\u90ae\u4ef6\uff0c\u90ae\u4ef6\u53ef\u4ee5\u6b63\u5e38\u63a5\u6536\uff0c\u4f46\u6536\u4ef6\u4eba\u680f\u663e\u793a\u5f02\u5e38               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Kvip <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5982\u9898\uff0c\u4f7f\u7528 python \u7f16\u5199\u4e86\u4e2a\u53d1\u9001\u90ae\u4ef6\u7684 Demo \u3002\u90ae\u4ef6\u53d1\u9001\u540e\uff0c\u7528 Foxmail \u80fd\u591f\u6b63\u5e38\u6536\u7684\u5230\u3002<\/p>\n<p>\u4f46\u662f\u6536\u4ef6\u4eba\u4e00\u680f\u663e\u793a\u7684\u6536\u4ef6\u90ae\u7bb1\u5f88\u5947\u602a\uff0c\u5982\u4e0b\u56fe\uff0c\u4e00\u6574\u4e2a\u6536\u4ef6\u90ae\u7bb1\u88ab\u6309\u5b57\u7b26\u62c6\u5206\u4e3a\u597d\u591a\u4e2a\u5c0f\u90ae\u7bb1\uff0c\u5c0f\u90ae\u7bb1\u7684\u90a3\u57df\u540d\u6211\u4e5f\u6ca1\u89c1\u8fc7\u3002<\/p>\n<p>\u539f\u672c\u6536\u4ef6\u90ae\u7bb1\u5e94\u8be5\u663e\u793a\u662f [email&#160;protected]<\/p>\n<p>\u4e0d\u77e5\u9053\u6709\u5927\u4f6c\u9047\u5230\u8fc7\u8fd9\u79cd\u60c5\u51b5\u6ca1\uff0c\u8bf7\u6559\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/07\/20200723_5f19923deb9b5.png\" alt=\"Python \u53d1\u9001\u90ae\u4ef6\uff0c\u90ae\u4ef6\u53ef\u4ee5\u6b63\u5e38\u63a5\u6536\uff0c\u4f46\u6536\u4ef6\u4eba\u680f\u663e\u793a\u5f02\u5e38\" \/><\/p>\n<p>\u4e0b\u9762\u662f\u6e90\u7801\uff08\u770b\u7740\u6ca1\u6bdb\u75c5\uff09\uff1a<\/p>\n<pre><code># \u7f16\u8bd1\u65e5\u671f\uff1a2019-10-14 15:01:36 # coding=utf-8  import os from email.mime.text import MIMEText from email.header import Header from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication  # \u53d1\u9001\u90ae\u4ef6\uff08\u9644\u4ef6\uff09 # email_user: \u53d1\u4ef6\u4eba # email_pass: \u5bc6\u7801 # receivers\uff1a\u6536\u4ef6\u4eba\u5217\u8868\uff0c\u4f8b\uff1a['[email&#160;protected]','[email&#160;protected]'] # filepath: \u6587\u4ef6\u5730\u5740 \u5982\uff1ar'D:\u5206\u884c\u67e5\u8be2\u652f\u884c\u5377\u522b\u5e93\u5b58 Excel\u4f5c\u6587.xlsx' def sendEmail(email_user, email_pass, receivers, filepath):     mail_host = '10.10.10.10' # \u8bbe\u7f6e\u670d\u52a1\u5668\u5730\u5740     sender = email_user + '@xxx.com.cn' # \u53d1\u4ef6\u4eba\u90ae\u7bb1      # \u521b\u5efa\u4e00\u4e2a\u5e26\u9644\u4ef6\u7684\u5b9e\u4f8b     message = MIMEMultipart()          # \u8bbe\u7f6e\u53d1\u4ef6\u4eba\u3001\u6536\u4ef6\u4eba     message['From'] = sender     message['To'] = \", \".join(receivers)     # \u8bbe\u7f6e\u90ae\u4ef6\u4e3b\u9898     message['Subject'] = Header('\u67e5\u51bb\u6263\u62a5\u5907\u6587\u4ef6', 'utf-8')     # \u90ae\u4ef6\u6b63\u6587     message.attach(MIMEText('\u67e5\u51bb\u6263\u62a5\u5907\u6587\u4ef6\u5df2\u53d1\u9001\uff0c\u8bf7\u67e5\u770b\u9644\u4ef6\uff01', 'plain', 'utf-8'))      # \u6784\u9020\u9644\u4ef6     xlsx = MIMEApplication(open(filepath,'rb').read())     xlsx['Content-Type'] = 'application\/octet-stream'     filename = os.path.basename(filepath)     xlsx.add_header('Content-Disposition', 'attachment', filename=Header(filename, 'utf-8').encode())     message.attach(xlsx)      try:         smtpObj = smtplib.SMTP(mail_host, 1025)         smtpObj.login(email_user, email_pass)         smtpObj.sendmail(sender, receivers, message.as_string())         print(\"\u90ae\u4ef6\u53d1\u9001\u6210\u529f\uff0c\u6536\u4ef6\u4eba\uff1a\" + str(receivers) + \"n \u9644\u4ef6\u5730\u5740:\" + str(filepath))     except smtplib.SMTPException as e:         print(\"\u90ae\u4ef6\u53d1\u9001\u5931\u8d25\uff0c\u6536\u4ef6\u4eba\uff1a\" + str(receivers) + \"n \u9644\u4ef6\u5730\u5740:\" + str(filepath))         print(e)  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2604343\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : j0hnj <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             `sendEmail` \u662f\u600e\u4e48\u8c03\u7528\u7684\u5462\uff1f\u6211\u731c\u53ef\u80fd\u662f\u628a `receivers` \u4f20\u6210 str \u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2604344\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             message[&#8216;To&#8217;] = Header(&#8220;,&#8221;.join(receivers), &#8216;utf-8&#8217;)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2604345\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Aliencn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u50cf\u662f\u4e00\u8bf4\u7684\u90a3\u6837\uff0c\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\u6709\u95ee\u9898\u3002<\/p>\n<p>\u53ef\u4ee5\u53c2\u8003\u6211\u4e4b\u524d\u5199\u7684\u4ee3\u7801\uff1a https:\/\/www.aliencn.net\/archives\/272.html                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2604346\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BryceBu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f list \u4e0d\u662f str \u5662                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2604347\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Kvip <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @j0hnj @Aliencn @BryceBu \u611f\u8c22\u51e0\u4f4d\u8001\u54e5\uff0c\u4f60\u4eec\u8bf4\u7684\u662f\u5bf9\u7684\u3002\u6211\u6d4b\u8bd5\u4e86\u4e00\u4e0b\uff0c\u786e\u5b9e\u662f list \u88ab\u4f20\u6210 str \u6240\u5bfc\u81f4\u3002\u4e0d\u8fc7\u8fd9\u70b9\u5012\u633a\u5947\u602a\u7684\uff0c\u4f20\u9519\u5bf9\u8c61\u5c45\u7136\u4e5f\u80fd\u6b63\u5e38\u6536\u5f97\u5230\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2604348\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : julyclyde <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Kvip \u56e0\u4e3a\u5176\u5b9e\u5e76\u6ca1\u6709\u4f20\u9519\u5bf9\u8c61\u554a\u3002sendmail \u51fd\u6570\u7684\u53c2\u6570\u91cc\u9762\uff0c\u53d1\u9001\u76ee\u6807\u662f receivers \u800c\u4e0d\u662f &#8220;, &#8220;.join(receivers)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2604349\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : j0hnj <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Kvip #5 \u51b3\u5b9a\u80fd\u4e0d\u80fd\u6536\u5230\u90ae\u4ef6\u7684\u662f `sendmail` \u7684 `to_addrs` \u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u6709\u7279\u6b8a\u5904\u7406\uff0c\u5982\u679c\u662f str \u7684\u8bdd\uff0c\u4f1a\u53d8\u6210 to_addrs = [to_addrs], \u4ee3\u7801\u5728\u8fd9\u91cc\uff1a<br \/>https:\/\/github.com\/python\/cpython\/blob\/master\/Lib\/smtplib.py#L873-L874<\/p>\n<p>\u800c message[&#8216;To&#8217;] \u662f\u51b3\u5b9a\u4e00\u5c01\u90ae\u4ef6\u91cc\u6536\u4ef6\u4eba\u90a3\u4e00\u5217\u7684\u5185\u5bb9\u7684\uff0c\u8fd9\u91cc\u5176\u5b9e\u662f\u53ef\u4ee5\u968f\u4fbf\u5199\u7684\uff0c\u5e76\u4e0d\u51b3\u5b9a\u90ae\u4ef6\u53d1\u7ed9\u8c01\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python \u53d1\u9001\u90ae\u4ef6\uff0c\u90ae\u4ef6\u53ef\u4ee5\u6b63&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/137968"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=137968"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/137968\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=137968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=137968"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=137968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}