{"id":2879,"date":"2023-12-10T21:44:50","date_gmt":"2023-12-10T13:44:50","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2879"},"modified":"2023-12-10T21:44:50","modified_gmt":"2023-12-10T13:44:50","slug":"2879","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2879.html","title":{"rendered":"\u4f7f\u7528Python\u81ea\u52a8\u53d1\u9001\u90ae\u4ef6"},"content":{"rendered":"
<\/p>\n
SMTP\uff08Simple Mail Transfer Protocol\uff09\u662f\u7b80\u5355\u4f20\u8f93\u534f\u8bae\u3002<\/span><\/p>\n python\u4e2d\u5bf9SMTP\u8fdb\u884c\u4e86\u7b80\u5355\u7684\u5c01\u88c5\uff0c\u53ef\u4ee5\u53d1\u9001\u7eaf\u6587\u672c\u90ae\u4ef6\u3001HTML \u90ae\u4ef6\u4ee5\u53ca\u5e26\u9644\u4ef6\u7684\u90ae\u4ef6\u3002\u4e24\u4e2a\u6838\u5fc3\u6a21\u5757\u5982\u4e0b\uff1a<\/span><\/p>\n email\u6a21\u5757\uff1a\u8d1f\u8d23\u6784\u5efa\u90ae\u4ef6<\/span><\/p>\n smtplib\u6a21\u5757\uff1a\u8d1f\u8d23\u53d1\u9001\u90ae\u4ef6<\/span><\/p>\n \u5e38\u7528\u65b9\u6cd5\u4e0e\u5c5e\u6027: <\/span><\/p>\n <\/span><\/p>\n \u5982\u679c\u8981\u4f7f\u7528<\/span>qq\u90ae\u7bb1\u53d1\u9001\u7684\u8bdd\uff0c\u9700\u8981\u5728qq\u90ae\u7bb1\u8bbe\u7f6e-\u8d26\u6237\u91cc\u9762\u5f00\u542fPOP3\/SMTP\u670d\u52a1<\/span><\/span><\/p>\n <\/span><\/span><\/p>\n <\/span><\/span><\/p>\n \u9996\u6b21\u5f00\u542f\u9700\u8981\u4f60\u7528\u624b\u673a\u53f7\u53d1\u9001\u77ed\u4fe1\uff0c\u7167\u7740\u505a\u5c31\u597d\u4e86\uff0c\u505a\u5b8c\u4e4b\u540e\u4f1a\u7ed9\u4f60\u4e00\u4e2a\u6388\u6743\u7801\uff0c\u8bb0\u7740\u8fd9\u4e2a\u6388\u6743\u7801\uff0c\u5199\u4ee3\u7801\u9700\u8981\u7528\u5230\uff0c\u5fd8\u8bb0\u4e86\u4e5f\u53ef\u4ee5\u627e\u56de<\/p>\n \u63a5\u7740\u7f16\u5199\u4ee3\u7801\uff0c\u53d1\u9001\u666e\u901a\u90ae\u4ef6<\/p>\n \u53d1\u9001HTML\u90ae\u4ef6\u53ea\u9700\u8981\u628a\u53d1\u9001\u7684\u5185\u5bb9\u6362\u6210html\u4ee3\u7801\u5373\u53ef<\/span><\/p>\n \u6709\u65f6\u90ae\u4ef6\u7684\u5185\u5bb9\u53ea\u9760\u6587\u672c\u662f\u65e0\u6cd5\u5b8c\u5168\u63cf\u8ff0\u5185\u5bb9\u3002\u8fd9\u65f6\u5c31\u53ef\u4ee5\u8003\u8651\u53d1 \u9001\u9644\u4ef6\u6765\u643a\u5e26\u5185\u5bb9\u3002\u5177\u4f53\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n \u8bbe\u7f6e\u767b\u5f55\u670d\u52a1\u5668 \u3001\u767b\u5f55\u90ae\u7bb1\u3001\u589e\u52a0\u9644\u4ef6 \u3001\u8bbe\u7f6e\u8bf7\u6c42\u5934\u3001 \u53d1\u9001\u90ae\u4ef6<\/p>\n <\/p>\n \u8fd9\u91cc\u4ee5\u53d1\u9001\u5de5\u8d44\u6761\u4e3a\u4f8b<\/p>\n \u5de5\u8d44\u6570\u636e\u5982\u4e0b\uff1a <\/p>\n <\/p>\n <\/p>\n Zmail\u7684\u4f18\u52bf<\/span><\/p>\n 1 \u81ea\u52a8\u586b\u5145\u5927\u591a\u6570\u5bfc\u81f4\u670d\u52a1\u7aef\u62d2\u4fe1\u7684\u5934\u4fe1\u606f\uff08From To LocalHost\u4e4b\u7c7b\u7684)<\/span><\/p>\n 2 \u5c06\u4e00\u4e2a\u5b57\u5178\u6620\u5c04\u4e3aemail\uff0c\u6784\u9020\u4fe1\u4ef6\u5c31\u50cf\u6784\u9020\u5b57\u5178\u4e00\u6837\u7b80\u5355<\/span><\/p>\n 3 \u81ea\u52a8\u5bfb\u627e\u90ae\u4ef6\u670d\u52a1\u5546\u7aef\u53e3\u53f7\u5730\u5740\uff0c\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u534f\u8bae\uff08\u7ecf\u8fc7\u8ba4\u8bc1\u7684\uff09<\/span><\/p>\n \u5b89\u88c5\uff1apip install zmail<\/span><\/p>\n <\/p>\n \u53d1\u9001\u7684\u6d88\u606f\u4ee5\u5b57\u5178\u53d1\u9001,\u5305\u542b\u7684key\uff1a<\/p>\n subject \u90ae\u4ef6\u4e3b\u9898<\/p>\n from \u53d1\u9001\u4eba<\/p>\n content_text \u90ae\u4ef6\u5185\u5bb9-\u6587\u672c<\/p>\n content_html \u90ae\u4ef6\u5185\u5bb9-HTML<\/p>\n attachments \u90ae\u4ef6\u5185\u5bb9-\u9644\u4ef6\uff0c\u53ef\u5199\u591a\u4e2a <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" SMTP\uff08Simple Mail Transfer Protocol\uff09\u662f\u7b80\u5355\u4f20\u8f93\u534f\u8bae\u3002 pytho […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-2879","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2879"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2879"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2879\/revisions"}],"predecessor-version":[{"id":2880,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2879\/revisions\/2880"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
<\/span><\/p>\n
<\/span><\/p>\n
<\/span><\/p>\n
<\/span><\/p>\nimport smtplib\r\nfrom email.mime.text import MIMEText\r\nfrom email.header import Header\r\n \r\ndef send_email():\r\n # \u8bbe\u7f6e\u8981\u767b\u5f55\u7684\u90ae\u7bb1\r\n smtp_obj = smtplib.SMTP('smtp.qq.com')\r\n # \u767b\u5f55\u90ae\u7bb1 \u8fd9\u91cc\u9700\u8981\u586b\u5199\u4f60\u7684qq\u90ae\u7bb1\u5730\u5740\u548c\u751f\u6210\u7684\u6388\u6743\u7801\r\n smtp_obj.login('xxx@qq.com','spcdwgqkltjsbiah')\r\n # \u7f16\u8f91\u5185\u5bb9\r\n mail_text = 'This is Email~ \u60a8\u8981\u7684\u90ae\u4ef6\u6765\u5566~~'\r\n # plain \u539f\u751f\u6587\u672c\u6a21\u5f0f\r\n msg_body = MIMEText(mail_text,'plain','utf-8')\r\n # \u8bbe\u7f6e\u4ece\u54ea\u53d1\u9001\u7684\r\n msg_body['From'] = Header('xxx','utf-8') # \u8bbe\u7f6e\u53d1\u9001\u4eba\r\n msg_body['Subject'] = Header('\u6d4b\u8bd5Python\u81ea\u52a8\u90ae\u4ef6','utf-8') # \u8bbe\u7f6e\u5185\u5bb9\u4e3b\u9898\r\n # \u53d1\u9001\u90ae\u4ef6 \u8fd9\u91cc\u7b2c\u4e00\u4e2a\u90ae\u7bb1\u586b\u81ea\u5df1\u7684\uff0c\u7b2c\u4e8c\u4e2a\u586b\u6536\u4ef6\u4eba\u7684\u90ae\u7bb1\u5730\u5740\r\n smtp_obj.sendmail('398707160@qq.com','hotelmail@126.com',msg_body.as_string())\r\n \r\nif __name__ =='__main__':\r\n send_email()<\/pre>\n
<\/span><\/span><\/p>\n<\/p>\nimport smtplib\r\nfrom email.mime.text import MIMEText\r\nfrom email.header import Header\r\n \r\ndef send_email():\r\n # \u8bbe\u7f6e\u8981\u767b\u5f55\u7684\u90ae\u7bb1\r\n smtp_obj = smtplib.SMTP('smtp.qq.com')\r\n # \u767b\u5f55\u90ae\u7bb1 \u8fd9\u91cc\u9700\u8981\u586b\u5199\u4f60\u7684qq\u90ae\u7bb1\u5730\u5740\u548c\u751f\u6210\u7684\u6388\u6743\u7801\r\n smtp_obj.login('xxx@qq.com','spcdwgqkltjsbiah')\r\n # \u7f16\u8f91\u5185\u5bb9\r\n mail_text = '''\r\n <h1 style='color:red'>\u8fd9\u662f\u4e00\u4e2aHTML\u90ae\u4ef6\u901a\u77e5<\/h1>\r\n <p>\u8fd9\u4e2a\u662f\u90ae\u4ef6\u7684\u5185\u5bb9<\/p>\r\n <p><a href="'iuuq:\/\/xxx.jucbj{ibo.do'&hu;\v8ge9\v4f2b\v662g\v94gf\v63b5μ\/b&hu;μ\/q&hu;\s\o&octq;&octq;&octq;&octq;'''\s\o&octq;&octq;&octq;&octq;#&octq;qmbjo&octq;\v539g\v751g\v6587\v672d\v6b21\v5g0g\s\o&octq;&octq;&octq;&octq;nth_cpez&octq;=&octq;NJNFUfyu(nbjm_ufyu,'qmbjo','vug-8')\s\o&octq;&octq;&octq;&octq;#&octq;\v8ccf\v7g6f\v4fdf\v54fb\v53e1\v9001\v7684\s\o&octq;&octq;&octq;&octq;nth_cpez['Gspn']&octq;=&octq;Ifbefs('yyy','vug-8')&octq;&octq;#&octq;\v8ccf\v7g6f\v53e1\v9001\v4fcb\s\o&octq;&octq;&octq;&octq;nth_cpez['Tvckfdu']&octq;=&octq;Ifbefs('\v6e4c\v8ce5Qzuipo\v81fb\v52b8\v90bf\v4fg6','vug-8')&octq;&octq;#&octq;\v8ccf\v7g6f\v5185\v5cc9\v4f3c\v9898\s\o&octq;&octq;&octq;&octq;#&octq;\v53e1\v9001\v90bf\v4fg6&octq;&octq;\v8ge9\v91dd\v7c2d\v4f00\v4f2b\v90bf\v7cc1\v586c\v81fb\v5eg1\v7684\vgg0d\v7c2d\v4f8d\v4f2b\v586c\v6536\v4fg6\v4fcb\v7684\v90bf\v7cc1\v5730\v5740\s\o&octq;&octq;&octq;&octq;tnuq_pck.tfoenbjm('398707160@rr.dpn','ipufmnbjm@126.dpn',nth_cpez.bt_tusjoh())\s\o&octq;\s\ojg&octq;__obnf__&octq;=='__nbjo__':\s\o&octq;&octq;&octq;&octq;tfoe_fnbjm()<\/qsf">\n
\u53d1\u9001\u9644\u4ef6\u90ae\u4ef6-smtplib<\/h2>\n
import smtplib\r\nfrom email.mime.text import MIMEText\r\nfrom email.mime.application import MIMEApplication\r\nfrom email.mime.multipart import MIMEMultipart\r\nfrom email.header import Header\r\n \r\ndef send_file_email():\r\n # \u8bbe\u7f6e\u90ae\u7bb1\u670d\u52a1\u5668\r\n stmp_obj = smtplib.SMTP('smtp.qq.com')\r\n # \u767b\u5f55\u90ae\u7bb1\r\n stmp_obj.login('xxx@qq.com','spcdwgqkltjsbiah')\r\n # \u8bbe\u7f6e\u90ae\u4ef6\u5185\u5bb9\r\n # \u6587\u672c\r\n msg_txt = MIMEText('\u8fd9\u4e2a\u5e26\u6709\u9644\u4ef6\u7684\u90ae\u4ef6','plain','utf-8')\r\n # \u9644\u4ef6 \u6253\u5f00\u4f60\u8981\u53d1\u9001\u7684\u6587\u4ef6\r\n msg_file = MIMEApplication(open('.\/base_data\/backg.jpg','rb').read())\r\n msg_file.add_header('Content-Disposition','attachment',filename='bg.jpg')\r\n # \u5c01\u88c5\u8981\u53d1\u9001\u7684\u6570\u636e\r\n part = MIMEMultipart()\r\n part.attach(msg_txt)\r\n part.attach(msg_file)\r\n # \u8bbe\u7f6e\u90ae\u4ef6\u5176\u5b83\u4fe1\u606f\r\n part['From'] = Header('xx','utf-8')\r\n part['Subject'] = Header('\u9644\u4ef6\u90ae\u4ef6','utf-8')\r\n # \u53d1\u9001\u90ae\u4ef6\r\n stmp_obj.sendmail('xxx@qq.com','hotelmail@126.com',part.as_string())\r\n \r\nif __name__ =='__main__':\r\n send_file_email()<\/pre>\n
\u6279\u91cf\u53d1\u9001\u90ae\u4ef6-smtplib<\/h2>\n
from email.header import Header\r\nfrom email.mime.text import MIMEText\r\nimport smtplib\r\n \r\nfrom openpyxl import load_workbook\r\n \r\ndef send_many_mail():\r\n # \u8bbe\u7f6e\u767b\u5f55\u90ae\u7bb1\u670d\u52a1\u5668\r\n smtp_obj = smtplib.SMTP('smtp.qq.com')\r\n # \u767b\u5f55\u90ae\u7bb1\r\n smtp_obj.login('xxx@qq.com','spcdwgqkltjsbiah')\r\n # \u6253\u5f00excel\u6587\u4ef6\r\n wb = load_workbook('.\/base_data\/\u5de5\u8d44\u6570\u636e.xlsx',data_only=True)\r\n # \u6fc0\u6d3b\u5de5\u4f5c\u7c3f\r\n sh = wb.active\r\n # \u8bfb\u53d6\u6570\u636e-\u904d\u5386\r\n for i,r in enumerate(sh.iter_rows()):\r\n if i != 0:\r\n # \u7f16\u8f91\u5185\u5bb9\r\n msg_txt = f'''\r\n <h3>\u60a8\u597d:{r[1].value}<\/h3>\r\n <p>\u8bf7\u67e5\u65362030\u5e7412\u6708\u5de5\u8d44\u6761\u8be6\u60c5\uff1a<\/p>\r\n <table border="1">\r\n <tr>\r\n <td>\u5de5\u53f7<\/td>\r\n <td>\u59d3\u540d<\/td>\r\n <td>\u90e8\u95e8<\/td>\r\n <td>\u57fa\u672c\u5de5\u8d44<\/td>\r\n <td>\u63d0\u6210<\/td>\r\n <td>\u52a0\u73ed\u5de5\u8d44<\/td>\r\n <td>\u793e\u4fdd\u6263\u9664<\/td>\r\n <td>\u8003\u52e4\u6263\u9664<\/td>\r\n <td>\u5e94\u53d1\u5de5\u8d44<\/td>\r\n <td>\u90ae\u7bb1<\/td>\r\n <\/tr>\r\n <tr>\r\n <td>{r[0].value}<\/td>\r\n <td>{r[1].value}<\/td>\r\n <td>{r[2].value}<\/td>\r\n <td>{r[3].value}<\/td>\r\n <td>{r[4].value}<\/td>\r\n <td>{r[5].value}<\/td>\r\n <td>{r[6].value}<\/td>\r\n <td>{r[7].value}<\/td>\r\n <td>{r[8].value}<\/td>\r\n <td>{r[9].value}<\/td>\r\n <\/tr>\r\n <\/table>\r\n '''\r\n msg = MIMEText(msg_txt,'html','utf-8')\r\n # \u8bbe\u7f6e\u90ae\u4ef6\u5176\u4ed6\u4fe1\u606f\r\n msg['From'] = Header('\u8d22\u52a1\u90e8','utf-8')\r\n msg['Subject'] = Header('\u5de5\u8d44\u6761','utf-8')\r\n #\u53d1\u9001\u90ae\u4ef6\r\n smtp_obj.sendmail('xxx@qq.com',{r[9].value},msg.as_string())\r\n print(f'{r[1].value} \u5de5\u8d44\u6761\u53d1\u9001\u6210\u529f\uff01\uff01')\r\nif __name__ =='__main__':\r\n send_many_mail()<\/pre>\n
\u53d1\u9001\u90ae\u4ef6-zmail<\/h2>\n
<\/span><\/p>\n
<\/span><\/p>\n
<\/span><\/p>\n
<\/span><\/p>\nimport zmail\r\ndef send_text():\r\n # \u767b\u5f55\u90ae\u7bb1\r\n server = zmail.server('xxx@qq.com','spcdwgqkltjsbiah')\r\n # \u7f16\u5199\u5185\u5bb9\r\n info = {\r\n 'from':'\u98de\u626c',\r\n 'subject':'\u6d4b\u8bd5zmail\u6a21\u5757',\r\n 'content_text':'\u8fd9\u4e2a\u662fzmail\u90ae\u4ef6\u4fe1\u606f'\r\n }\r\n # \u53d1\u9001\u90ae\u4ef6\r\n server.send_mail('feiyang@126.com',info)\r\ndef send_html():\r\n # \u767b\u5f55\u90ae\u7bb1\r\n server = zmail.server('xxx@qq.com','spcdwgqkltjsbiah')\r\n # \u7f16\u5199\u5185\u5bb9\r\n info = {\r\n 'from':'\u98de\u626c',\r\n 'subject':'\u6d4b\u8bd5zmail\u6a21\u5757',\r\n 'content_html':'<h1>\u8fd9\u4e2a\u662fzmail\u90ae\u4ef6\u4fe1\u606f<\/h1>'\r\n }\r\n # \u53d1\u9001\u90ae\u4ef6\r\n server.send_mail('hotelmail@126.com',info)\r\ndef send_file():\r\n # \u767b\u5f55\u90ae\u7bb1\r\n server = zmail.server('xxx@qq.com','spcdwgqkltjsbiah')\r\n # \u7f16\u5199\u5185\u5bb9\r\n info = {\r\n 'from':'\u98de\u626c',\r\n 'subject':'\u6d4b\u8bd5zmail\u6a21\u5757',\r\n 'content_html':'<h1>\u8fd9\u4e2a\u662fzmail\u90ae\u4ef6\u4fe1\u606f<\/h1>',\r\n 'attachments':[r'.\\base_data\\backg.jpg']\r\n }\r\n # \u53d1\u9001\u90ae\u4ef6\r\n server.send_mail('feiyang@126.com',info)\r\ndef get_email():\r\n # \u767b\u5f55\u90ae\u7bb1\r\n server = zmail.server('xxx@qq.com','tzteewnmyfqacbce')\r\n email = server.get_latest()\r\n print(email.get('subject'))\r\n print(email.get('from'))\r\n print(email.get('content_html'))\r\n print(email.get('content_text'))\r\n \r\nif __name__ == '__main__':\r\n # send_text()\r\n # send_html()\r\n # send_file()\r\n get_email()<\/pre>\n