使用PHPMailer實現(xiàn)郵件的實時發(fā)送功能
今天我們利用GitHub上20K+星星的項目 PHPMailer
實現(xiàn)一個接收詢盤并實時同步到指定郵箱的功能。
實現(xiàn)基本的HTML+CSS
首先我們用 HTML+CSS 做一個簡單的 form
表單
<div> <div> <div>You can contact us at anytime!</div> <form action="zuizhong.php" method="post"> <input type="text" name="inquiry_lam_name_footer" placeholder='Your Name'> <input type="text" name="inquiry_lam_email_footer" placeholder='Your E-mail'> <input type="text" name="inquiry_lam_phone_footer" placeholder='Your Phone'> <input type="text" name="inquiry_lam_address_footer" placeholder='Your Company Name'> <textarea name="inquiry_lam_message_footer" placeholder='Briefly describe your requirement'></textarea> <button type="submit">Send</button> </form> </div> </div>
加點 CSS
body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 0; } div { max-width: 600px; margin: 20px auto; padding: 20px; background-color: #fff; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } div > div { text-align: center; margin-bottom: 20px; } form input[type="text"], form textarea { width: 100%; padding: 10px; margin-bottom: 10px; border-radius: 5px; border: 1px solid #ccc; box-sizing: border-box; } form button { padding: 10px 20px; border: none; border-radius: 5px; background-color: #007bff; color: #fff; cursor: pointer; } form button:hover { background-color: #0056b3; }
此時表單顯示如下:
下載 PHPMailer 并配置
Github地址:github.com/PHPMailer/PHPMailer
我是直接下載上面的這個壓縮包,下載后解壓,層級一定要放對,不然無法調(diào)用。
獲取郵箱授權(quán)碼
這里我就以國內(nèi)使用最多的QQ郵箱為例,當(dāng)然其他郵箱也都類似,首先登錄網(wǎng)頁版QQ郵箱,找到設(shè)置——賬號
翻到下面找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務(wù),點擊管理服務(wù),有的可能沒開啟,需要先開啟服務(wù)
點擊生成授權(quán)碼,記得保存一下,后面需要用到
mail.php 示例代碼
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; $mail = new PHPMailer(true); try { $mail->isSMTP(); $mail->Host = 'smtp.qq.com'; //QQ郵箱用這個,跟我一樣就行 $mail->SMTPAuth = true; $mail->Username = '1836360247@qq.com'; //換成你的qq郵箱 $mail->Password = 'eqjnv*****achaa'; //就是剛剛的授權(quán)碼,用你的替換 $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->Port = 465; //默認(rèn)都是465 //Recipients $mail->setFrom('1836360247@qq.com', 'haiyong'); $mail->addAddress('208617432@qq.com', 'Joe User'); //添加收件人 // $mail->addAddress('208617432@qq.com'); //名字可加可不加,需要多個收件人,在后面增加就行 //郵件內(nèi)容 $mail->isHTML(true); $mail->Subject = '來自 海擁 的詢盤'; $mail->Body = '這是一封來自 <b>海擁</b> 的詢盤'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo '郵件已發(fā)送'; } catch (Exception $e) { echo "郵件未發(fā)送 Mailer Error: {$mail->ErrorInfo}"; }
測試一下,可成功收到郵件。
最終實現(xiàn)代碼
zuizhong.php
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; // 獲取表單提交的數(shù)據(jù) if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_POST['inquiry_lam_name_footer'] ?? ''; $email = $_POST['inquiry_lam_email_footer'] ?? ''; $phone = $_POST['inquiry_lam_phone_footer'] ?? ''; $company = $_POST['inquiry_lam_address_footer'] ?? ''; $message = $_POST['inquiry_lam_message_footer'] ?? ''; // 獲取當(dāng)前時間 date_default_timezone_set('Your_Timezone'); // 設(shè)置您所在的時區(qū) $currentTime = date('Y-m-d H:i:s'); // 構(gòu)建保存到文件的內(nèi)容 $data = "Time: $currentTime\nName: $name\nEmail: $email\nPhone: $phone\nCompany: $company\nMessage: $message\n\n"; // 打開或創(chuàng)建一個文件用于寫入 $file = fopen("user_data.php", "a"); // 'a' 模式表示追加寫入 // if ($file) { // // 寫入數(shù)據(jù)到文件 // fwrite($file, $data); // fclose($file); if ($file) { // 解碼 HTML 實體編碼,并轉(zhuǎn)換為 UTF-8 編碼,然后將數(shù)據(jù)直接寫入文件 $decodedData = mb_convert_encoding(html_entity_decode($data, ENT_QUOTES | ENT_HTML5, 'UTF-8'), 'UTF-8'); fwrite($file, "\xEF\xBB\xBF"); // 添加 UTF-8 BOM,確保以 UTF-8 編碼打開 fwrite($file, $decodedData); fclose($file); // 構(gòu)建 HTML 內(nèi)容,每個字段后添加 <br> 標(biāo)簽來換行 $htmlContent = "<strong>Time:</strong> $currentTime<br>" . "<strong>Name:</strong> $name<br>" . "<strong>Email:</strong> $email<br>" . "<strong>Phone:</strong> $phone<br>" . "<strong>Company:</strong> $company<br>" . "<strong>Message:</strong> $message<br><br>"; // 使用 <br> 換行,并添加額外的 <br> 產(chǎn)生兩行間隔 // 發(fā)送郵件 $mail = new PHPMailer(true); try { //Server settings $mail->isSMTP(); $mail->Host = 'smtp.qq.com'; //QQ郵箱用這個,跟我一樣就行 $mail->SMTPAuth = true; $mail->Username = '1836360247@qq.com'; //換成你的郵箱 $mail->Password = 'eqj******haa'; //你的授權(quán)碼 $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->Port = 465; //不用改,一般都是465 $mail->setFrom('1836360247@qq.com', 'haiyong'); $mail->addAddress('208617432@qq.com', 'hy2'); $mail->addAddress('haiyong314@163.com', 'hy3'); //收件人,可無限加 //郵件內(nèi)容 $mail->isHTML(true); $mail->Subject = 'New Contact Form haiyong.site'; $mail->Body = $htmlContent; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. haiyong Error: {$mail->ErrorInfo}"; } // 如果郵件發(fā)送成功或失敗,重定向到 contactsave.html 頁面 header("Location: contactsave.html"); exit(); } else { echo "Error opening file."; } } ?>
表單填寫內(nèi)容
后臺 user_data.php
文件內(nèi)顯示
QQ郵箱收到的內(nèi)容
成功接收郵件,統(tǒng)計放入了 user_data.php
文件,并顯示出了此時時間。
到此這篇關(guān)于使用PHPMailer實現(xiàn)郵件的實時發(fā)送功能的文章就介紹到這了,更多相關(guān)PHPMailer郵件發(fā)送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php park、unpark、ord 函數(shù)使用方法(二進(jìn)制流接口應(yīng)用實例)
在工作中,我也逐漸了解到park,unpark,ord對于二進(jìn)制字節(jié)處理的強大。 下面我逐一介紹它們。2010-10-10php實現(xiàn)圖片上傳并利用ImageMagick生成縮略圖
這篇文章主要為大家詳細(xì)介紹了php實現(xiàn)圖片上傳并利用ImageMagick生成縮略圖的相關(guān)資料,需要的朋友可以參考下2016-03-03Yii2——使用數(shù)據(jù)庫操作匯總(增刪查改、事務(wù))
本篇文章主要介紹了Yii2——使用數(shù)據(jù)庫操作匯總,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12PHP改進(jìn)計算字符串相似度的函數(shù)similar_text()、levenshtein()
PHP 原生的similar_text()函數(shù)、levenshtein()函數(shù)對中文漢字支持不好,我自己寫了一個,測試使用正常,推薦給大家,如果有什么問題,請留言2014-10-10php中判斷數(shù)組相等的方法以及數(shù)組運算符介紹
這篇文章主要介紹了php中判斷數(shù)組相等的方法以及數(shù)組運算符介紹,本文講解了相關(guān)知識并給出實例代碼,需要的朋友可以參考下2015-03-03