{"id":167051,"date":"2020-01-01T10:01:20","date_gmt":"2020-01-01T02:01:20","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=167051"},"modified":"2019-12-23T11:03:26","modified_gmt":"2019-12-23T03:03:26","slug":"python-send-email-2","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-send-email-2.html","title":{"rendered":"Python \u53d1\u9001\u5e26\u9644\u4ef6\u7684\u90ae\u4ef6"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u53d1\u9001\u5e26\u9644\u4ef6\u7684\u90ae\u4ef6\uff0c\u9996\u5148\u8981\u521b\u5efaMIMEMultipart()\u5b9e\u4f8b\uff0c\u7136\u540e\u6784\u9020\u9644\u4ef6\uff0c\u5982\u679c\u6709\u591a\u4e2a\u9644\u4ef6\uff0c\u53ef\u4f9d\u6b21\u6784\u9020\uff0c\u6700\u540e\u5229\u7528smtplib.smtp\u53d1\u9001\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u5b9e\u4f8b\r\n#!\/usr\/bin\/python3\r\n\r\nimport smtplib\r\nfrom email.mime.text import MIMEText\r\nfrom email.mime.multipart import MIMEMultipart\r\nfrom email.header import Header\r\n\r\nsender = 'from@runoob.com'\r\nreceivers = ['429240967@qq.com'] # \u63a5\u6536\u90ae\u4ef6\uff0c\u53ef\u8bbe\u7f6e\u4e3a\u4f60\u7684QQ\u90ae\u7bb1\u6216\u8005\u5176\u4ed6\u90ae\u7bb1\r\n\r\n#\u521b\u5efa\u4e00\u4e2a\u5e26\u9644\u4ef6\u7684\u5b9e\u4f8b\r\nmessage = MIMEMultipart()\r\nmessage['From'] = Header(\"\u83dc\u9e1f\u6559\u7a0b\", 'utf-8')\r\nmessage['To'] = Header(\"\u6d4b\u8bd5\", 'utf-8')\r\nsubject = 'Python SMTP \u90ae\u4ef6\u6d4b\u8bd5'\r\nmessage['Subject'] = Header(subject, 'utf-8')\r\n\r\n#\u90ae\u4ef6\u6b63\u6587\u5185\u5bb9\r\nmessage.attach(MIMEText('\u8fd9\u662f\u83dc\u9e1f\u6559\u7a0bPython \u90ae\u4ef6\u53d1\u9001\u6d4b\u8bd5\u2026\u2026', 'plain', 'utf-8'))\r\n\r\n# \u6784\u9020\u9644\u4ef61\uff0c\u4f20\u9001\u5f53\u524d\u76ee\u5f55\u4e0b\u7684 test.txt \u6587\u4ef6\r\natt1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')\r\natt1[\"Content-Type\"] = 'application\/octet-stream'\r\n# \u8fd9\u91cc\u7684filename\u53ef\u4ee5\u4efb\u610f\u5199\uff0c\u5199\u4ec0\u4e48\u540d\u5b57\uff0c\u90ae\u4ef6\u4e2d\u663e\u793a\u4ec0\u4e48\u540d\u5b57\r\natt1[\"Content-Disposition\"] = 'attachment; filename=\"test.txt\"'\r\nmessage.attach(att1)\r\n\r\n# \u6784\u9020\u9644\u4ef62\uff0c\u4f20\u9001\u5f53\u524d\u76ee\u5f55\u4e0b\u7684 runoob.txt \u6587\u4ef6\r\natt2 = MIMEText(open('runoob.txt', 'rb').read(), 'base64', 'utf-8')\r\natt2[\"Content-Type\"] = 'application\/octet-stream'\r\natt2[\"Content-Disposition\"] = 'attachment; filename=\"runoob.txt\"'\r\nmessage.attach(att2)\r\n\r\ntry:\r\nsmtpObj = smtplib.SMTP('localhost')\r\nsmtpObj.sendmail(sender, receivers, message.as_string())\r\nprint (\"\u90ae\u4ef6\u53d1\u9001\u6210\u529f\")\r\nexcept smtplib.SMTPException:\r\nprint (\"Error: \u65e0\u6cd5\u53d1\u9001\u90ae\u4ef6\")<\/pre>\n
$ python3 test.py\r\n\u90ae\u4ef6\u53d1\u9001\u6210\u529f<\/pre>\n

\u67e5\u770b\u6211\u4eec\u7684\u6536\u4ef6\u7bb1(\u4e00\u822c\u5728\u5783\u573e\u7bb1)\uff0c\u5c31\u53ef\u4ee5\u67e5\u770b\u5230\u90ae\u4ef6\u4fe1\u606f\uff1a
\n\"\"<\/p>\n

\u5728 HTML \u6587\u672c\u4e2d\u6dfb\u52a0\u56fe\u7247<\/strong><\/span><\/div>\n

\u90ae\u4ef6\u7684 HTML \u6587\u672c\u4e2d\u4e00\u822c\u90ae\u4ef6\u670d\u52a1\u5546\u6dfb\u52a0\u5916\u94fe\u662f\u65e0\u6548\u7684\uff0c\u6b63\u786e\u6dfb\u52a0\u56fe\u7247\u7684\u5b9e\u4f8b\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\u5b9e\u4f8b\r\n#!\/usr\/bin\/python3\r\n\r\nimport smtplib\r\nfrom email.mime.image import MIMEImage\r\nfrom email.mime.multipart import MIMEMultipart\r\nfrom email.mime.text import MIMEText\r\nfrom email.header import Header\r\n\r\nsender = 'from@runoob.com'\r\nreceivers = ['429240967@qq.com'] # \u63a5\u6536\u90ae\u4ef6\uff0c\u53ef\u8bbe\u7f6e\u4e3a\u4f60\u7684QQ\u90ae\u7bb1\u6216\u8005\u5176\u4ed6\u90ae\u7bb1\r\n\r\nmsgRoot = MIMEMultipart('related')\r\nmsgRoot['From'] = Header(\"\u83dc\u9e1f\u6559\u7a0b\", 'utf-8')\r\nmsgRoot['To'] = Header(\"\u6d4b\u8bd5\", 'utf-8')\r\nsubject = 'Python SMTP \u90ae\u4ef6\u6d4b\u8bd5'\r\nmsgRoot['Subject'] = Header(subject, 'utf-8')\r\n\r\nmsgAlternative = MIMEMultipart('alternative')\r\nmsgRoot.attach(msgAlternative)\r\n\r\nmail_msg = \"\"\"\r\n\r\nPython \u90ae\u4ef6\u53d1\u9001\u6d4b\u8bd5...\r\n\r\n\r\n\"\"\"\r\nmsgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))\r\n\r\n# \u6307\u5b9a\u56fe\u7247\u4e3a\u5f53\u524d\u76ee\u5f55\r\nfp = open('test.png', 'rb')\r\nmsgImage = MIMEImage(fp.read())\r\nfp.close()\r\n\r\n# \u5b9a\u4e49\u56fe\u7247 ID\uff0c\u5728 HTML \u6587\u672c\u4e2d\u5f15\u7528\r\nmsgImage.add_header('Content-ID', '')\r\nmsgRoot.attach(msgImage)\r\n\r\ntry:\r\nsmtpObj = smtplib.SMTP('localhost')\r\nsmtpObj.sendmail(sender, receivers, msgRoot.as_string())\r\nprint (\"\u90ae\u4ef6\u53d1\u9001\u6210\u529f\")\r\nexcept smtplib.SMTPException:\r\nprint (\"Error: \u65e0\u6cd5\u53d1\u9001\u90ae\u4ef6\")<\/pre>\n
$ python3 test.py\r\n\u90ae\u4ef6\u53d1\u9001\u6210\u529f<\/pre>\n

\u67e5\u770b\u6211\u4eec\u7684\u6536\u4ef6\u7bb1(\u5982\u679c\u5728\u5783\u573e\u7bb1\u53ef\u80fd\u9700\u8981\u79fb\u52a8\u5230\u6536\u4ef6\u7bb1\u624d\u53ef\u6b63\u5e38\u663e\u793a)\uff0c\u5c31\u53ef\u4ee5\u67e5\u770b\u5230\u90ae\u4ef6\u4fe1\u606f\uff1a
\n\"\"<\/p>\n

\u4f7f\u7528\u7b2c\u4e09\u65b9 SMTP \u670d\u52a1\u53d1\u9001<\/strong><\/span><\/div>\n

\u8fd9\u91cc\u4f7f\u7528\u4e86 QQ \u90ae\u7bb1(\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 163\uff0cGmail\u7b49)\u7684 SMTP \u670d\u52a1\uff0c\u9700\u8981\u505a\u4ee5\u4e0b\u914d\u7f6e\uff1a
\n\"\"
\n\"\"
\nQQ \u90ae\u7bb1 SMTP \u670d\u52a1\u5668\u5730\u5740\uff1asmtp.qq.com\uff0cssl \u7aef\u53e3\uff1a465\u3002<\/p>\n

\u4ee5\u4e0b\u5b9e\u4f8b\u4f60\u9700\u8981\u4fee\u6539\uff1a\u53d1\u4ef6\u4eba\u90ae\u7bb1\uff08\u4f60\u7684QQ\u90ae\u7bb1\uff09\uff0c\u5bc6\u7801\uff0c\u6536\u4ef6\u4eba\u90ae\u7bb1\uff08\u53ef\u53d1\u7ed9\u81ea\u5df1\uff09\u3002<\/p>\n

QQ SMTP\r\n#!\/usr\/bin\/python3\r\n\r\nimport smtplib\r\nfrom email.mime.text import MIMEText\r\nfrom email.utils import formataddr\r\n\r\nmy_sender='429240967@qq.com' # \u53d1\u4ef6\u4eba\u90ae\u7bb1\u8d26\u53f7\r\nmy_pass = 'xxxxxxxxxx' # \u53d1\u4ef6\u4eba\u90ae\u7bb1\u5bc6\u7801\r\nmy_user='429240967@qq.com' # \u6536\u4ef6\u4eba\u90ae\u7bb1\u8d26\u53f7\uff0c\u6211\u8fd9\u8fb9\u53d1\u9001\u7ed9\u81ea\u5df1\r\ndef mail():\r\nret=True\r\ntry:\r\nmsg=MIMEText('\u586b\u5199\u90ae\u4ef6\u5185\u5bb9','plain','utf-8')\r\nmsg['From']=formataddr([\"FromRunoob\",my_sender]) # \u62ec\u53f7\u91cc\u7684\u5bf9\u5e94\u53d1\u4ef6\u4eba\u90ae\u7bb1\u6635\u79f0\u3001\u53d1\u4ef6\u4eba\u90ae\u7bb1\u8d26\u53f7\r\nmsg['To']=formataddr([\"FK\",my_user]) # \u62ec\u53f7\u91cc\u7684\u5bf9\u5e94\u6536\u4ef6\u4eba\u90ae\u7bb1\u6635\u79f0\u3001\u6536\u4ef6\u4eba\u90ae\u7bb1\u8d26\u53f7\r\nmsg['Subject']=\"\u83dc\u9e1f\u6559\u7a0b\u53d1\u9001\u90ae\u4ef6\u6d4b\u8bd5\" # \u90ae\u4ef6\u7684\u4e3b\u9898\uff0c\u4e5f\u53ef\u4ee5\u8bf4\u662f\u6807\u9898\r\n\r\nserver=smtplib.SMTP_SSL(\"smtp.qq.com\", 465) # \u53d1\u4ef6\u4eba\u90ae\u7bb1\u4e2d\u7684SMTP\u670d\u52a1\u5668\uff0c\u7aef\u53e3\u662f25\r\nserver.login(my_sender, my_pass) # \u62ec\u53f7\u4e2d\u5bf9\u5e94\u7684\u662f\u53d1\u4ef6\u4eba\u90ae\u7bb1\u8d26\u53f7\u3001\u90ae\u7bb1\u5bc6\u7801\r\nserver.sendmail(my_sender,[my_user,],msg.as_string()) # \u62ec\u53f7\u4e2d\u5bf9\u5e94\u7684\u662f\u53d1\u4ef6\u4eba\u90ae\u7bb1\u8d26\u53f7\u3001\u6536\u4ef6\u4eba\u90ae\u7bb1\u8d26\u53f7\u3001\u53d1\u9001\u90ae\u4ef6\r\nserver.quit() # \u5173\u95ed\u8fde\u63a5\r\nexcept Exception: # \u5982\u679c try \u4e2d\u7684\u8bed\u53e5\u6ca1\u6709\u6267\u884c\uff0c\u5219\u4f1a\u6267\u884c\u4e0b\u9762\u7684 ret=False\r\nret=False\r\nreturn ret\r\n\r\nret=mail()\r\nif ret:\r\nprint(\"\u90ae\u4ef6\u53d1\u9001\u6210\u529f\")\r\nelse:\r\nprint(\"\u90ae\u4ef6\u53d1\u9001\u5931\u8d25\")<\/pre>\n
$ python test.py\r\n\u90ae\u4ef6\u53d1\u9001\u6210\u529f<\/pre>\n

\u53d1\u9001\u6210\u529f\u540e\uff0c\u767b\u9646\u6536\u4ef6\u4eba\u90ae\u7bb1\u5373\u53ef\u67e5\u770b\uff1a
\n\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5b9e\u4f8b #!\/usr\/bin\/python3 import smtplib from email.mime.te […]<\/p>\n","protected":false},"author":1470,"featured_media":167053,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-167051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=167051"}],"version-history":[{"count":10,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167051\/revisions"}],"predecessor-version":[{"id":167841,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167051\/revisions\/167841"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/167053"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=167051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=167051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=167051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}