最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP利用PHPMailer實現(xiàn)郵件發(fā)送功能

 更新時間:2022年03月28日 11:26:26   作者:autofelix  
phpMailer是一個非常強大的php發(fā)送郵件類,可以設(shè)定發(fā)送郵件地址、回復(fù)地址、郵件主題、html網(wǎng)頁,上傳附件,并且使用起來非常方便。本文將利用它實現(xiàn)郵件發(fā)送功能,需要的可以參考一下

導(dǎo)語

〝 古人學(xué)問遺無力,少壯功夫老始成 〞

隨著企業(yè)化的管理越來越規(guī)范,各種項目管理系統(tǒng)中,都需要加入到郵件實時通知功能,所以在項目中如何整合發(fā)郵件功能,其實是很重要的一點。如果這篇文章能給你帶來一點幫助,希望給飛兔小哥哥一鍵三連,表示支持,謝謝各位小伙伴們。

一、安裝環(huán)境

PHPMailer 需要 PHP 的 sockets 擴展支持

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

二、下載 

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

三、 郵箱設(shè)置

所有的主流郵箱都支持 SMTP 協(xié)議,但并非所有郵箱都默認(rèn)開啟

您可以在郵箱的設(shè)置里面手動開啟

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

通過它來控制郵件的中轉(zhuǎn)方式

SMTP 服務(wù)器認(rèn)證密碼,需要妥善保管

四、php發(fā)送郵件

<?php
 
// 引入PHPMailer的核心文件
require_once("PHPMailer/class.phpmailer.php");
require_once("PHPMailer/class.smtp.php");
 
// 實例化PHPMailer核心類
$mail = new PHPMailer();
 
// 是否啟用smtp的debug進行調(diào)試 開發(fā)環(huán)境建議開啟 生產(chǎn)環(huán)境注釋掉即可 默認(rèn)關(guān)閉debug調(diào)試模式
$mail->SMTPDebug = 1;
 
// 使用smtp鑒權(quán)方式發(fā)送郵件
$mail->isSMTP();
 
// smtp需要鑒權(quán) 這個必須是true
$mail->SMTPAuth = true;
 
// 鏈接qq域名郵箱的服務(wù)器地址
$mail->Host = 'smtp.qq.com';
 
// 設(shè)置使用ssl加密方式登錄鑒權(quán)
$mail->SMTPSecure = 'ssl';
 
// 設(shè)置ssl連接smtp服務(wù)器的遠(yuǎn)程服務(wù)器端口號
$mail->Port = 465;
 
// 設(shè)置發(fā)送的郵件的編碼
$mail->CharSet = 'UTF-8';
 
// 設(shè)置發(fā)件人昵稱 顯示在收件人郵件的發(fā)件人郵箱地址前的發(fā)件人姓名
$mail->FromName = '發(fā)件人昵稱';
 
// smtp登錄的賬號 任意郵箱即可
$mail->Username = 'xxxxxxx@163.com';
 
// smtp登錄的密碼 使用生成的授權(quán)碼
$mail->Password = '**********';
 
// 設(shè)置發(fā)件人郵箱地址 同登錄賬號
$mail->From = 'xxxxxxx@qq.com';
 
// 郵件正文是否為html編碼 注意此處是一個方法
$mail->isHTML(true);
 
// 設(shè)置收件人郵箱地址
$mail->addAddress('xxxxxxxxx@qq.com');
 
// 添加多個收件人 則多次調(diào)用方法即可
$mail->addAddress('xxxxxxxxx@163.com');
 
// 添加該郵件的主題
$mail->Subject = '郵件主題';
 
// 添加郵件正文
$mail->Body = '<h1>Hello, i am autofelix</h1>';
 
// 為該郵件添加附件
$mail->addAttachment('./附件.pdf');
 
// 發(fā)送郵件 返回狀態(tài)
$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實現(xiàn)郵件發(fā)送功能的詳細(xì)內(nèi)容,更多關(guān)于PHP PHPMailer郵件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

温泉县| 南皮县| 桂阳县| 霞浦县| 灵石县| 梁山县| 巴林左旗| 安康市| 深水埗区| 凤山县| 韶山市| 正蓝旗| 扎鲁特旗| 莫力| 蒙城县| 通海县| 明水县| 乌拉特中旗| 罗山县| 曲水县| 柳江县| 竹溪县| 饶平县| 崇仁县| 汝阳县| 革吉县| 宁德市| 潍坊市| 资源县| 阿合奇县| 鄂尔多斯市| 环江| 都江堰市| 遵义市| 崇阳县| 怀安县| 湛江市| 射洪县| 玛曲县| 仙居县| 红河县|