php邮件发送tp,Thinkphp5 邮件发送Thinkphp发送邮件
在項(xiàng)目的開發(fā)中 用戶修改密碼,需要發(fā)送驗(yàn)證碼到用戶郵箱,
在common.php公共文件中加入以下代碼:
/**
* 系統(tǒng)郵件發(fā)送函數(shù)
* @param string $tomail 接收郵件者郵箱
* @param string $name 接收郵件者名稱
* @param string $subject 郵件主題
* @param string $body 郵件內(nèi)容
* @param string $attachment 附件列表
* @return boolean
*/
function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null) {
$mail = new PHPMailer\PHPMailer\PHPMailer(); //實(shí)例化PHPMailer對(duì)象
$mail->CharSet = 'UTF-8'; //設(shè)定郵件編碼,默認(rèn)ISO-8859-1,如果發(fā)中文此項(xiàng)必須設(shè)置,否則亂碼
$mail->IsSMTP(); // 設(shè)定使用SMTP服務(wù)
$mail->SMTPDebug = 0; // SMTP調(diào)試功能 0=關(guān)閉 1 = 錯(cuò)誤和消息 2 = 消息
$mail->SMTPAuth = true; // 啟用 SMTP 驗(yàn)證功能
$mail->SMTPSecure = 'ssl'; // 使用安全協(xié)議
$mail->Host = "smtp.qq.com"; // SMTP 服務(wù)器
$mail->Port = 465; // SMTP服務(wù)器的端口號(hào)
$mail->Username = "xxxxxx@qq.com"; // SMTP服務(wù)器用戶名
$mail->Password = "xxxxxx"; // SMTP服務(wù)器密碼
$mail->SetFrom('xxxx@qq.com', '發(fā)送人的名稱');
$replyEmail = ''; //留空則為發(fā)件人EMAIL
$replyName = ''; //回復(fù)名稱(留空則為發(fā)件人名稱)
$mail->AddReplyTo($replyEmail, $replyName);
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->AddAddress($tomail, $name);
if (is_array($attachment)) { // 添加附件
foreach ($attachment as $file) {
is_file($file) && $mail->AddAttachment($file);
}
}
return $mail->Send();
}
控制器:
public function email(){
//收件人的郵箱
$toemail='xxxxx@qq.com';
//收件人的名稱
$name='xxxxxxx';
$subject='QQ郵件發(fā)送測(cè)試';
$code = mt_rand(10000, 99999);
session("code",$code);
$content='你得驗(yàn)證碼為'.$code;
dump(send_mail($toemail,$name,$subject,$content));
}
最后驗(yàn)證是否正確 根據(jù)輸入驗(yàn)證碼的去判斷取出session的 code的進(jìn)行比較
TP5實(shí)現(xiàn)郵件發(fā)送(二)使用 composer 命令進(jìn)行安裝或下載源代碼使用。
composer require houdunwang/mail
郵件配置,以下是126郵箱配置的示例:
return [
'ssl' => false,//服務(wù)器使用ssl
'username' => 'houdunwang@126.com',//郵箱帳號(hào)
'password' => '',//登錄密碼
'host' => 'smtp.126.com',//服務(wù)器主機(jī)
'port' => '25',//服務(wù)器端口號(hào)
'fromname' => '后盾網(wǎng)',//發(fā)件人(會(huì)員收到郵件時(shí)顯示)
'frommail' => 'houdunwang@126.com',//發(fā)件人郵箱(會(huì)員收到郵件時(shí)顯示)
];
發(fā)送郵件
函數(shù)語法
Mail::send(收件人郵箱,收件人名稱,郵件標(biāo)題,郵件內(nèi)容);
發(fā)送郵件
Mail::send(
'xxx@qq.com',
'xxx@qq.com',
"這是郵件標(biāo)題",
"這是郵件正文"
);
使用閉包設(shè)置內(nèi)容
Mail::send('test@xx.com', 'test@xx.com', "這是郵件標(biāo)題", function () { return View::instance()->with(['title' => '測(cè)試郵件發(fā)送成功', 'content' => '這是郵件內(nèi)容'])->fetch('resource/view/email.html'); });
總結(jié)
以上是生活随笔為你收集整理的php邮件发送tp,Thinkphp5 邮件发送Thinkphp发送邮件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redis php方案,Redis三种部
- 下一篇: omnicppcomplete php,