PHP利用PHPMailer實(shí)現(xiàn)郵件發(fā)送功能
導(dǎo)語(yǔ)
〝 古人學(xué)問(wèn)遺無(wú)力,少壯功夫老始成 〞
隨著企業(yè)化的管理越來(lái)越規(guī)范,各種項(xiàng)目管理系統(tǒng)中,都需要加入到郵件實(shí)時(shí)通知功能,所以在項(xiàng)目中如何整合發(fā)郵件功能,其實(shí)是很重要的一點(diǎn)。如果這篇文章能給你帶來(lái)一點(diǎn)幫助,希望給飛兔小哥哥一鍵三連,表示支持,謝謝各位小伙伴們。
一、安裝環(huán)境
PHPMailer 需要 PHP 的 sockets 擴(kuò)展支持
另外登錄 QQ 郵箱 SMTP 服務(wù)器則必須通過(guò) SSL 加密的, PHP 還得包含 openssl 的支持

二、下載
地址: https://github.com/PHPMailer/PHPMailer/

三、 郵箱設(shè)置
所有的主流郵箱都支持 SMTP 協(xié)議,但并非所有郵箱都默認(rèn)開(kāi)啟
您可以在郵箱的設(shè)置里面手動(dòng)開(kāi)啟
第三方服務(wù)在提供了賬號(hào)和密碼之后就可以登錄 SMTP 服務(wù)器
通過(guò)它來(lái)控制郵件的中轉(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)試 開(kāi)發(fā)環(huán)境建議開(kāi)啟 生產(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)文章
實(shí)戰(zhàn)mysql導(dǎo)出中文亂碼及phpmyadmin導(dǎo)入中文亂碼的解決方法
因?yàn)橐驯緳C(jī)的gbk編碼的mysql數(shù)據(jù)庫(kù)導(dǎo)入到虛擬主機(jī)去,服務(wù)商只提供了phpmyadmin供你導(dǎo)入導(dǎo)出。2010-06-06
php驗(yàn)證手機(jī)號(hào)碼(支持歸屬地查詢及編碼為UTF8)
本文將實(shí)現(xiàn)以下功能:手機(jī)號(hào)驗(yàn)證/手機(jī)號(hào)碼歸屬地/轉(zhuǎn)換字符串編碼為UTF8,對(duì)此有興趣的朋友可以參考下,或許本文對(duì)你有所幫助2013-02-02
如何對(duì)PHP程序中的常見(jiàn)漏洞進(jìn)行攻擊(上)
如何對(duì)PHP程序中的常見(jiàn)漏洞進(jìn)行攻擊(上)...2006-12-12
PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之對(duì)象生成方法詳解
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之對(duì)象生成方法,簡(jiǎn)單介紹了php常見(jiàn)對(duì)象生成模式并結(jié)合實(shí)例形式分析了php對(duì)象生成的單例模式、工廠模式、原形模式等概念與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-12-12

