- ?? 個(gè)人網(wǎng)站:【 海擁】【神級(jí)代碼資源網(wǎng)站】【辦公神器】
- ?? 基于Web端打造的:??輕量化工具創(chuàng)作平臺(tái)
- ?? 想尋找共同學(xué)習(xí)交流的小伙伴,請(qǐng)點(diǎn)擊【全棧技術(shù)交流群】
今天我們利用GitHub上20K+星星的項(xiàng)目 PHPMailer
實(shí)現(xiàn)一個(gè)接收詢盤并實(shí)時(shí)同步到指定郵箱的功能。
實(shí)現(xiàn)基本的HTML+CSS
首先我們用 HTML+CSS 做一個(gè)簡單的 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>
加點(diǎn) 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;
}
此時(shí)表單顯示如下:
下載 PHPMailer 并配置
Github地址:https://github.com/PHPMailer/PHPMailer
我是直接下載上面的這個(gè)壓縮包,下載后解壓,層級(jí)一定要放對(duì),不然無法調(diào)用。
獲取郵箱授權(quán)碼
這里我就以國內(nèi)使用最多的QQ郵箱為例,當(dāng)然其他郵箱也都類似,首先登錄網(wǎng)頁版QQ郵箱,找到設(shè)置——賬號(hào)
翻到下面找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務(wù),點(diǎn)擊管理服務(wù),有的可能沒開啟,需要先開啟服務(wù)
點(diǎn)擊生成授權(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郵箱用這個(gè),跟我一樣就行
$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'); //名字可加可不加,需要多個(gè)收件人,在后面增加就行
//郵件內(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}";
}
測試一下,可成功收到郵件。
最終實(shí)現(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)前時(shí)間
date_default_timezone_set('Your_Timezone'); // 設(shè)置您所在的時(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)建一個(gè)文件用于寫入
$file = fopen("user_data.php", "a"); // 'a' 模式表示追加寫入
// if ($file) {
// // 寫入數(shù)據(jù)到文件
// fwrite($file, $data);
// fclose($file);
if ($file) {
// 解碼 HTML 實(shí)體編碼,并轉(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)容,每個(gè)字段后添加 <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郵箱用這個(gè),跟我一樣就行
$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)容
后臺(tái) user_data.php
文件內(nèi)顯示
QQ郵箱收到的內(nèi)容
成功接收郵件,統(tǒng)計(jì)放入了 user_data.php
文件,并顯示出了此時(shí)時(shí)間。到這里我們就完整實(shí)現(xiàn)了使用 PHPMailer 進(jìn)行郵件的實(shí)時(shí)發(fā)送,希望本篇文章能幫助到大家。
?? 好書推薦
《數(shù)學(xué)之美》
【內(nèi)容簡介】
本書從幾個(gè)著名數(shù)學(xué)問題出發(fā),深入淺出地講解了與我國初高中的教學(xué)實(shí)際緊密聯(lián)系的數(shù)學(xué)知識(shí),并把知識(shí)內(nèi)容與數(shù)學(xué)核心素養(yǎng)結(jié)合起來。在這條知識(shí)主線的周邊,穿插介紹知識(shí)內(nèi)容的歷史發(fā)展過程,對(duì)相關(guān)數(shù)學(xué)分支在數(shù)學(xué)史上的地位進(jìn)行深入思考,并輔之以數(shù)學(xué)文化、趣味知識(shí)、數(shù)學(xué)游戲、數(shù)學(xué)悖論等茂盛枝葉。全書共6章,第1章介紹無處不在的楊輝三角;第2章介紹當(dāng)我們談?wù)撜襟w時(shí),我們能夠談?wù)撔┦裁?;?章介紹了神奇的 2;第4章介紹斐波那契數(shù)列與黃金分割;第5章介紹圓錐曲線面面觀;第6章介紹感悟數(shù)學(xué)的魅力與威力。
?? 京東購買鏈接:《數(shù)學(xué)之美》
《光的共鳴:人像板繪原理與技法》
【內(nèi)容簡介】
本書共6章,以頗受插畫師青睞的Procreate軟件為主要工具,帶領(lǐng)讀者提升人物繪畫創(chuàng)作的技能和技巧。書中包括對(duì)比例與結(jié)構(gòu)、光影二分法、直接畫法、色彩與光影的關(guān)系處理、繪畫的心得與技巧等的介紹,內(nèi)容涉及繪制頭部時(shí)不同角度的比例表現(xiàn)、平光和陰天光線的表現(xiàn)方法、通透的皮膚質(zhì)感表現(xiàn)方法、頭發(fā)的層次和質(zhì)感表現(xiàn)方法、二次元繪畫的表現(xiàn)方法等,講解分析了30余個(gè)典型案例,并在第 6 章展示了大量供讀者學(xué)習(xí)借鑒的光影表現(xiàn)作品。文章來源:http://www.zghlxwxcb.cn/news/detail-753176.html
?? 京東購買鏈接:《光的共鳴:人像板繪原理與技法》文章來源地址http://www.zghlxwxcb.cn/news/detail-753176.html
到了這里,關(guān)于使用 PHPMailer 實(shí)現(xiàn)郵件的實(shí)時(shí)發(fā)送的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!