{"id":214689,"date":"2021-04-06T08:50:32","date_gmt":"2021-04-06T00:50:32","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=214689"},"modified":"2021-03-24T16:51:38","modified_gmt":"2021-03-24T08:51:38","slug":"understand-how-php","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/understand-how-php.html","title":{"rendered":"\u8ba4\u8bc6\u4e0bPHP\u5982\u4f55\u4f7f\u7528 phpmailer \u53d1\u9001\u7535\u5b50\u90ae\u4ef6"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nphpMailer \u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684 ph p\u53d1\u9001\u90ae\u4ef6\u7c7b,\u53ef\u4ee5\u8bbe\u5b9a\u53d1\u9001\u90ae\u4ef6\u5730\u5740\u3001\u56de\u590d\u5730\u5740\u3001\u90ae\u4ef6\u4e3b\u9898\u3001html\u7f51\u9875,\u4e0a\u4f20\u9644\u4ef6,\u5e76\u4e14\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u65b9\u4fbf\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

phpMailer \u7684\u7279\u70b9\uff1a<\/strong><\/p>\n

    \n
  1. \u5728\u90ae\u4ef6\u4e2d\u5305\u542b\u591a\u4e2a TO\u3001CC\u3001BCC \u548c REPLY-TO\u3002<\/li>\n
  2. \u5e73\u53f0\u5e94\u7528\u5e7f\u6cdb\uff0c\u652f\u6301\u7684 SMTP \u670d\u52a1\u5668\u5305\u62ec Sendmail\u3001qmail\u3001Postfix\u3001Gmail\u3001Imail\u3001Exchange \u7b49\u7b49\u3002<\/li>\n
  3. \u652f\u6301\u5d4c\u5165\u56fe\u50cf\uff0c\u9644\u4ef6\uff0cHTML \u90ae\u4ef6\u3002<\/li>\n
  4. \u53ef\u9760\u7684\u5f3a\u5927\u7684\u8c03\u8bd5\u529f\u80fd\u3002<\/li>\n
  5. \u652f\u6301 SMTP \u8ba4\u8bc1\u3002<\/li>\n
  6. \u81ea\u5b9a\u4e49\u90ae\u4ef6\u5934\u3002<\/li>\n
  7. \u652f\u6301 8bit\u3001base64\u3001binary \u548c quoted-printable \u7f16\u7801\u3002<\/li>\n<\/ol>\n

    phpmailer \u5b89\u88c5\u6216\u8005\u4e0b\u8f7d\u65b9\u5f0f:<\/p>\n

      \n
    1. \u4ece github \u4e0a\u4e0b\u8f7d: https:\/\/github.com\/PHPMailer\/PHPMailer\/<\/li>\n
    2. \u4f7f\u7528 composer \u5b89\u88c5:<\/li>\n<\/ol>\n
      composer require phpmailer\/phpmailer<\/pre>\n

      \u53d1\u9001\u4e4b\u524d\u9700\u8981\u62e5\u6709\u81ea\u5df1\u7684\u90ae\u4ef6\u670d\u52a1\u5668\uff0c\u6d4b\u8bd5\u7684\u65f6\u5019\u5176\u5b9e\u7528\u81ea\u5df1\u7533\u8bf7\u7684\u514d\u8d39\u90ae\u7bb1\u6700\u65b9\u4fbf\u4e86\uff0c\u4e0d\u9700\u8981\u81ea\u5df1\u518d\u642d\u5efa\u670d\u52a1\u5668\u4e86\uff0c\u53ef\u80fd\u8981\u914d\u7f6e\u90ae\u7bb1\u7684SMTP\u670d\u52a1\uff0c\u5927\u90e8\u5206\u516c\u5171\u90ae\u7bb1\uff08163\u3001qq\u7b49\uff09\u4e3a\u4e86\u5b89\u5168\u9ed8\u8ba4\u662f\u5173\u95ed\u7684\u3002<\/p>\n

      \u7f51\u6613\u90ae\u7bb1\u914d\u7f6e\u5982\u4e0b\u56fe:<\/p>\n

      \"\"<\/p>\n

      QQ \u90ae\u7bb1\u76f8\u5173\u914d\u7f6e\u5982\u4e0b\u56fe:<\/p>\n\n\n\n\n
      \u90ae\u7bb1<\/th>\nPOP3\u670d\u52a1\u5668\uff08\u7aef\u53e3995\uff09<\/th>\nSMTP\u670d\u52a1\u5668\uff08\u7aef\u53e3465\u6216587\uff09<\/th>\n<\/tr>\n
      qq.com<\/td>\npop.qq.com<\/td>\nsmtp.qq.com<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

      \u5f53\u7136\u9664\u4e86\u7f51\u6613\u548c QQ \u90ae\u7bb1\u5176\u4ed6\u90ae\u7bb1\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u4e0b\u9762\u7ed9\u51fa php \u4ee3\u7801\u793a\u4f8b\uff1a
      \n\u5b9e\u4f8b<\/strong><\/p>\n

      <?php\r\n
      <\/span>use <\/span>PHPMailer<\/span>\\<\/span>PHPMailer<\/span>\\<\/span>PHPMailer<\/span>;\r\n
      use <\/span>PHPMailer<\/span>\\<\/span>PHPMailer<\/span>\\<\/span>Exception<\/span>;\r\n
      \r\n
      require <\/span>'.\/src\/Exception.php'<\/span>;\r\n
      require <\/span>'.\/src\/PHPMailer.php'<\/span>;\r\n
      require <\/span>'.\/src\/SMTP.php'<\/span>;\r\n
      \r\n
      <\/span>$mail <\/span>= new <\/span>PHPMailer<\/span>(<\/span>true<\/span>);                              <\/span>\/\/ Passing `true` enables exceptions\r\n
      <\/span>try {\r\n
          <\/span>\/\/\u670d\u52a1\u5668\u914d\u7f6e\r\n
          <\/span>$mail<\/span>-><\/span>CharSet <\/span>=<\/span>\"UTF-8\"<\/span>;                     <\/span>\/\/\u8bbe\u5b9a\u90ae\u4ef6\u7f16\u7801\r\n
          <\/span>$mail<\/span>-><\/span>SMTPDebug <\/span>= <\/span>0<\/span>;                        <\/span>\/\/ \u8c03\u8bd5\u6a21\u5f0f\u8f93\u51fa\r\n
          <\/span>$mail<\/span>-><\/span>isSMTP<\/span>();                             <\/span>\/\/ \u4f7f\u7528SMTP\r\n
          <\/span>$mail<\/span>-><\/span>Host <\/span>= <\/span>'smtp.163.com'<\/span>;                <\/span>\/\/ SMTP\u670d\u52a1\u5668\r\n
          <\/span>$mail<\/span>-><\/span>SMTPAuth <\/span>= <\/span>true<\/span>;                      <\/span>\/\/ \u5141\u8bb8 SMTP \u8ba4\u8bc1\r\n
          <\/span>$mail<\/span>-><\/span>Username <\/span>= <\/span>'\u90ae\u7bb1\u7528\u6237\u540d'<\/span>;                <\/span>\/\/ SMTP \u7528\u6237\u540d  \u5373\u90ae\u7bb1\u7684\u7528\u6237\u540d\r\n
          <\/span>$mail<\/span>-><\/span>Password <\/span>= <\/span>'\u5bc6\u7801\u6216\u8005\u6388\u6743\u7801'<\/span>;             <\/span>\/\/ SMTP \u5bc6\u7801  \u90e8\u5206\u90ae\u7bb1\u662f\u6388\u6743\u7801(\u4f8b\u5982163\u90ae\u7bb1)\r\n
          <\/span>$mail<\/span>-><\/span>SMTPSecure <\/span>= <\/span>'ssl'<\/span>;                    <\/span>\/\/ \u5141\u8bb8 TLS \u6216\u8005ssl\u534f\u8bae\r\n
          <\/span>$mail<\/span>-><\/span>Port <\/span>= <\/span>465<\/span>;                            <\/span>\/\/ \u670d\u52a1\u5668\u7aef\u53e3 25 \u6216\u8005465 \u5177\u4f53\u8981\u770b\u90ae\u7bb1\u670d\u52a1\u5668\u652f\u6301\r\n
      \r\n
          <\/span>$mail<\/span>-><\/span>setFrom<\/span>(<\/span>'xxxx@163.com'<\/span>, <\/span>'Mailer'<\/span>);  <\/span>\/\/\u53d1\u4ef6\u4eba\r\n
          <\/span>$mail<\/span>-><\/span>addAddress<\/span>(<\/span>'aaaa@126.com'<\/span>, <\/span>'Joe'<\/span>);  <\/span>\/\/ \u6536\u4ef6\u4eba\r\n
          \/\/$mail->addAddress('ellen@example.com');  \/\/ \u53ef\u6dfb\u52a0\u591a\u4e2a\u6536\u4ef6\u4eba\r\n
          <\/span>$mail<\/span>-><\/span>addReplyTo<\/span>(<\/span>'xxxx@163.com'<\/span>, <\/span>'info'<\/span>); <\/span>\/\/\u56de\u590d\u7684\u65f6\u5019\u56de\u590d\u7ed9\u54ea\u4e2a\u90ae\u7bb1 \u5efa\u8bae\u548c\u53d1\u4ef6\u4eba\u4e00\u81f4\r\n
          \/\/$mail->addCC('cc@example.com');                    \/\/\u6284\u9001\r\n
          \/\/$mail->addBCC('bcc@example.com');                    \/\/\u5bc6\u9001\r\n
      \r\n
          \/\/\u53d1\u9001\u9644\u4ef6\r\n
          \/\/ $mail->addAttachment('..\/xy.zip');         \/\/ \u6dfb\u52a0\u9644\u4ef6\r\n
          \/\/ $mail->addAttachment('..\/thumb-1.jpg', 'new.jpg');    \/\/ \u53d1\u9001\u9644\u4ef6\u5e76\u4e14\u91cd\u547d\u540d\r\n
      \r\n
          \/\/Content\r\n
          <\/span>$mail<\/span>-><\/span>isHTML<\/span>(<\/span>true<\/span>);                                  <\/span>\/\/ \u662f\u5426\u4ee5HTML\u6587\u6863\u683c\u5f0f\u53d1\u9001  \u53d1\u9001\u540e\u5ba2\u6237\u7aef\u53ef\u76f4\u63a5\u663e\u793a\u5bf9\u5e94HTML\u5185\u5bb9\r\n
          <\/span>$mail<\/span>-><\/span>Subject <\/span>= <\/span>'\u8fd9\u91cc\u662f\u90ae\u4ef6\u6807\u9898' <\/span>. <\/span>time<\/span>();\r\n
          <\/span>$mail<\/span>-><\/span>Body    <\/span>= <\/span>'<h1>\u8fd9\u91cc\u662f\u90ae\u4ef6\u5185\u5bb9<\/h1>' <\/span>. <\/span>date<\/span>(<\/span>'Y-m-d H:i:s'<\/span>);\r\n
          <\/span>$mail<\/span>-><\/span>AltBody <\/span>= <\/span>'\u5982\u679c\u90ae\u4ef6\u5ba2\u6237\u7aef\u4e0d\u652f\u6301HTML\u5219\u663e\u793a\u6b64\u5185\u5bb9'<\/span>;\r\n
      \r\n
          <\/span>$mail<\/span>-><\/span>send<\/span>();\r\n
          echo <\/span>'\u90ae\u4ef6\u53d1\u9001\u6210\u529f'<\/span>;\r\n
      } catch (<\/span>Exception $e<\/span>) {\r\n
          echo <\/span>'\u90ae\u4ef6\u53d1\u9001\u5931\u8d25: '<\/span>, <\/span>$mail<\/span>-><\/span>ErrorInfo<\/span>;\r\n
      }<\/span>\r\n\r\n<\/pre>\n

      \u53d1\u9001\u5b8c\u5e26\u9644\u4ef6\u7684\u90ae\u4ef6\u754c\u9762\u5982\u4e0b\uff1a
      \n\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

      phpMailer \u7684\u7279\u70b9\uff1a \u5728\u90ae\u4ef6\u4e2d\u5305\u542b\u591a\u4e2a TO\u3001CC\u3001BCC \u548c REPLY-TO\u3002 \u5e73\u53f0\u5e94\u7528\u5e7f\u6cdb\uff0c\u652f\u6301 […]<\/p>\n","protected":false},"author":321,"featured_media":214702,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-214689","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\/214689","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\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=214689"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/214689\/revisions"}],"predecessor-version":[{"id":214705,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/214689\/revisions\/214705"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/214702"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=214689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=214689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=214689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}