{"id":363,"date":"2023-04-26T10:00:48","date_gmt":"2023-04-26T02:00:48","guid":{"rendered":"https:\/\/www.kenpai.com\/cms\/363\/"},"modified":"2023-04-26T10:00:48","modified_gmt":"2023-04-26T02:00:48","slug":"php%e9%82%ae%e4%bb%b6mail%e5%87%bd%e6%95%b0%e8%a7%a3%e6%9e%90%e5%92%8csmtp%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95%e5%8f%8a%e4%b9%b1%e7%a0%81%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.kenpai.com\/cms\/363\/","title":{"rendered":"PHP\u90ae\u4ef6mail()\u51fd\u6570\u89e3\u6790\u548cSMTP\u4f7f\u7528\u65b9\u6cd5\u53ca\u4e71\u7801\u95ee\u9898"},"content":{"rendered":"<p>\u524d\u6bb5\u65f6\u95f4\u7531\u4e8e\u4e00\u4e2a\u9700\u6c42\u9700\u8981\u4f7f\u7528\u8bc4\u8bba\u81ea\u52a8\u53d1\u9001\u90ae\u4ef6\u5230\u7f51\u7ad9\u5b98\u65b9\u90ae\u7bb1\u7684\u529f\u80fd\uff0c\u671f\u95f4\u78b0\u5230\u4e0d\u5c11\u95ee\u9898\u7279\u522b\u662f\u4e71\u7801\u95ee\u9898\uff0c\u7ecf\u8fc7\u4e00\u756a\u7ea0\u7ed3\u540e\u7ec8\u4e8e\u5b8c\u7f8e\u89e3\u51b3\u4e86\u3002<\/p>\n<p>\u4e00\u3001\u4f7f\u7528PHP\u5185\u7f6e\u7684mail \u51fd\u6570<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong>\u00a0mail(to,subject,message,headers,parameters)<\/p>\n<ul>\n<li>to \u89c4\u5b9a\u90ae\u4ef6\u7684\u63a5\u6536\u8005<\/li>\n<li>subject \u89c4\u5b9a\u90ae\u4ef6\u7684\u4e3b\u9898\u3002\u8be5\u53c2\u6570\u4e0d\u80fd\u5305\u542b\u4efb\u4f55\u6362\u884c\u5b57\u7b26<\/li>\n<li>message \u89c4\u5b9a\u8981\u53d1\u9001\u7684\u6d88\u606f<\/li>\n<li>headers \u89c4\u5b9a\u989d\u5916\u7684\u62a5\u5934\uff0c\u6bd4\u5982 From, Cc \u4ee5\u53ca Bcc<\/li>\n<li>parameters \u89c4\u5b9a sendmail \u7a0b\u5e8f\u7684\u989d\u5916\u53c2\u6570\u3002<\/li>\n<\/ul>\n<p>\u78b0\u5230\u7684\u4e3b\u8981\u95ee\u9898\u662f\u4e71\u7801\u95ee\u9898\uff0c\u521a\u5f00\u59cb\u662f\u67d0\u4e9b\u5ba2\u6237\u7aef\u63a5\u6536\u90ae\u4ef6\u65f6\u597d(\u6bd4\u5982QQ\u90ae\u7bb1\uff0c\u4f30\u8ba1\u5e26\u81ea\u52a8\u90a3\u4e2a\u8bc6\u522b\u7f16\u7801)\u7684\u6709\u4e9b\u4e0dfoxmail\u3001ipad\u663e\u793a\u4e71\u7801\uff0c\u89e3\u51b3\u65b9\u5f0f\u6b63\u786e\u7684\u8bbe\u7f6e\u8fd9\u4e2amail\u7684headers\u5c31\u884c\u4e86\uff0c\u4e0b\u9762\u662f\u6211\u4f7f\u7528\u7684\u5b8c\u7f8e\u7684\u65e0\u4e71\u7801\u7684\u4f8b\u5b50\u3002<\/p>\n<pre style=\"margin: 15px 0;font: 100 12px\/18px monaco, andale mono, courier new;padding: 10px 12px;border: #ccc 1px solid;border-left-width: 4px;background-color: #fefefe;color: #444\">&lt;?php\n\/\/ \u5f53\u53d1\u9001 HTML \u7535\u5b50\u90ae\u4ef6\u65f6\uff0c\u8bf7\u59cb\u7ec8\u8bbe\u7f6e content-type\n$headers = \"MIME-Version: 1.0\" . \"\\r\\n\";\n\n$headers .= \"Content-type:text\/html; charset=utf-8\";\n\nmail($to,$subject,$message,$headers);\n?&gt;<\/pre>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u4f7f\u7528\u65b9\u4fbf\u5c31\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570<br \/>\n<strong>\u7f3a\u70b9\uff1a<\/strong>\u9700\u8981php.ini\u652f\u6301\u8be5\u51fd\u6570\uff0c\u5982\u679c\u67d0\u4e9b\u670d\u52a1\u5668\u4e0d\u652f\u6301\u800c\u53c8\u4e0d\u80fd\u6539\u73af\u5883\u90a3\u5c31\u4e0d\u884c\u4e86\u800c\u4e14\u603b\u662f\u4e0d\u7a33\u5b9a\uff0c\u53d1\u7684\u6709\u65f6\u80fd\u6536\u5230\u6709\u65f6\u4e0d\u80fd\u3002<\/p>\n<p>\u4e8c\u3001\u4f7f\u7528SMTP\u53d1\u9001\u90ae\u4ef6<\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e00\u4e2a\u5df2\u7ecf\u5c01\u88c5\u597d\u7684smtp\u7c7bemail.class.php\u7c7b\u88ab\u6211\u4fee\u6539\u8fc7\uff0c\u589e\u52a0\u4e86\u7f16\u7801\u8bbe\u7f6e\uff0c\u6ca1\u52a0\u7f16\u7801\u8bbe\u7f6e\u7684\u8bdd\u4f1a\u5728\u67d0\u4e9b\u4e0d\u80fd\u81ea\u52a8\u8bc6\u522b\u7f16\u7801\u7684\u5ba2\u6237\u7aef\u4e0a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<p>\u5173\u4e8e\u8fd9\u4e2a\u7c7b\u7684\u4f7f\u7528:<\/p>\n<pre style=\"margin: 15px 0;font: 100 12px\/18px monaco, andale mono, courier new;padding: 10px 12px;border: #ccc 1px solid;border-left-width: 4px;background-color: #fefefe;color: #444\">&lt;?php\nrequire_once ('email.class.php');\n\n$smtpserver = \"smtp.exmail.qq.com\";\n$smtpserverport =25;\n$smtpusermail = \"lyushine@qq.com\";\n$smtpuser = \"lyushine@qq.com\";\n$smtppass = \"xxxxxx\";\n$mailtype = \"HTML\";\n\n$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);\n$smtp-&gt;debug = false;\n$smtp-&gt;sendmail($to, $smtpusermail, $subject,$message, $mailtype,\"utf-8\");\n?&gt;<\/pre>\n<p>\u770b\u4ee5\u4e0a\u4ee3\u7801\u5927\u6982\u4e86\u89e3\u4e86\u5427\u3002\u5148\u5f15\u5165email.class.php\u7136\u540e\u5b9e\u4f8b\u5316smtp\u7c7b\uff0c\u914d\u7f6edebug\u6a21\u5f0f\uff0c\u53d1\u9001\u90ae\u4ef6\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u8bbe\u7f6e\u6700\u540e\u7684\u7f16\u7801\u3002<\/p>\n<p>\u521a\u5f00\u59cb\u6211\u7f51\u4e0a\u627e\u7684\u6ca1\u6709\u5bf9\u7f16\u7801\u8fdb\u884c\u5904\u7406\u5bfc\u81f4\u4e71\u7801\u3002<\/p>\n<p>\u5173\u4e8e headers \u4e2d subject \u7684\u4e71\u7801\uff1a<\/p>\n<p>\u4e0d\u8981\u628a\u4efb\u4f55\u9664\u4e86 iso-8859-1 \u7f16\u7801\u4e4b\u5916\u7684\u6587\u5b57\u76f4\u63a5\u653e\u8fdb subject\uff0c\u6b63\u786e\u7684\u65b9\u5f0f\u662f\u8ba9 subject \u91cc\u5934\u7684\u6587\u5b57\u7b26\u5408 RFC 2047 \u7684\u89c4\u8303\uff0c\u5047\u8bbe\u4f60\u7684 subject \u5904\u7684\u6587\u5b57\u7684\u7f16\u7801\u662f GB2312\uff0c\u90a3\u4e48\u6b63\u786e\u7684 subject \u5e94\u8be5\u662f\u8fd9\u6837\u7684\u683c\u5f0f $headers[&#8216;subject&#8217;] = &#8216;=?GB2312?B?&#8217; . base64_encode(&#8216;\u6807\u9898\u6587\u5b57&#8217;) . &#8216;?=&#8217;;\u5982\u679c\u662f UTF-8 \u7684\u8bdd\uff0c\u628a\u4e0a\u9762\u5f00\u5934\u7684 GB2312 \u66ff\u6362\u6210 UTF-8 \u5373\u53ef\u3002\u63a8\u8350\u5728\u6807\u9898\u5904\u4f7f\u7528 GB2312 \u7f16\u7801\uff0c\u56e0\u4e3a MTA \u7684\u5206\u5316\u5b9e\u5728\u662f\u4e25\u91cd\uff0c\u5927\u90e8\u5206\u7684 MSP \u4f7f\u7528\u7684\u90fd\u662f\u81ea\u5df1\u5f00\u53d1\u7684 MTA\uff0c\u800c\u76f8\u4fe1\u56fd\u5185\u7684\u5927\u90e8\u5206 MTA \u65e0\u8bba\u5982\u4f55\u90fd\u662f\u53ef\u4ee5\u652f\u6301 GB2312 \u7f16\u7801\u7684\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\u6211\u628a\u5bf9subject\u7684\u5904\u7406\u5728\u4e86\u7c7b\u91cc\u9762\uff0c\u800c\u5916\u9762\u53ea\u8981\u4f20\u53c2\u8bbe\u7f6e\u7f16\u7801\u4e3autf-8\u6216\u8005GB2312\u5c31\u884c\u4e86\u3002\u5728email.class.php\u91cc\u662f\u8fd9\u6837\u5904\u7406\u7684\uff0c\u8fd9\u6837\u6709\u6548\u7684\u89e3\u51b3\u4e86\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u53ef\u9760\u6027\u5f3a\uff0c\u7a33\u5b9a<br \/>\n<strong>\u7f3a\u70b9\uff1a<\/strong>\u4ee3\u7801\u8f83\u591a\uff0c\u90ae\u7bb1\u7684\u8d26\u53f7\u5bc6\u7801\u5fc5\u987b\u4f7f\u7528\u660e\u6587\u5b58\u4e8e\u672c\u7ad9\u4e2d\uff0c\u4e0d\u5b89\u5168\u56e0\u7d20\u589e\u52a0,\u9700\u8981\u5728\u81ea\u5df1\u7684\u90ae\u7bb1\u914d\u7f6e\u5f00\u542fsmtp\u670d\u52a1<\/p>\n<p>\u4e0b\u9762\u9644\u4e0a email.class.php<\/p>\n","protected":false},"excerpt":{"rendered":"<p><strong>\u6458\u8981\uff1a<\/strong><\/p>\n","protected":false},"author":1,"featured_media":4350,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3019],"tags":[3160,2997],"class_list":["post-363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-phpmailsmtp","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts\/363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/comments?post=363"}],"version-history":[{"count":0,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/posts\/363\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/media\/4350"}],"wp:attachment":[{"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/media?parent=363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/categories?post=363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenpai.com\/cms\/wp-json\/wp\/v2\/tags?post=363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}