溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP怎么利用PHPMailer實現郵件發送功能

發布時間:2022-03-28 13:55:18 來源:億速云 閱讀:408 作者:小新 欄目:開發技術

這篇文章將為大家詳細講解有關PHP怎么利用PHPMailer實現郵件發送功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

一、安裝環境

PHPMailer 需要 PHP 的 sockets 擴展支持

另外登錄 QQ 郵箱 SMTP 服務器則必須通過 SSL 加密的, PHP 還得包含 openssl 的支持

PHP怎么利用PHPMailer實現郵件發送功能

二、下載 

地址: https://github.com/PHPMailer/PHPMailer/

PHP怎么利用PHPMailer實現郵件發送功能

三、 郵箱設置

所有的主流郵箱都支持 SMTP 協議,但并非所有郵箱都默認開啟

您可以在郵箱的設置里面手動開啟

第三方服務在提供了賬號和密碼之后就可以登錄 SMTP 服務器

通過它來控制郵件的中轉方式

SMTP 服務器認證密碼,需要妥善保管

PHP怎么利用PHPMailer實現郵件發送功能

PHP怎么利用PHPMailer實現郵件發送功能

PHP怎么利用PHPMailer實現郵件發送功能

PHP怎么利用PHPMailer實現郵件發送功能

四、php發送郵件

<?php
 
// 引入PHPMailer的核心文件
require_once("PHPMailer/class.phpmailer.php");
require_once("PHPMailer/class.smtp.php");
 
// 實例化PHPMailer核心類
$mail = new PHPMailer();
 
// 是否啟用smtp的debug進行調試 開發環境建議開啟 生產環境注釋掉即可 默認關閉debug調試模式
$mail->SMTPDebug = 1;
 
// 使用smtp鑒權方式發送郵件
$mail->isSMTP();
 
// smtp需要鑒權 這個必須是true
$mail->SMTPAuth = true;
 
// 鏈接qq域名郵箱的服務器地址
$mail->Host = 'smtp.qq.com';
 
// 設置使用ssl加密方式登錄鑒權
$mail->SMTPSecure = 'ssl';
 
// 設置ssl連接smtp服務器的遠程服務器端口號
$mail->Port = 465;
 
// 設置發送的郵件的編碼
$mail->CharSet = 'UTF-8';
 
// 設置發件人昵稱 顯示在收件人郵件的發件人郵箱地址前的發件人姓名
$mail->FromName = '發件人昵稱';
 
// smtp登錄的賬號 任意郵箱即可
$mail->Username = 'xxxxxxx@163.com';
 
// smtp登錄的密碼 使用生成的授權碼
$mail->Password = '**********';
 
// 設置發件人郵箱地址 同登錄賬號
$mail->From = 'xxxxxxx@qq.com';
 
// 郵件正文是否為html編碼 注意此處是一個方法
$mail->isHTML(true);
 
// 設置收件人郵箱地址
$mail->addAddress('xxxxxxxxx@qq.com');
 
// 添加多個收件人 則多次調用方法即可
$mail->addAddress('xxxxxxxxx@163.com');
 
// 添加該郵件的主題
$mail->Subject = '郵件主題';
 
// 添加郵件正文
$mail->Body = '<h2>Hello, i am autofelix</h2>';
 
// 為該郵件添加附件
$mail->addAttachment('./附件.pdf');
 
// 發送郵件 返回狀態
$status = $mail->send();

五、php框架中使用

先使用composer進行安裝:composer require phpmailer/phpmailer ^6.5

使用

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
 
class mail 
{
    public function send() 
    {
        //Create an instance; passing `true` enables exceptions
        $mail = new PHPMailer(true);
 
        try {
            //Server settings
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
            $mail->isSMTP();                                            //Send using SMTP
            $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
            $mail->Username   = 'user@example.com';                     //SMTP username
            $mail->Password   = 'secret';                               //SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
            $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
 
            //Recipients
            $mail->setFrom('from@example.com', 'Mailer');
            $mail->addAddress('joe@example.net', 'Joe User');     //Add a recipient
            $mail->addAddress('ellen@example.com');               //Name is optional
            $mail->addReplyTo('info@example.com', 'Information');
            $mail->addCC('cc@example.com');
            $mail->addBCC('bcc@example.com');
 
            //Attachments
            $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
            $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name
 
            //Content
            $mail->isHTML(true);                                  //Set email format to HTML
            $mail->Subject = 'Here is the subject';
            $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
 
            $mail->send();
            echo 'Message has been sent';
        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    }
}

關于“PHP怎么利用PHPMailer實現郵件發送功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女