thinkPHP中利用phhmailer發(fā)送郵箱驗(yàn)證碼

一,首先用composer安裝phpmailer

1、未安裝composer的是官網(wǎng)安裝

2、配置composer全局變量

composer config -g repo.packagist composer https://packagist.phpcomposer.com

3、安裝phpmailer

composer require phpmailer/phpmailer

2)從GitHub上下載PHPMailer

4、如果是從GitHub上下載的,將其放在項(xiàng)目的開放目錄下,我在ThinkPHP下面

所在目錄

5、在function.php里面加上發(fā)送驗(yàn)證碼的方法

function send_mail($toemail, $name, $subject = '', $body = '',$attachment = null) {

? ? require './ThinkPHP/PHPMailer/PHPMailer/src/PHPMailer.php';

? ? require './ThinkPHP/PHPMailer/PHPMailer/src/SMTP.php';

? ? require './ThinkPHP/PHPMailer/PHPMailer/src/Exception.php';

? ? $mail = new PHPMailer\PHPMailer\PHPMailer();

? ? $mail->CharSet = 'UTF-8';? ? ? ? ? //設(shè)定郵件編碼,默認(rèn)ISO-8859-1,如果發(fā)中文此項(xiàng)必須設(shè)置,否則亂碼

? ? $mail->IsSMTP();? ? ? ? ? ? ? ? ? ? // 設(shè)定使用SMTP服務(wù)

? ? $mail->SMTPDebug = 2;? ? ? ? ? ? ? // SMTP調(diào)試功能 0=關(guān)閉 1 = 錯誤和消息 2 = 消息

? ? $mail->SMTPAuth = true;? ? ? ? ? ? // 啟用 SMTP 驗(yàn)證功能

? ? $mail->SMTPSecure = 'ssl';? ? ? ? ? // 使用安全協(xié)議

? ? $mail->Host = "smtp.163.com"; // smtp服務(wù)器的名稱,這里用的是163郵箱,qq: smtp.qq.com , 163:smtp.163.com,sina:smtp.sina.com

? ? $mail->Port = 465;? ? ? ? ? ? ? ? ? // SMTP服務(wù)器的端口號

? ? $mail->Username = '15202429501@163.com';? ? // SMTP服務(wù)器用戶名

? ? $mail->Password = 'zxc06245379';? ? // SMTP服務(wù)器密碼//這里的密碼可以是郵箱登錄密碼也可以是SMTP服務(wù)器密碼

? ? $mail->SetFrom('15202429501@163.com', '68房源');

? ? $replyEmail = '';? ? ? ? ? ? ? ? ? //留空則為發(fā)件人EMAIL

? ? $replyName = '';? ? ? ? ? ? ? ? ? ? //回復(fù)名稱(留空則為發(fā)件人名稱)

? ? $mail->AddReplyTo($replyEmail, $replyName);

? ? $mail->Subject = $subject;

? ? $mail->MsgHTML($body);

? ? $mail->AddAddress($toemail, $name);

? ? if (is_array($attachment)) { // 添加附件

? ? ? ? foreach ($attachment as $file) {

? ? ? ? ? ? is_file($file) && $mail->AddAttachment($file);

? ? ? ? }

}

? ? return $mail->Send() ? true : $mail->ErrorInfo;

}

6、在UserController里面寫上發(fā)送的方法

請求發(fā)送代碼

7、完成

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容