一,首先用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ā)送的方法

7、完成