欧美精品免费观看二区/在线观看av/粉嫩国产精品14xxxxx/亚洲精品视频在线观看免费

ThinkPHP中使用PHPMailer發(fā)送郵件
發(fā)布時間:2024-03-07

    1、設(shè)置我們的郵箱客戶端授權(quán)碼

    2、下載第三方類庫 PHPMailer


    Github下載地址:
        https://github.com/PHPMailer/PHPMailer
    ThinkPHP中使用Composer安裝命令:
        composer require phpmailer/phpmailer
        安裝在 vendor/phpmailer/ 目錄下


    3、創(chuàng)建代碼片段并配置相關(guān)的參數(shù)
        1、找到我們項(xiàng)目公共配置文件的common.php文件,創(chuàng)建一個公用的函數(shù)。代碼如下:

        

    /**
     * 郵件發(fā)送
     * * * * * * * * * * * * * * * * * * * * * *
     * * * * * * * * * * * * * * * * * * * * * *
     * * 開始的時候,記得引用類                    *
     * * use PHPMailerPHPMailerPHPMailer;    *
     * * 應(yīng)用公共函數(shù)文件,函數(shù)不能定義為public類型  *
     * * * * * * * * * * * * * * * * * * * * * *
     * * * * * * * * * * * * * * * * * * * * * *
     */
    
    function sendEmail( $desc_content $toemail  $desc_url)
    {
        // 實(shí)力化類
        $mail = new PHPMailer();
        // 使用SMTP服務(wù)
        $mail->isSMTP();
        // 編碼格式為utf8,不設(shè)置編碼的話,中文會出現(xiàn)亂碼
        $mail->CharSet = "utf8";
        // 發(fā)送方的SMTP服務(wù)器地址
        $mail->Host = "smtp.163.com";
        // 是否使用身份驗(yàn)證
        $mail->SMTPAuth = true;
        // 發(fā)送方的163郵箱用戶名,就是你申請163的SMTP服務(wù)使用的163郵箱
        $mail->Username = "********@163.com";
        // 發(fā)送方的郵箱密碼,注意用163郵箱這里填寫的是“客戶端授權(quán)密碼”而不是郵箱的登錄密碼
        $mail->Password = "******";
        // 使用ssl協(xié)議方式
        $mail->SMTPSecure = "ssl";
        // 163郵箱的ssl協(xié)議方式端口號是465/994
        $mail->Port = 994;
        // 設(shè)置發(fā)件人信息,如郵件格式說明中的發(fā)件人,這里會顯示為Mailer(xxxx@163.com),Mailer是當(dāng)做名字顯示
        $mail->setFrom("*******@163.com" "Mailer");
        // 設(shè)置收件人信息,如郵件格式說明中的收件人,這里會顯示為Liang(yyyy@163.com)
        $mail->addAddress($toemail '');
        // 設(shè)置回復(fù)人信息,指的是收件人收到郵件后,如果要回復(fù),回復(fù)郵件將發(fā)送到的郵箱地址
        $mail->addReplyTo("itlaowen@163.com" "Reply");
        // 設(shè)置郵件抄送人,可以只寫地址,上述的設(shè)置也可以只寫地址(這個人也能收到郵件)
        //$mail->addCC("xxx@163.com");
        // 設(shè)置秘密抄送人(這個人也能收到郵件)
        //$mail->addBCC("xxx@163.com");
        // 添加附件
        //$mail->addAttachment("bug0.jpg");
        // 郵件標(biāo)題
        $mail->Subject = "郵件標(biāo)題!";
        // 郵件正文
        $mail->Body = "測試內(nèi)容:" . $desc_content . "點(diǎn)擊可以查看文章地址:" . $desc_url;
        // 這個是設(shè)置純文本方式顯示的正文內(nèi)容,如果不支持Html方式,就會用到這個,基本無用
        //$mail->AltBody = "This is the plain text純文本";
        if (!$mail->send()) { // 發(fā)送郵件
            return $mail->ErrorInfo;
            // echo "Message could not be sent.";
            // echo "Mailer Error: ".$mail->ErrorInfo;// 輸出錯誤信息
        } else {
            return 1;
        }
    }
    

    2、創(chuàng)建好后,我們便可以直接調(diào)用該函數(shù)。



    public function send()
    {
        echo sendEmail( '測試' '111111@163.com' 'http://www.baidu.com' );
    }