PHPMailer——发送邮件函数封装
生活随笔
收集整理的這篇文章主要介紹了
PHPMailer——发送邮件函数封装
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
源代碼?
<?php /** * 郵件發(fā)送 * @param $to 接收人 * @param string $subject 郵件標(biāo)題 * @param string $content 郵件內(nèi)容(html模板渲染后的內(nèi)容) * @throws Exception * @throws phpmailerException */ // Import PHPMailer classes into the global namespace // These must be at the top of your script, not inside a function use PHPMailer\PHPMailer\PHPMailer; function send_email($to="1234567890@qq.com",$subject='',$content='<h1>Hello World</h1>'){// 引入PHPMailer的核心文件require_once(dirname(__FILE__)."/PHPMailer/src/PHPMailer.php");require_once(dirname(__FILE__)."/PHPMailer/src/SMTP.php");// 實例化PHPMailer核心類$mail = new PHPMailer();// 是否啟用smtp的debug進行調(diào)試 開發(fā)環(huán)境建議開啟 生產(chǎn)環(huán)境注釋掉即可 默認(rèn)關(guān)閉debug調(diào)試模式//Enable SMTP debugging// 0 = off (for production use)// 1 = client messages// 2 = client and server messages$mail->SMTPDebug = 1;//調(diào)試輸出格式//$mail->Debugoutput = 'html';// 使用smtp鑒權(quán)方式發(fā)送郵件$mail->isSMTP();// smtp需要鑒權(quán) 這個必須是true$mail->SMTPAuth = true;// 鏈接qq域名郵箱的服務(wù)器地址$mail->Host = 'smtp.qq.com';// 設(shè)置使用ssl加密方式登錄鑒權(quán)$mail->SMTPSecure = 'ssl';// 設(shè)置ssl連接smtp服務(wù)器的遠程服務(wù)器端口號$mail->Port = 465;// 設(shè)置發(fā)送的郵件的編碼$mail->CharSet = 'UTF-8';// 設(shè)置發(fā)件人昵稱 顯示在收件人郵件的發(fā)件人郵箱地址前的發(fā)件人姓名$mail->FromName = '維修部信息處';// smtp登錄的賬號 QQ郵箱即可$mail->Username = '1234567890@qq.com';// smtp登錄的密碼 使用生成的授權(quán)碼$mail->Password = 'abcdefghijklmn';// 設(shè)置發(fā)件人郵箱地址 同登錄賬號$mail->From = '1234567890@qq.com';// 郵件正文是否為html編碼 注意此處是一個方法$mail->isHTML(true);// 設(shè)置收件人郵箱地址if(is_array($to)){foreach($to as $v){$mail->addAddress($v);}}else{$mail->addAddress($to);}$mail->addAddress('1234567890@qq.com');// 添加多個收件人 則多次調(diào)用方法即可//$mail->addAddress('1234567890@163.com');// 添加該郵件的主題$mail->Subject = $subject;// 添加郵件正文$email=$content;//$mail->Body = '<h1>Hello World</h1>';$mail->Body = $email;// 為該郵件添加附件//$mail->addAttachment('./example.pdf');//附加信息,可以省略$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; // 發(fā)送郵件 返回狀態(tài)return $status = $mail->send(); } send_email("1234567890@qq.com"); ?>資源下載
PHPMailer Github:?https://github.com/PHPMailer/PHPMailer/
參考文章
https://blog.csdn.net/weixin_43272781/article/details/102932918
https://blog.csdn.net/zhsp1029/article/details/2206483
總結(jié)
以上是生活随笔為你收集整理的PHPMailer——发送邮件函数封装的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP——获取路径和目录
- 下一篇: Nginx+PHP-FPM——Nginx