Python\u521b\u5efa SMTP \u5bf9\u8c61\u8bed\u6cd5<\/strong><\/span><\/div>\n\r\nimport smtplib\r\n \r\nsmtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )\r\n<\/pre>\n\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n
host: SMTP \u670d\u52a1\u5668\u4e3b\u673a\u3002 \u4f60\u53ef\u4ee5\u6307\u5b9a\u4e3b\u673a\u7684ip\u5730\u5740\u6216\u8005\u57df\u540d\u5982:w3cschool.cc\uff0c\u8fd9\u4e2a\u662f\u53ef\u9009\u53c2\u6570\u3002<\/p>\n
port: \u5982\u679c\u4f60\u63d0\u4f9b\u4e86 host \u53c2\u6570, \u4f60\u9700\u8981\u6307\u5b9a SMTP \u670d\u52a1\u4f7f\u7528\u7684\u7aef\u53e3\u53f7\uff0c\u4e00\u822c\u60c5\u51b5\u4e0bSMTP\u7aef\u53e3\u53f7\u4e3a25\u3002<\/p>\n
local_hostname: \u5982\u679cSMTP\u5728\u4f60\u7684\u672c\u673a\u4e0a\uff0c\u4f60\u53ea\u9700\u8981\u6307\u5b9a\u670d\u52a1\u5668\u5730\u5740\u4e3a localhost \u5373\u53ef\u3002<\/p>\n
Python SMTP\u5bf9\u8c61\u4f7f\u7528sendmail\u65b9\u6cd5\u53d1\u9001\u90ae\u4ef6\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n
\r\nSMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]\r\n<\/pre>\n\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n
from_addr: \u90ae\u4ef6\u53d1\u9001\u8005\u5730\u5740\u3002<\/p>\n
to_addrs: \u5b57\u7b26\u4e32\u5217\u8868\uff0c\u90ae\u4ef6\u53d1\u9001\u5730\u5740\u3002<\/p>\n
msg: \u53d1\u9001\u6d88\u606f<\/p>\n
\u8fd9\u91cc\u8981\u6ce8\u610f\u4e00\u4e0b\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0cmsg\u662f\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u90ae\u4ef6\u3002\u6211\u4eec\u77e5\u9053\u90ae\u4ef6\u4e00\u822c\u7531\u6807\u9898\uff0c\u53d1\u4fe1\u4eba\uff0c\u6536\u4ef6\u4eba\uff0c\u90ae\u4ef6\u5185\u5bb9\uff0c\u9644\u4ef6\u7b49\u6784\u6210\uff0c\u53d1\u9001\u90ae\u4ef6\u7684\u65f6\u5019\uff0c\u8981\u6ce8\u610fmsg\u7684\u683c\u5f0f\u3002\u8fd9\u4e2a\u683c\u5f0f\u5c31\u662fsmtp\u534f\u8bae\u4e2d\u5b9a\u4e49\u7684\u683c\u5f0f\u3002<\/p>\n
\u5b9e\u4f8b<\/strong><\/div>\n\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Python\u53d1\u9001\u90ae\u4ef6\u7b80\u5355\u7684\u5b9e\u4f8b<\/strong><\/span><\/div>\n\r\n#!\/usr\/bin\/python\r\n \r\nimport smtplib\r\n \r\nsender = 'from@fromdomain.com'\r\nreceivers = ['to@todomain.com']\r\n \r\nmessage = \"\"\"From: From PersonTo: To PersonSubject: SMTP e-mail test\r\n \r\nThis is a test e-mail message.\r\n\"\"\"\r\n \r\ntry:\r\n smtpObj = smtplib.SMTP('localhost')\r\n smtpObj.sendmail(sender, receivers, message) \r\n print \"Successfully sent email\"\r\nexcept SMTPException:\r\n print \"Error: unable to send email\"\r\n<\/pre>\n\u4f7f\u7528Python\u53d1\u9001HTML\u683c\u5f0f\u7684\u90ae\u4ef6<\/strong><\/span><\/div>\nPython\u53d1\u9001HTML\u683c\u5f0f\u7684\u90ae\u4ef6\u4e0e\u53d1\u9001\u7eaf\u6587\u672c\u6d88\u606f\u7684\u90ae\u4ef6\u4e0d\u540c\u4e4b\u5904\u5c31\u662f\u5c06MIMEText\u4e2d_subtype\u8bbe\u7f6e\u4e3ahtml\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
\r\nimport smtplib \r\nfrom email.mime.text import MIMEText \r\nmailto_list=[\"YYY@YYY.com\"] \r\nmail_host=\"smtp.XXX.com\" #\u8bbe\u7f6e\u670d\u52a1\u5668\r\nmail_user=\"XXX\" #\u7528\u6237\u540d\r\nmail_pass=\"XXXX\" #\u53e3\u4ee4 \r\nmail_postfix=\"XXX.com\" #\u53d1\u4ef6\u7bb1\u7684\u540e\u7f00\r\n \r\ndef send_mail(to_list,sub,content): #to_list\uff1a\u6536\u4ef6\u4eba\uff1bsub\uff1a\u4e3b\u9898\uff1bcontent\uff1a\u90ae\u4ef6\u5185\u5bb9\r\n me=\"hello\"+\"< \"+mail_user+\"@\"+mail_postfix+\">\" #\u8fd9\u91cc\u7684hello\u53ef\u4ee5\u4efb\u610f\u8bbe\u7f6e\uff0c\u6536\u5230\u4fe1\u540e\uff0c\u5c06\u6309\u7167\u8bbe\u7f6e\u663e\u793a\r\n msg = MIMEText(content,_subtype='html',_charset='gb2312') #\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u4e3ahtml\u683c\u5f0f\u90ae\u4ef6\r\n msg['Subject'] = sub #\u8bbe\u7f6e\u4e3b\u9898\r\n msg['From'] = me \r\n msg['To'] = \";\".join(to_list) \r\n try: \r\n s = smtplib.SMTP() \r\n s.connect(mail_host) #\u8fde\u63a5smtp\u670d\u52a1\u5668\r\n s.login(mail_user,mail_pass) #\u767b\u9646\u670d\u52a1\u5668\r\n s.sendmail(me, to_list, msg.as_string()) #\u53d1\u9001\u90ae\u4ef6\r\n s.close() \r\n return True \r\n except Exception, e: \r\n print str(e) \r\n return False \r\nif __name__ == '__main__': \r\n if send_mail(mailto_list,\"hello\",\"\u5c0f\u4e94\u4e49\"): \r\n print \"\u53d1\u9001\u6210\u529f\" \r\n else: \r\n print \"\u53d1\u9001\u5931\u8d25\"\r\n<\/pre>\n\u6216\u8005\u4f60\u4e5f\u53ef\u4ee5\u5728\u6d88\u606f\u4f53\u4e2d\u6307\u5b9aContent-type\u4e3atext\/html,\u5982\u4e0b\u5b9e\u4f8b:<\/p>\n
\r\n#!\/usr\/bin\/python\r\n \r\nimport smtplib\r\n \r\nmessage = \"\"\"From: From PersonTo: To PersonMIME-Version: 1.0\r\nContent-type: text\/html\r\nSubject: SMTP HTML e-mail test\r\n \r\nThis is an e-mail message to be sent in HTML format \r\nThis is HTML message.This is headline.\"\"\"\r\n \r\ntry:\r\n smtpObj = smtplib.SMTP('localhost')\r\n smtpObj.sendmail(sender, receivers, message) \r\n print \"Successfully sent email\"\r\nexcept SMTPException:\r\n print \"Error: unable to send email\"\r\n<\/pre>\nPython\u53d1\u9001\u5e26\u9644\u4ef6\u7684\u90ae\u4ef6<\/strong><\/span><\/div>\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<\/p>\n
\r\nfrom email.mime.text import MIMEText\r\nfrom email.mime.multipart import MIMEMultipart\r\nimport smtplib\r\n \r\n#\u521b\u5efa\u4e00\u4e2a\u5e26\u9644\u4ef6\u7684\u5b9e\u4f8b\r\nmsg = MIMEMultipart()\r\n \r\n#\u6784\u9020\u9644\u4ef61\r\natt1 = MIMEText(open('d:123.rar', 'rb').read(), 'base64', 'gb2312')\r\natt1[\"Content-Type\"] = 'application\/octet-stream'\r\natt1[\"Content-Disposition\"] = 'attachment; filename=\"123.doc\"'#\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\nmsg.attach(att1)\r\n \r\n#\u6784\u9020\u9644\u4ef62\r\natt2 = MIMEText(open('d:123.txt', 'rb').read(), 'base64', 'gb2312')\r\natt2[\"Content-Type\"] = 'application\/octet-stream'\r\natt2[\"Content-Disposition\"] = 'attachment; filename=\"123.txt\"'\r\nmsg.attach(att2)\r\n \r\n#\u52a0\u90ae\u4ef6\u5934\r\nmsg['to'] = 'YYY@YYY.com'\r\nmsg['from'] = 'XXX@XXX.com'\r\nmsg['subject'] = 'hello world'\r\n#\u53d1\u9001\u90ae\u4ef6\r\ntry:\r\n server = smtplib.SMTP()\r\n server.connect('smtp.XXX.com')\r\n server.login('XXX','XXXXX')#XXX\u4e3a\u7528\u6237\u540d\uff0cXXXXX\u4e3a\u5bc6\u7801\r\n server.sendmail(msg['from'], msg['to'],msg.as_string())\r\n server.quit()\r\n print '\u53d1\u9001\u6210\u529f'\r\nexcept Exception, e: \r\n print str(e)\r\n<\/pre>\n\u4ee5\u4e0b\u5b9e\u4f8b\u6307\u5b9a\u4e86Content-type header \u4e3a multipart\/mixed\uff0c\u5e76\u53d1\u9001\/tmp\/test.txt \u6587\u672c\u6587\u4ef6\uff1a<\/p>\n
\r\n#!\/usr\/bin\/python\r\n \r\nimport smtplib\r\nimport base64\r\n \r\nfilename = \"\/tmp\/test.txt\"\r\n \r\n# \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u4f7f\u7528 base64 \u7f16\u7801\r\nfo = open(filename, \"rb\")\r\nfilecontent = fo.read()\r\nencodedcontent = base64.b64encode(filecontent) # base64\r\n \r\nsender = 'webmaster@tutorialpoint.com'\r\nreciever = 'amrood.admin@gmail.com'\r\n \r\nmarker = \"AUNIQUEMARKER\"\r\n \r\nbody =\"\"\"\r\nThis is a test email to send an attachement.\r\n\"\"\"\r\n# \u5b9a\u4e49\u5934\u90e8\u4fe1\u606f\r\npart1 = \"\"\"From: From PersonTo: To PersonSubject: Sending Attachement\r\nMIME-Version: 1.0\r\nContent-Type: multipart\/mixed; boundary=%s\r\n--%s\r\n\"\"\" % (marker, marker)\r\n \r\n# \u5b9a\u4e49\u6d88\u606f\u52a8\u4f5c\r\npart2 = \"\"\"Content-Type: text\/plain\r\nContent-Transfer-Encoding:8bit\r\n \r\n%s\r\n--%s\r\n\"\"\" % (body,marker)\r\n \r\n# \u5b9a\u4e49\u9644\u8fd1\u90e8\u5206\r\npart3 = \"\"\"Content-Type: multipart\/mixed; name=\"%s\"\r\nContent-Transfer-Encoding:base64\r\nContent-Disposition: attachment; filename=%s\r\n \r\n%s\r\n--%s--\r\n\"\"\" %(filename, filename, encodedcontent, marker)\r\nmessage = part1 + part2 + part3\r\n \r\ntry:\r\n smtpObj = smtplib.SMTP('localhost')\r\n smtpObj.sendmail(sender, reciever, message)\r\n print \"Successfully sent email\"\r\nexcept Exception:\r\n print \"Error: unable to send email\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"python\u7684smtplib\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f88\u65b9\u4fbf\u7684\u9014\u5f84\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u3002\u5b83\u5bf9smtp\u534f\u8bae\u8fdb\u884c\u4e86\u7b80\u5355\u7684\u5c01\u88c5\u3002 impor […]<\/p>\n","protected":false},"author":1479,"featured_media":83128,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-161353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/161353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=161353"}],"version-history":[{"count":5,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/161353\/revisions"}],"predecessor-version":[{"id":161597,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/161353\/revisions\/161597"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/83128"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=161353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=161353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=161353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}