PHPMailer——发送邮件函数封装
生活随笔
收集整理的這篇文章主要介紹了
PHPMailer——发送邮件函数封装
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
源代碼?
<?php /** * 郵件發送 * @param $to 接收人 * @param string $subject 郵件標題 * @param string $content 郵件內容(html模板渲染后的內容) * @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進行調試 開發環境建議開啟 生產環境注釋掉即可 默認關閉debug調試模式//Enable SMTP debugging// 0 = off (for production use)// 1 = client messages// 2 = client and server messages$mail->SMTPDebug = 1;//調試輸出格式//$mail->Debugoutput = 'html';// 使用smtp鑒權方式發送郵件$mail->isSMTP();// smtp需要鑒權 這個必須是true$mail->SMTPAuth = true;// 鏈接qq域名郵箱的服務器地址$mail->Host = 'smtp.qq.com';// 設置使用ssl加密方式登錄鑒權$mail->SMTPSecure = 'ssl';// 設置ssl連接smtp服務器的遠程服務器端口號$mail->Port = 465;// 設置發送的郵件的編碼$mail->CharSet = 'UTF-8';// 設置發件人昵稱 顯示在收件人郵件的發件人郵箱地址前的發件人姓名$mail->FromName = '維修部信息處';// smtp登錄的賬號 QQ郵箱即可$mail->Username = '1234567890@qq.com';// smtp登錄的密碼 使用生成的授權碼$mail->Password = 'abcdefghijklmn';// 設置發件人郵箱地址 同登錄賬號$mail->From = '1234567890@qq.com';// 郵件正文是否為html編碼 注意此處是一個方法$mail->isHTML(true);// 設置收件人郵箱地址if(is_array($to)){foreach($to as $v){$mail->addAddress($v);}}else{$mail->addAddress($to);}$mail->addAddress('1234567890@qq.com');// 添加多個收件人 則多次調用方法即可//$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"; // 發送郵件 返回狀態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
總結
以上是生活随笔為你收集整理的PHPMailer——发送邮件函数封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP——获取路径和目录
- 下一篇: JAVA四大名著