PHP利用PHPMailer實(shí)現(xiàn)郵件發(fā)送功能
導(dǎo)語
〝 古人學(xué)問遺無力,少壯功夫老始成 〞
隨著企業(yè)化的管理越來越規(guī)范,各種項(xiàng)目管理系統(tǒng)中,都需要加入到郵件實(shí)時(shí)通知功能,所以在項(xiàng)目中如何整合發(fā)郵件功能,其實(shí)是很重要的一點(diǎn)。如果這篇文章能給你帶來一點(diǎn)幫助,希望給飛兔小哥哥一鍵三連,表示支持,謝謝各位小伙伴們。
一、安裝環(huán)境
PHPMailer 需要 PHP 的 sockets 擴(kuò)展支持
另外登錄 QQ 郵箱 SMTP 服務(wù)器則必須通過 SSL 加密的, PHP 還得包含 openssl 的支持
二、下載
地址: https://github.com/PHPMailer/PHPMailer/
三、 郵箱設(shè)置
所有的主流郵箱都支持 SMTP 協(xié)議,但并非所有郵箱都默認(rèn)開啟
您可以在郵箱的設(shè)置里面手動(dòng)開啟
第三方服務(wù)在提供了賬號(hào)和密碼之后就可以登錄 SMTP 服務(wù)器
通過它來控制郵件的中轉(zhuǎn)方式
SMTP 服務(wù)器認(rèn)證密碼,需要妥善保管
四、php發(fā)送郵件
<?php // 引入PHPMailer的核心文件 require_once("PHPMailer/class.phpmailer.php"); require_once("PHPMailer/class.smtp.php"); // 實(shí)例化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) 這個(gè)必須是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ù)器端口號(hào) $mail->Port = 465; // 設(shè)置發(fā)送的郵件的編碼 $mail->CharSet = 'UTF-8'; // 設(shè)置發(fā)件人昵稱 顯示在收件人郵件的發(fā)件人郵箱地址前的發(fā)件人姓名 $mail->FromName = '發(fā)件人昵稱'; // smtp登錄的賬號(hào) 任意郵箱即可 $mail->Username = 'xxxxxxx@163.com'; // smtp登錄的密碼 使用生成的授權(quán)碼 $mail->Password = '**********'; // 設(shè)置發(fā)件人郵箱地址 同登錄賬號(hào) $mail->From = 'xxxxxxx@qq.com'; // 郵件正文是否為html編碼 注意此處是一個(gè)方法 $mail->isHTML(true); // 設(shè)置收件人郵箱地址 $mail->addAddress('xxxxxxxxx@qq.com'); // 添加多個(gè)收件人 則多次調(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利用PHPMailer實(shí)現(xiàn)郵件發(fā)送功能的詳細(xì)內(nèi)容,更多關(guān)于PHP PHPMailer郵件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
PHP下操作Linux消息隊(duì)列完成進(jìn)程間通信的方法
當(dāng)我們開發(fā)的系統(tǒng)需要使用多進(jìn)程方式運(yùn)行時(shí),進(jìn)程間通信便成了至關(guān)重要的環(huán)節(jié)。消息隊(duì)列(message queue)是Linux系統(tǒng)進(jìn)程間通信的一種方式。2010-07-07php去除二維數(shù)組的重復(fù)項(xiàng)方法
這篇文章主要介紹了php去除二維數(shù)組的重復(fù)項(xiàng)方法,感興趣的小伙伴們可以參考一下2015-11-11PHP中header和session_start前不能有輸出原因分析
在http傳輸文本中,規(guī)定必須 header和content順序必須是:header在前content在后,并且header的格式必須滿足“keyword: value\n”這種格式,大家知道這是為什么嗎?接下來為您詳細(xì)解答2013-01-01php設(shè)計(jì)模式 FlyWeight (享元模式)
運(yùn)用享元技術(shù)有效的支持大量細(xì)粒度的對(duì)象,需要的朋友可以參考下。2011-06-06php解決crontab定時(shí)任務(wù)不能寫入文件問題的方法分析
這篇文章主要介紹了php解決crontab定時(shí)任務(wù)不能寫入文件問題的方法,結(jié)合實(shí)例形式分析了crontab定時(shí)任務(wù)無法正常執(zhí)行的原因與解決方法,需要的朋友可以參考下2019-09-09