1. 程式人生 > >PHPMailer使用 (郵箱傳送)

PHPMailer使用 (郵箱傳送)

本例是在TP5下進行:

使用phpmailer    需要PHP開啟   php_sockets 和 php_fileinfo

使用 phpinfo() 函式檢視 socket 和 openssl 擴充套件資訊(wamp server 預設啟用了該擴充套件);

1.首先檢查php.ini中;extension=php_openssl.dll是否存在, 如果存在的話去掉前面的註釋符‘;', 如果不存在這行,那麼新增extension=php_openssl.dll;

2.開啟php.ini檔案,需要做以下事情:

(1)啟用extension_dir = "ext"

(2)啟用extension=php_sockets.dll

可以執行這樣一段程式碼看是否開啟了sockets(也可info()檢視):

<?php
if(extension_loaded('sockets')){
echo "已開啟";
}else{
echo "未開啟";
}
?>

將下載壓縮包中的這五個檔案考出來

在自己專案中的vendor資料夾下建立一個PHPMailer資料夾並將之前考的五個檔案複製進去

開啟vendor檔案下的composer.json檔案新增這樣一行程式碼

將PHPMailer資料夾下的5個檔案的名稱空間修改為

namespace ven\PHPMailer;

設定將發件人開啟SMTP:

此地以QQ郵箱為例:在郵件的設定——>賬戶中開啟,跟著系統操作會生成一個授權碼,用於填寫在common.php的檔案中客戶端授權碼那個位置;

4.寫公共方法common.php, 根據註釋修改成自己的配置項

/**
 * 傳送郵件
 * @param $toemail 收件人email
 * @param $subject 郵件主題
 * @param $message 正文
 * @param $from 發件人
 * @param $cfg 郵件配置資訊
 * @param $sitename 郵件站點名稱
 */
function send_email($toemail, $subject, $message, $from = '', $cfg = array(), $sitename = '')
{
    //判斷openssl是否開啟
    $openssl_funcs = get_extension_funcs('openssl');
    if (!$openssl_funcs) {
        return array('status' => -1, 'msg' => '請先開啟openssl擴充套件');
    }
    //表單提交 測試傳送
    /*if ($cfg && is_array($cfg)) {
    $from = $cfg['from'];
    $email = $cfg;
    } else {
    $config = cache('Config');
 
    }*/
 
    // $config = cache('Config');
    $mail = new ven\PHPMailer\PHPMailer();
    //Server settings
    // var_dump($mail);die;
    $mail->CharSet = 'UTF-8'; //設定郵件編碼,預設ISO-8859-1,如果發中文此項必須設定,否則亂碼
    $mail->SMTPDebug = 0; // Enable verbose debug output
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host = 'smtp.qq.com'; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = '設定開啟了SMTP的郵箱號'; // SMTP username
    $mail->Password = 'SMTP的客戶端授權碼'; // SMTP password
    $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 25; //埠 - likely to be 25, 465 or 587
    //Recipients
    $mail->setFrom('發郵件的賬號(例如:
[email protected]
)', '發郵件的賬戶名'); //傳送方地址和暱稱 $mail->addAddress($toemail, 'Joe User'); // Add a recipient //$mail->addReplyTo('[email protected]', 'Information'); //回覆地址 //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = $subject; //標題 $mail->Body = $message; //內容 //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if (!$mail->send()) { return array('status' => -1, 'msg' => '傳送失敗: ' . $mail->ErrorInfo); } else { return array('status' => 1, 'msg' => '傳送成功'); } }

5.開啟命令列,cd到自己的專案目錄  執行以下這串程式碼會生成一個檔案

composer dump-autoload

6.寫方法執行

<?php
namespace app\index\controller;


use think\Controller;
use Psr\Log\Logger;


class Index extends Controller
{
    public function index()
    {
        // send_email($toemail, $subject, $message, $from = '', $cfg = array(), $sitename = '')
        send_email('[email protected]', '大兄弟你趕緊滴兒', '<a href="http://www.baidu.com/">我跟你說啊,我等半個小時了都,扛不住啦我的天啦!!!</a>', $from = '發件人', $cfg = array(), $sitename = '站點名稱');
        echo "yes";
    }


}

執行Index下的index控制器;傳送郵件!!!

7.ok,成功傳送郵件