基于PHP實現(xiàn)郵件實時通知功能
一、安裝環(huán)境
PHPMailer 需要 PHP 的 sockets 擴(kuò)展支持
另外登錄 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進(jìn)行調(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進(jìn)行安裝: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實現(xiàn)郵件實時通知功能的詳細(xì)內(nèi)容,更多關(guān)于PHP郵件實時通知的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
php打開遠(yuǎn)程文件的方法和風(fēng)險及解決方法
本文講的是php打開遠(yuǎn)程文件的使用方法,還有使用風(fēng)險和解決方法2013-11-11PHP實現(xiàn)處理三級分類數(shù)據(jù)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用PHP實現(xiàn)處理三級分類數(shù)據(jù),文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以參考一下2024-02-02PHP實現(xiàn)手機(jī)號碼中間四位用星號(*)隱藏的自定義函數(shù)分享
這篇文章主要介紹了PHP實現(xiàn)手機(jī)號碼中間四位用星號(*)隱藏的自定義函數(shù)分享,這是一個比較常用的功能,需要的朋友可以參考下2014-09-09