国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHPMAILER實現PHP發郵件功能

2024-05-05 00:03:15
字體:
來源:轉載
供稿:網友

本文實例為大家分享了PHPMAILER實現PHP發郵件功能的具體代碼,供大家參考,具體內容如下

第一步:打開網址下載PHPMailer,PHPMailer 需要 PHP 的 sockets 擴展支持,而登錄 QQ 郵箱 SMTP 服務器則必須通過 SSL 加密的, PHP 還得包含 openssl 的支持。

phpmailer,PHP,郵件

第二步:使用 phpinfo() 函數查看 socket 和 openssl 擴展信息(wamp server 默認啟用了該擴展)。

openssl 如果沒有開啟請打開php.ini文件進行開啟

首先檢查php.ini中;extension=php_openssl.dll是否存在, 如果存在的話去掉前面的注釋符‘;', 如果不存在這行,那么添加extension=php_openssl.dll。

phpmailer,PHP,郵件

PHPMailer 核心文件

phpmailer,PHP,郵件

第三步:QQ 郵箱設置

所有的主流郵箱都支持 SMTP 協議,但并非所有郵箱都默認開啟,您可以在郵箱的設置里面手動開啟。

第三方服務在提供了賬號和密碼之后就可以登錄 SMTP 服務器,通過它來控制郵件的中轉方式。

第四步:開啟 SMTP 服務

phpmailer,PHP,郵件

選擇 IMAP/SMTP 服務,點擊開啟服務

第五步:驗證密保

phpmailer,PHP,郵件

發送短信“配置郵件客戶端”至1069-0700-69

第六步:獲取授權碼

phpmailer,PHP,郵件

SMTP 服務器認證密碼,需要妥善保管(PS:密碼直接沒有空格)

第七步:PHP發送郵件

基本代碼

下面的代碼演示了 PHPMailer 的使用方法,注意 PHPMailer 實例的配置過程。

// 引入PHPMailer的核心文件require_once("PHPMailer/class.phpmailer.php");require_once("PHPMailer/class.smtp.php"); // 實例化PHPMailer核心類$mail = new PHPMailer();// 是否啟用smtp的debug進行調試 開發環境建議開啟 生產環境注釋掉即可 默認關閉debug調試模式$mail->SMTPDebug = 1;// 使用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 = '12345678@qq.com';// smtp登錄的密碼 使用生成的授權碼$mail->Password = '**********';// 設置發件人郵箱地址 同登錄賬號$mail->From = '12345678@qq.com';// 郵件正文是否為html編碼 注意此處是一個方法$mail->isHTML(true);// 設置收件人郵箱地址$mail->addAddress('87654321@qq.com');// 添加多個收件人 則多次調用方法即可$mail->addAddress('87654321@163.com');// 添加該郵件的主題$mail->Subject = '郵件主題';// 添加郵件正文$mail->Body = '<h1>Hello World</h1>';// 為該郵件添加附件$mail->addAttachment('./example.pdf');// 發送郵件 返回狀態$status = $mail->send(); 

我在thinkphp5.0中使用代碼

/*** 郵件發送* @param $to 接收人* @param string $subject 郵件標題* @param string $content 郵件內容(html模板渲染后的內容)* @throws Exception* @throws phpmailerException*/function send_email($to,$subject='',$content=''){  vendor('phpmailer.PHPMailerAutoload');//require_once 'vendor/phpmailer/PHPMailerAutoload.php';  $mail = new PHPMailer;  $arr = db('config')->where('inc_type','smtp')->select();  $config = convert_arr_kv($arr,'name','value');  $mail->CharSet = 'UTF-8'; //設定郵件編碼,默認ISO-8859-1,如果發中文此項必須設置,否則亂碼  $mail->isSMTP();//Enable SMTP debugging// 0 = off (for production use)// 1 = client messages// 2 = client and server messages  $mail->SMTPDebug = 0;//調試輸出格式//$mail->Debugoutput = 'html';//smtp服務器  $mail->Host = $config['smtp_server'];//端口 - likely to be 25, 465 or 587  $mail->Port = $config['smtp_port'];     if($mail->Port === 465) $mail->SMTPSecure = 'ssl';// 使用安全協議//Whether to use SMTP authentication  $mail->SMTPAuth = true;//發送郵箱  $mail->Username = $config['smtp_user'];//密碼  $mail->Password = $config['smtp_pwd'];//Set who the message is to be sent from  $mail->setFrom($config['smtp_user'],$config['email_id']);//回復地址//$mail->addReplyTo('replyto@example.com', 'First Last');//接收郵件方  if(is_array($to)){    foreach ($to as $v){      $mail->addAddress($v);    }  }else{    $mail->addAddress($to);  }     $mail->isHTML(true);// send as HTML//標題  $mail->Subject = $subject;//HTML內容轉換  $mail->msgHTML($content);//Replace the plain text body with one created manually//$mail->AltBody = 'This is a plain-text message body';//添加附件//$mail->addAttachment('images/phpmailer_mini.png');//send the message, check for errors  return $mail->send();}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 油尖旺区| 许昌市| 临桂县| 广汉市| 阳新县| 阳谷县| 泊头市| 贵港市| 厦门市| 怀宁县| 肇源县| 南和县| 保德县| 高平市| 玉田县| 大关县| 濮阳市| 太仓市| 日喀则市| 武鸣县| 敖汉旗| 黎川县| 米泉市| 新兴县| 仁怀市| 长白| 黑山县| 会昌县| 沈丘县| 洪江市| 青海省| 青铜峡市| 中方县| 泗洪县| 松江区| 安西县| 樟树市| 勐海县| 新巴尔虎左旗| 项城市| 镇巴县|