{"id":6240,"date":"2021-02-07T21:55:51","date_gmt":"2021-02-07T13:55:51","guid":{"rendered":"https:\/\/vip.fxxkpython.com\/?p=6240"},"modified":"2021-02-07T21:57:15","modified_gmt":"2021-02-07T13:57:15","slug":"%e7%94%a8-python-%e5%8f%91%e4%b8%80%e6%b3%a2%e9%82%ae%e4%bb%b6%ef%bc%8c%e6%95%88%e6%9e%9c%e6%9d%a0%e6%9d%a0%e6%bb%b4","status":"publish","type":"post","link":"https:\/\/vip.fxxkpython.com\/?p=6240","title":{"rendered":"\u7528 Python \u53d1\u4e00\u6ce2\u90ae\u4ef6\uff0c\u6548\u679c\u6760\u6760\u6ef4"},"content":{"rendered":"\n<p>\u6709\u4e9b\u9879\u76ee\u53ef\u80fd\u9700\u8981\u6211\u4eec\u901a\u8fc7\u4ee3\u7801\u7684\u65b9\u5f0f\u6765\u53d1\u9001\u90ae\u4ef6\uff0c\u6bd4\u5982\u5728\u540e\u53f0\u7ed9\u5ba2\u6237\u6279\u91cf\u53d1\u9001\u90ae\u4ef6\uff0c\u6216\u8005\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u7ed9\u81ea\u5df1\u53d1\u9001\u63d0\u9192\u90ae\u4ef6\uff0c\u6bd4\u5982\u89e6\u53d1\u4e86\u67d0\u4e9b\u4e8b\u4ef6\u9700\u8981\u6211\u4eec\u4eb2\u81ea\u53bb\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7801\u76f4\u63a5\u7ed9\u81ea\u5df1\u53d1\u9001\u4e00\u5c01\u9884\u8b66\u90ae\u4ef6\u7b49\u7b49\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u4f7f\u7528 Python \u6765\u53d1\u9001\u90ae\u4ef6\u5462\uff1f\u5728\u6b64\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u7b80\u5355\u7684\u4e86\u89e3\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<h2>\u4ec0\u4e48\u662f SMTP\uff1f<\/h2>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\u5728\u4e92\u8054\u7f51\u4e2d\u5e38\u7528\u7684\u534f\u8bae\u662f HTTP\uff0c\u8fd9\u6837\u6211\u4eec\u7684\u6d4f\u89c8\u5668\u624d\u80fd\u83b7\u53d6\u5230\u670d\u52a1\u5668\u7684\u7f51\u9875\u5185\u5bb9\uff0c\u540c\u6837\u7684 SMTP \u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u4e92\u8054\u7f51\u5e94\u7528\u5c42\u4f20\u8f93\u534f\u8bae\uff0c\u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u79cd\u7c7b\u578b\u662f\u4e13\u95e8\u7528\u6765\u5904\u7406\u90ae\u4ef6\u7684\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4f60\u5728\u53d1\u9001\u90ae\u4ef6\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u662f\u76f4\u63a5\u5c31\u628a\u4f60\u5199\u7684\u5185\u5bb9\u53d1\u9001\u5230\u5bf9\u65b9\u7684\u6536\u4ef6\u7bb1\u4e2d\uff0c\u5b83\u4f1a\u5148\u53d1\u9001\u7ed9 smtp \u670d\u52a1\u5668\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540e\u53d1\u9001\u5230\u5bf9\u5e94\u7684\u670d\u52a1\u5668\u4e2d\uff0c\u6bd4\u5982 Gmail\uff0c\u7136\u540e Gmail \u5c31\u4f1a\u4e0b\u8f7d\u90ae\u4ef6\uff0c\u628a\u5b83\u653e\u5230\u6536\u4ef6\u4eba\u7684\u90ae\u7bb1\u91cc\u9762\u53bb\u3002<\/p>\n\n\n\n<p>\u5728 Python \u4e2d\u6709\u4e00\u4e2a smtpd \u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u5728\u672c\u5730\u5f00\u542f\u4e00\u4e2a SMTP \u670d\u52a1\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n\n\n\n<p>python -m smtpd -c DebuggingServer -n localhost:1088<\/p>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u672c\u5730\u6d4b\u8bd5\u90ae\u4ef6\u7684\u53d1\u9001\u4e86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-ad51283487316c8c361b4c5a1627e8cb_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5f53\u7136\uff0c\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u81ea\u5df1\u521b\u5efa SMTP \u670d\u52a1\uff0c\u4e0d\u540c\u7684\u90ae\u4ef6\u5382\u5546\u90fd\u6709\u63d0\u4f9b\u5b83\u4eec\u7684 STMP \u670d\u52a1\u5730\u5740\uff0c\u6bd4\u5982 Gmail \u7684\u5c31\u662f smtp.gmail.com:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic2.zhimg.com\/80\/v2-51ce0839bb1bbf586ec61f9e4ca02bc9_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2>Python \u53d1\u9001\u90ae\u4ef6<\/h2>\n\n\n\n<p>\u77e5\u9053\u4e86\u4ec0\u4e48\u662f smtp \u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 Python \u6765\u53d1\u9001\u90ae\u4ef6\u4e86\u3002\u5728 Python \u4e2d\u6709\u4e00\u4e2a\u5185\u7f6e\u7684 smtplib \u5e93\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8fde\u63a5 smtp \u670d\u52a1\u5668\uff0c\u50cf\u521a\u521a\u5c0f\u5e05b\u7ed9\u4f60\u770b\u5230\u7684\u672c\u5730\u6d4b\u8bd5\uff0c\u53d1\u9001\u4fe1\u606f\u7ed9 stmp \u670d\u52a1\u5c31\u662f\u8fd9\u6837\u64cd\u4f5c\u7684\uff1a<\/p>\n\n\n\n<p>smtpObj=smtplib.SMTP(host=&#8217;localhost&#8217;,port=1088 smtpObj.sendmail(sender_mail,receivers_mail, message)<\/p>\n\n\n\n<p>\u6211\u4eec\u5728\u53d1\u9001\u90ae\u4ef6\u7684\u65f6\u5019\uff0c\u662f\u6709\u4e00\u5b9a\u7684\u6807\u51c6\u7684\uff0c\u5b83\u600e\u4e48\u53bb\u533a\u5206\u8981\u53d1\u7684\u5185\u5bb9\u662f\u7eaf\u6587\u672c\uff0c\u8fd8\u662f\u542b\u6709 HTML\uff0c\u6216\u8005\u662f\u9644\u4ef6\u5462\uff0c\u6240\u4ee5\u6709\u4e00\u4e2a MIME \u7684\u6807\u51c6\uff0c\u901a\u8fc7\u8fd9\u4e2a\u6807\u51c6\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u53d1\u9001\u4e0d\u540c\u7684\u90ae\u4ef6\u7c7b\u578b\u3002\u5728 Python \u91cc\u9762\u6709\u4e00\u4e2a email \u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6784\u9020 MIME massgae\uff0c\u7136\u540e\u901a\u8fc7 smtplib \u6765\u53d1\u9001\u3002<\/p>\n\n\n\n<p><strong>from<\/strong> email.mime.multipart<\/p>\n\n\n\n<p> <strong>import<\/strong> MIMEMultipart<\/p>\n\n\n\n<p> <strong>from<\/strong> email.mime.text <strong>import<\/strong> MIMEText<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6765\u4f7f\u7528 Python \u5728 Gmail \u7ed9\u522b\u4eba\u53d1\u9001\u4e00\u4e2a\u300c\u5e05b\u8001\u4ed9\u6765\u5566\u300d\u7684\u7eaf\u6587\u672c\u90ae\u4ef6\u8bd5\u8bd5\u5427\u3002\u56e0\u4e3a\u6211\u4eec\u8981\u4f7f\u7528\u7684\u662f Gmail \uff0c\u6240\u4ee5\u6709\u4e24\u4e2a\u70b9\u9700\u8981\u5148\u6ce8\u610f\u4e00\u4e0b\uff0c\u7b2c\u4e00\u4e2a\u4f60\u61c2\u7684\uff0c\u9700\u8981\u505a\u4e00\u4e0b\u4ee3\u7406\uff0c\u4f60\u53ef\u4ee5\u5148\u5b89\u88c5\u4e00\u6ce2 PySocks\uff0c\u7136\u540e\u8fd9\u6837\u6765\u8ba9 smtp \u4f7f\u7528 socks \u4ee3\u7406\uff1a<\/p>\n\n\n\n<p><strong>importsockssocks<\/strong>.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, &#8216;127.0.0.1&#8217;, 1080) <\/p>\n\n\n\n<p><strong>socks<\/strong>.wrapmodule(smtplib)<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u4e2a\u662f Google \u4e3a\u4e86\u4f60\u7684\u5b89\u5168\uff0c\u4e0d\u4f1a\u8f7b\u6613\u8ba9\u4f60\u5728\u522b\u7684\u5730\u65b9\u767b\u9646\u4f60\u7684 Gmail \u8d26\u53f7\uff0c\u6240\u4ee5\u4f60\u9700\u8981\u5728\u8bbe\u7f6e\u90a3\u91cc\uff0c\u628a\u4f4e\u5b89\u5168\u6027\u5e94\u7528\u8bbf\u95ee\u6743\u9650\u6253\u5f00\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic2.zhimg.com\/80\/v2-537fe7f08bd79aa32422744a50fc7b34_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2>Python \u53d1\u9001\u7eaf\u6587\u672c\u90ae\u4ef6\u5185\u5bb9<\/h2>\n\n\n\n<p>\u597d\u4e86\uff0c\u5bfc\u5165\u76f8\u5173\u7684\u5e93\u5e76\u5f00\u542f\u4ee3\u7406\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic2.zhimg.com\/80\/v2-6cbf49e36877c30e1261558464b2ea79_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5b9a\u4e49\u597d\u4f60\u7684\u6536\u53d1\u90ae\u4ef6\u5730\u5740\uff0c\u4ee5\u53ca\u4f60\u8981\u53d1\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-280d8db36284ae24442901d1962787b3_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u6784\u9020 MIME\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic4.zhimg.com\/80\/v2-1825c1966b482463c7d88c472f99f8a0_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u8fde\u63a5 Gamil \u7684 smtp \u670d\u52a1\uff0c\u5f00\u542f tls \u52a0\u5bc6\uff0c\u767b\u9646\u7136\u540e\u53d1\u9001\u4e00\u6ce2\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic3.zhimg.com\/80\/v2-12c29e929e062ca47698430c85f373cf_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u8fd0\u884c\u4e00\u6ce2\uff0c\u6ca1\u4ec0\u4e48\u95ee\u9898\u7684\u8bdd\u5bf9\u65b9\u5c31\u4f1a\u6536\u5230\u4f60\u53d1\u7684\u90ae\u4ef6\u5566\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-8fc828deb0026b70afa0eb22f638f95f_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2>Python \u53d1\u9001 HTML \u90ae\u4ef6\u5185\u5bb9<\/h2>\n\n\n\n<p>\u521a\u521a\uff0c\u6211\u4eec\u8fd9\u91cc \u300cMIMEText(content, &#8216;plain&#8217;, &#8216;utf-8&#8217;)\u300d\u4e2d\u6307\u5b9a\u7684\u662f plain\uff0c\u8bf4\u660e\u90ae\u4ef6\u662f\u7eaf\u6587\u672c\uff0c\u90a3\u4e48\u5982\u679c\u6211\u4eec\u8981\u53d1\u9001\u7684\u90ae\u4ef6\u5185\u5bb9\u662f HTML \u5462\uff1f\u6211\u4eec\u53ea\u9700\u8981\u5b9a\u4e49 HTML \u8bed\u8a00\uff0c\u7136\u540e\u5728 MIMEText \u4f20\u5165 html \u5c31\u53ef\u4ee5\u4e86\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic4.zhimg.com\/80\/v2-26f60bccedd45b12335fedcdb1dc6944_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic2.zhimg.com\/80\/v2-c849679bd8e8bb8ec500cbdae61564fc_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u8fd9\u6837\u6536\u5230\u7684\u90ae\u4ef6\u5c31\u662f HTML \u7684\u5185\u5bb9\u4e86\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic2.zhimg.com\/80\/v2-aeec769bb3ad6366184e128cf00b6411_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2>\u4e00\u884c\u4ee3\u7801\u53d1\u9001\u90ae\u4ef6<\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u548c\u6211\u4e00\u6837\u7528\u7684\u662f Gmail\uff0c\u90a3\u6211\u8fd8\u8981\u544a\u8bc9\u4f60\u4e00\u4e2a\u4e0d\u9519\u7684\u4e1c\u897f\uff0c\u6709\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\u5c01\u88c5\u597d\u4e86\u7684 Python \u90ae\u4ef6\u53d1\u9001\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u90ae\u4ef6\u7684\u53d1\u9001\u3002\u5b89\u88c5:pip install yagmail\u53d1\u9001\uff1ayagmail.SMTP(me,password).send(to, &#8216;\u5e05b\u6765\u5566\uff01\uff01&#8217;, &#8216;\u6765\u4e86\u6765\u4e86&#8217;)\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-d00c3746c3de1271987cf70b8b83253e_1440w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b83\u5feb\u901f\u5b9e\u73b0\u6279\u91cf\u53d1\u9001\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic4.zhimg.com\/80\/v2-7e7de9b19fdbcd13c35b6715ccac0eed_1440w.webp\" alt=\"\"\/><\/figure>\n\n\n\n<p>ok\uff0c\u4ee5\u4e0a\u5c31\u662f\u5c0f\u5e05b\u4eca\u5929\u7ed9\u4f60\u5e26\u6765\u7684\u5206\u4eab\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u5e2e\u52a9\uff0c\u90a3\u4e48\u6211\u4eec\u4e0b\u56de\u89c1\uff0cpeace<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u4e9b\u9879\u76ee\u53ef\u80fd\u9700\u8981\u6211\u4eec\u901a\u8fc7\u4ee3\u7801\u7684\u65b9\u5f0f\u6765\u53d1\u9001\u90ae\u4ef6\uff0c\u6bd4\u5982\u5728\u540e\u53f0\u7ed9\u5ba2\u6237\u6279\u91cf\u53d1\u9001\u90ae\u4ef6\uff0c\u6216\u8005\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u7ed9\u81ea\u5df1\u53d1\u9001\u63d0\u9192\u90ae\u4ef6\uff0c\u6bd4\u5982\u89e6\u53d1\u4e86\u67d0\u4e9b\u4e8b\u4ef6\u9700\u8981\u6211\u4eec\u4eb2\u81ea\u53bb\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7801\u76f4\u63a5\u7ed9\u81ea\u5df1\u53d1\u9001\u4e00\u5c01\u9884\u8b66\u90ae\u4ef6\u7b49\u7b49\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u4f7f\u7528 Python \u6765\u53d1\u9001\u90ae\u4ef6&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[303],"tags":[],"_links":{"self":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6240"}],"collection":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6240"}],"version-history":[{"count":2,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6240\/revisions"}],"predecessor-version":[{"id":6242,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6240\/revisions\/6242"}],"wp:attachment":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}