欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長(zhǎng)資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          怎么通過(guò)CakePHP內(nèi)置模塊發(fā)送郵件(Gmail)

          本篇文章給大家介紹如何通過(guò)CakePHP內(nèi)置模塊發(fā)送郵件(Gmail),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)大家有所幫助。

          效果

          通過(guò)事先配置模塊,來(lái)實(shí)現(xiàn)發(fā)送郵件的功能。(此代碼無(wú)法根據(jù)用戶輸入信息來(lái)更改發(fā)送郵箱)

          前提

          閱讀者已有CakePHP的基礎(chǔ)知識(shí)

          版本

          CakePHP 3.6.1
          PHP 7.4.10

          準(zhǔn)備

          <ProjectFolder>configapp.php里加入以下配置(記得替換郵箱信息)

          'EmailTransport' => [         'default' => [             'className' => 'Smtp',             'host' => 'smtp.gmail.com',             'port' => 587,             'timeout' => 30,             'username' => 'name@gmail.com',             'password' => '12345678',             'tls' => true,             'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),         ],     ], 'Email' => [      'default' => [          'transport' => 'default',          'from' => 'name@gamail.com',          //'charset' => 'utf-8',          //'headerCharset' => 'utf-8',      ],  ],

          去任意Controller.php里,寫發(fā)送郵件的函數(shù)。
          *此處有參考stackoverflow

          //在最上面加載模塊 use CakeMailerEmail;  //在任意class下寫郵件發(fā)送的函數(shù) public function send()     {         $email = new Email('default');         try {             $email->setFrom(['name@gmail.com' => 'My Site'])                 ->setTo('接受者郵箱@126.com')                 ->setSubject('主題')                 ->send('本文');             echo "success";         } catch (CakeNetworkExceptionSocketException $exception) {             $lastResponse = $email->transport()->getLastResponse();             var_dump($lastResponse);         }     }

          在<ProjectName>configroutes.php寫好路由

          $routes->connect('/send', ['controller' => 'ControllerName', 'action' => 'send']);

          訪問(wèn),測(cè)試
          怎么通過(guò)CakePHP內(nèi)置模塊發(fā)送郵件(Gmail)

          報(bào)錯(cuò),這是因?yàn)間mail的安全防護(hù)級(jí)別太高了。

          登錄到自己的google賬號(hào)管理中心->安全性

          關(guān)閉兩步驗(yàn)證

          怎么通過(guò)CakePHP內(nèi)置模塊發(fā)送郵件(Gmail)

          開啟訪問(wèn)權(quán)限

          怎么通過(guò)CakePHP內(nèi)置模塊發(fā)送郵件(Gmail)

          怎么通過(guò)CakePHP內(nèi)置模塊發(fā)送郵件(Gmail)

          然后刷新頁(yè)面,可以發(fā)現(xiàn)我們已經(jīng)成功通過(guò)gmail發(fā)送郵件了。
          怎么通過(guò)CakePHP內(nèi)置模塊發(fā)送郵件(Gmail)

          為了賬號(hào)安全,測(cè)試成功之后記得把安全防護(hù)級(jí)別調(diào)高。

          推薦學(xué)習(xí):《PHP視頻教程》

          贊(3)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)