使用PHPMailer實(shí)現(xiàn)郵件的實(shí)時(shí)發(fā)送功能
今天我們利用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地址:github.com/PHPMailer/PHPMailer

我是直接下載上面的這個(gè)壓縮包,下載后解壓,層級(jí)一定要放對,不然無法調(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í)間。
到此這篇關(guān)于使用PHPMailer實(shí)現(xiàn)郵件的實(shí)時(shí)發(fā)送功能的文章就介紹到這了,更多相關(guān)PHPMailer郵件發(fā)送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
thinkphp在低版本Nginx 下支持PATHINFO的方法分享
本文給大家分享的是如何讓thinkPHP在低版本的Nginx下支持PATHINFO去掉index.php路徑的方法,十分的簡單實(shí)用,思路也很巧妙,有需要的小伙伴可以參考下2016-05-05
淺析虛擬主機(jī)服務(wù)器php fsockopen函數(shù)被禁用的解決辦法
以下是對虛擬主機(jī)服務(wù)器php fsockopen函數(shù)被禁用的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08
PHP Beanstalkd消息隊(duì)列的安裝與使用方法實(shí)例詳解
這篇文章主要介紹了PHP Beanstalkd消息隊(duì)列的安裝與使用方法,結(jié)合實(shí)例形式詳細(xì)分析了PHP Beanstalkd消息隊(duì)列的基本功能、原理、安裝、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-02-02
php中mkdir()函數(shù)的權(quán)限問題分析
這篇文章主要介紹了php中mkdir()函數(shù)的權(quán)限問題分析,需要的朋友可以參考下2016-09-09

