{"id":681,"date":"2020-10-16T15:16:04","date_gmt":"2020-10-16T07:16:04","guid":{"rendered":"https:\/\/www.cxybj.com\/?p=681"},"modified":"2020-10-16T15:16:04","modified_gmt":"2020-10-16T07:16:04","slug":"thinkphp5-composer%e5%ae%89%e8%a3%85%e5%92%8c%e5%8e%8b%e7%bc%a9%e5%8c%85%e5%ae%89%e8%a3%85-phpmailer%e5%ae%9e%e7%8e%b0%e9%82%ae%e4%bb%b6%e5%8f%91%e9%80%81","status":"publish","type":"post","link":"https:\/\/www.cxybj.com\/?p=681","title":{"rendered":"Thinkphp5 Composer\u5b89\u88c5\u548c\u538b\u7f29\u5305\u5b89\u88c5 PHPMailer\u5b9e\u73b0\u90ae\u4ef6\u53d1\u9001"},"content":{"rendered":"<p><strong>\u65b9\u6cd5\u4e00\uff1a<\/strong><\/p>\n<div id=\"wrapper\">\n<div id=\"container\">\n<div id=\"main\" class=\"clearfix\">\n<div class=\"main-left\">\n<div id=\"article\">\n<div id=\"content\">\n<p><a href=\"https:\/\/www.cxybj.com\/wp-content\/uploads\/soft\/PHPMailer-master.zip\">\u4e0b\u8f7dPHPMailer<\/a>\u7c7b\u5305\uff0c\u653e\u5165ThinkPHP\u7684Vendor\u76ee\u5f55\uff0c<br \/>\n\u5b98\u7f51\u4e0b\u8f7d\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/PHPMailer\/PHPMailer\/\">https:\/\/github.com\/PHPMailer\/PHPMailer\/<\/a><\/p>\n<p><a href=\"https:\/\/www.cxybj.com\/wp-content\/uploads\/2020\/10\/phpmaier.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-682\" src=\"https:\/\/www.cxybj.com\/wp-content\/uploads\/2020\/10\/phpmaier.png\" alt=\"\" width=\"497\" height=\"253\" srcset=\"https:\/\/www.cxybj.com\/wp-content\/uploads\/2020\/10\/phpmaier.png 497w, https:\/\/www.cxybj.com\/wp-content\/uploads\/2020\/10\/phpmaier-300x153.png 300w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><\/a><\/p>\n<p>Thinkphp\u6846\u67b6extend\u6587\u4ef6\u5939\u4e2d\u65b0\u5efa\u6587\u4ef6phpmailer\uff0c\u52a0\u5165\u4e0a\u97623\u4e2a\u6587\u4ef6\uff0c<\/p>\n<p><span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">phpmailer<\/span>;<\/p>\n<p>\u5728application\u4e0b\u521b\u5efaextra\u76ee\u5f55\uff0c\u521b\u5efa email.php\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<div>return [<\/div>\n<div>\u00a0<wbr \/>\u00a0\u00a0<wbr \/>&#8216;EMAIL_SMTP&#8217;=&gt;&#8217;smtp.163.com&#8217;,<\/div>\n<div>\u00a0<wbr \/>\u00a0\u00a0<wbr \/>&#8216;EMAIL_ADDRESS&#8217;=&gt;&#8217;###@163.com&#8217;,<\/div>\n<div>\u00a0<wbr \/>\u00a0\u00a0<wbr \/>&#8216;EMAIL_NICKNAME&#8217;=&gt;&#8217;###\u7f51&#8217;,<\/div>\n<div>\u00a0<wbr \/>\u00a0\u00a0<wbr \/>&#8216;EMAIL_LOGINNAME&#8217;=&gt;&#8221;,<\/div>\n<div>\u00a0<wbr \/>\u00a0\u00a0<wbr \/>&#8216;EMAIL_PASSWORD&#8217;=&gt;&#8221;,<\/div>\n<div>\u00a0<wbr \/>\u00a0\u00a0<wbr \/>\u00a0&#8216;EMAIL_PORT&#8217; =&gt; &#8217;25&#8217;\u00a0<wbr \/>\u00a0\u00a0<wbr \/>\u00a0<span style=\"color: #ff0000;\">\/\/\u5982\u679c\u662f\u963f\u91cc\u4e91\uff0c\u6362\u6210 465\u7aef\u53e3<\/span><\/div>\n<div>];<\/div>\n<\/div>\n<div>\n<div>\u5728application\u4e0b common.php\u6dfb\u52a0\u4ee3\u7801<\/div>\n<div>\u6700\u9876\uff1a<\/div>\n<div>use <span class=\"hljs-title\">phpmailer<\/span>\\PHPMailer;<\/div>\n<div>\u65b9\u6cd5\uff1a\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>function sendEmail($email, $content, $title){\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail = new PHPMailer(); \/\/3.\u8bbe\u7f6e\u5c5e\u6027\uff0c\u544a\u8bc9\u6211\u4eec\u7684\u670d\u52a1\u5668\uff0c\u8c01\u8ddf\u8c01\u53d1\u9001\u90ae\u4ef6\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; IsSMTP(); \/\/\u544a\u8bc9\u670d\u52a1\u5668\u4f7f\u7528smtp\u534f\u8bae\u53d1\u9001\u00a0<wbr \/><\/div>\n<div>$mail-&gt;SMTPSecure = &#8216;ssl&#8217;;\/\/\u8bbe\u7f6e\u4f7f\u7528ssl\u52a0\u5bc6\u65b9\u5f0f\u767b\u5f55\u9274\u6743\u00a0<wbr \/><br \/>\n<span style=\"color: #ff1111;\">\/\/\u963f\u91cc\u4e91 \u5f00\u542fssl \u65b9\u5f0f \u7aef\u53e3\uff1a465<\/span><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; SMTPAuth = true; \/\/\u5f00\u542fSMTP\u6388\u6743\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; Host = config(&#8217;email.EMAIL_SMTP&#8217;); \/\/\u544a\u8bc9\u6211\u4eec\u7684\u670d\u52a1\u5668\u4f7f\u7528163\u7684smtp\u670d\u52a1\u5668\u53d1\u9001\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; From = config(&#8217;email.EMAIL_ADDRESS&#8217;); \/\/\u53d1\u9001\u8005\u7684\u90ae\u4ef6\u5730\u5740\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; FromName = config(&#8217;email.EMAIL_NICKNAME&#8217;); \/\/\u53d1\u9001\u90ae\u4ef6\u7684\u7528\u6237\u6635\u79f0\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; Username = config(&#8217;email.EMAIL_LOGINNAME&#8217;); \/\/\u767b\u5f55\u5230\u90ae\u7bb1\u7684\u7528\u6237\u540d\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; Password = config(&#8217;email.EMAIL_PASSWORD&#8217;); \/\/\u7b2c\u4e09\u65b9\u767b\u5f55\u7684\u6388\u6743\u7801\uff0c\u5728\u90ae\u7bb1\u91cc\u9762\u8bbe\u7f6e \/\/\u7f16\u8f91\u53d1\u9001\u7684\u90ae\u4ef6\u5185\u5bb9\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; Port = config(&#8217;email.EMAIL_PORT&#8217;);\u00a0<wbr \/><\/div>\n<div><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; IsHTML(true); \/\/\u53d1\u9001\u7684\u5185\u5bb9\u4f7f\u7528html\u7f16\u5199\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; CharSet = &#8216;utf-8&#8217;; \/\/\u8bbe\u7f6e\u53d1\u9001\u5185\u5bb9\u7684\u7f16\u7801\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; Subject = $title;\/\/\u8bbe\u7f6e\u90ae\u4ef6\u7684\u6807\u9898\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; MsgHTML($content); \/\/\u53d1\u9001\u7684\u90ae\u4ef6\u5185\u5bb9\u4e3b\u4f53\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$mail -&gt; AddAddress($email); \/\/\u6536\u4eba\u7684\u90ae\u4ef6\u5730\u5740 \/\/\u8c03\u7528send\u65b9\u6cd5\uff0c\u6267\u884c\u53d1\u9001\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>$result = $mail -&gt; Send();\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>if($result){<\/div>\n<div>\u00a0<wbr \/>return true;\u00a0<wbr \/><\/div>\n<div>\u00a0<wbr \/>}<\/div>\n<div>else{ return $mail -&gt; ErrorInfo; }\u00a0<wbr \/><\/div>\n<div>}<\/div>\n<div>\u5230\u8fd9\u91cc\u57fa\u672c\u5b8c\u6210\u4e86\uff0c\u4e0d\u8fc7\u6709\u4e24\u4e2a\u5751\uff1a<\/div>\n<div>\u7b2c\u4e00\u6ca1\u6253\u5f00extend\u76ee\u5f55\uff0c\u5c31\u662f\u627e\u4e0d\u5230PHPMailer \uff0c\u67e5\u770b\u4e00\u4e0b\u9879\u76eeindex.php\u4e2d\u7684\u662f\u5426\u6709\uff1a<\/div>\n<div>define(&#8216;EXTEND_PATH&#8217;, __DIR__ .&#8217;\/extend\/&#8217;);<\/div>\n<\/div>\n<div id=\"content\">\n<div id=\"wrapper\">\n<div id=\"container\">\n<div id=\"main\" class=\"clearfix\">\n<div class=\"main-left\">\n<div id=\"article\">\n<div id=\"content\">\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>\u653e\u6cd5\u4e8c:<\/strong><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>composer\u00a0 require\u00a0\u00a0phpmailer\/phpmailer<\/p>\n<p>\u7136\u540e\u5728common.php\u91cc\u5f15\u5165\uff1a<\/p>\n<p>use PHPMailer\\PHPMailer\\PHPMailer;<\/p>\n<p>\/**<\/p>\n<p>* @function\u00a0 \u00a0\u00a0sendEmail<\/p>\n<p>* @intro\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u53d1\u9001\u90ae\u4ef6\uff08\u5e26\u9644\u4ef6\uff09<\/p>\n<p>* @param\u00a0$email\u00a0 \u00a0 \u00a0\u63a5\u6536\u90ae\u7bb1<\/p>\n<p>* @param\u00a0$title\u00a0 \u00a0 \u00a0\u90ae\u4ef6\u6807\u9898<\/p>\n<p>* @param\u00a0$from_name\u00a0 \u00a0 \u00a0\u53d1\u4ef6\u4eba<\/p>\n<p>* @param\u00a0$content\u00a0 \u00a0 \u00a0\u90ae\u4ef6\u5185\u5bb9<\/p>\n<p>* @param\u00a0$attachmentFile\u00a0 \u00a0 \u00a0\u9644\u4ef6 \uff08string | array\uff09<\/p>\n<p>* @return\u00a0\u00a0array<\/p>\n<p>*\/<\/p>\n<p>function sendEmail($email=&#8221;, $title=&#8221;, $from_name=&#8221;, $content=&#8221;, $attachmentFile=&#8221;){<\/p>\n<p>date_default_timezone_set(&#8216;PRC&#8217;);<\/p>\n<p>\/\/Create a new PHPMailer instance<\/p>\n<p>$mail\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= new PHPMailer;<\/p>\n<p>\/\/Tell PHPMailer to use SMTP<\/p>\n<p>$mail-&gt;isSMTP();<\/p>\n<p>\/\/Enable SMTP debugging<\/p>\n<p>\/\/ 0 = off (for production use)<\/p>\n<p>\/\/ 1 = client messages<\/p>\n<p>\/\/ 2 = client and server messages<\/p>\n<p>$mail-&gt;SMTPDebug\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 = 0;<\/p>\n<p>\/\/Ask for HTML-friendly debug output<\/p>\n<p>$mail-&gt;Debugoutput\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 = &#8216;html&#8217;;<\/p>\n<p>\/\/charset<\/p>\n<p>$mail-&gt;CharSet\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 = &#8216;UTF-8&#8217;;<\/p>\n<p>\/\/Set the hostname of the mail server<\/p>\n<p>$mail-&gt;Host\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= &#8220;&#8221;;\/\/\u8bf7\u586b\u5199\u4f60\u7684\u90ae\u7bb1\u670d\u52a1\u5668<\/p>\n<p>\/\/Set the SMTP port number &#8211; likely to be 25, 465 or 587<\/p>\n<p>$mail-&gt;Port\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= 25;\/\/\u7aef\u53e3\u53f7<\/p>\n<p>\/\/Whether to use SMTP authentication<\/p>\n<p>$mail-&gt;SMTPAuth\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= true;<\/p>\n<p>\/\/Username to use for SMTP authentication<\/p>\n<p>$mail-&gt;Username\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= &#8220;&#8221;;\/\/\u53d1\u4ef6\u90ae\u7bb1\u7528\u6237\u540d<\/p>\n<p>\/\/Password to use for SMTP authentication<\/p>\n<p>$mail-&gt;Password\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= &#8220;&#8221;;\/\/\u53d1\u4ef6\u90ae\u7bb1\u5bc6\u7801<\/p>\n<p>\/\/Set who the message is to be sent from<\/p>\n<p>$mail-&gt;setFrom(&#8216;\u53d1\u4ef6\u90ae\u7bb1\u7528\u6237\u540d&#8217;, $from_name);<\/p>\n<p>\/\/Set an alternative reply-to address(\u7528\u6237\u76f4\u63a5\u56de\u590d\u90ae\u4ef6\u7684\u5730\u5740)<\/p>\n<p>$mail-&gt;addReplyTo(&#8216;\u53d1\u4ef6\u90ae\u7bb1\u7528\u6237\u540d&#8217;, $from_name);<\/p>\n<p>\/\/Set who the message is to be sent to<\/p>\n<p>$mail-&gt;addAddress($email);<\/p>\n<p>\/\/Set the subject line<\/p>\n<p>$mail-&gt;Subject\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 = $title;<\/p>\n<p>\/\/Read an HTML message body from an external file, convert referenced images to embedded,<\/p>\n<p>\/\/convert HTML into a basic plain-text alternative body<\/p>\n<p>$mail-&gt;msgHTML($content);<\/p>\n<p>\/\/Replace the plain text body with one created manually<\/p>\n<p>$mail-&gt;AltBody\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 = &#8221;;<\/p>\n<p>if(is_array($attachmentFile)){<\/p>\n<p>for ($i=0; $i &lt; count($attachmentFile); $i++) {<\/p>\n<p>$mail-&gt;addAttachment($attachmentFile[$i],&#8217;Filename&#8217;.$i);\/\/\u8fd9\u91cc\u53ef\u4ee5\u662f\u591a\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u5faa\u73af\u9644\u4ef6\u7684\u6587\u4ef6\u548c\u540d\u79f0<\/p>\n<p>}<\/p>\n<p>}else{<\/p>\n<p>if($attachmentFile !=&#8221;){<\/p>\n<p>\/\/Attach an image file<\/p>\n<p>$mail-&gt;addAttachment($attachmentFile, &#8216;Filename&#8217;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/send the message, check for errors<\/p>\n<p>if (!$mail-&gt;send()) {<\/p>\n<p>$status\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= 0;<\/p>\n<p>$data\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= &#8220;\u90ae\u4ef6\u53d1\u9001\u5931\u8d25&#8221; . $mail-&gt;ErrorInfo;;<\/p>\n<p>} else {<\/p>\n<p>$status\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= 1;<\/p>\n<p>$data\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= &#8220;\u90ae\u4ef6\u53d1\u9001\u6210\u529f&#8221;;<\/p>\n<p>}<\/p>\n<p>return [&#8216;status&#8217;=&gt;$status,&#8217;data&#8217;=&gt;$data];\/\/\u8fd4\u56de\u503c\uff08\u53ef\u9009\uff09<\/p>\n<p>}<\/p>\n<p>\u5728\u9700\u8981\u53d1\u90ae\u4ef6\u7684\u5730\u65b9\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<p>sendEmail($email, &#8216;\u90ae\u4ef6\u6807\u9898&#8217;, &#8216;\u53d1\u4ef6\u4eba\u7b80\u79f0&#8217;, $content, Env::get(&#8216;root_path&#8217;).&#8217;public\/uploads\/file\/&#8217;.$file, &#8216;file.jpg&#8217;);<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\u4e00\uff1a \u4e0b\u8f7dPHPMailer\u7c7b\u5305\uff0c\u653e\u5165ThinkPHP\u7684 &hellip; <a href=\"https:\/\/www.cxybj.com\/?p=681\" class=\"more-link\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,133],"tags":[],"_links":{"self":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/681"}],"collection":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=681"}],"version-history":[{"count":1,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/681\/revisions"}],"predecessor-version":[{"id":683,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/681\/revisions\/683"}],"wp:attachment":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}