今天我们利用GitHub上20K+星星的项目 PHPMailer
实现一个接收询盘并实时同步到指定邮箱的功能。
实现基本的HTML+CSS 首先我们用 HTML+CSS 做一个简单的 form
表单
1 2 3 4 5 6 7 8 9 10 11 12 13 <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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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地址:https://github.com/PHPMailer/PHPMailer
我是直接下载上面的这个压缩包,下载后解压,层级一定要放对,不然无法调用。
获取邮箱授权码 这里我就以国内使用最多的QQ邮箱为例,当然其他邮箱也都类似,首先登录网页版QQ邮箱,找到设置——账号
翻到下面找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,点击管理服务,有的可能没开启,需要先开启服务
点击生成授权码,记得保存一下,后面需要用到
mail.php 示例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <?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' ; $mail ->SMTPAuth = true ; $mail ->Username = '1836360247@qq.com' ; $mail ->Password = 'eqjnv*****achaa' ; $mail ->SMTPSecure = PHPMailer ::ENCRYPTION_SMTPS ; $mail ->Port = 465 ; $mail ->setFrom ('1836360247@qq.com' , 'haiyong' ); $mail ->addAddress ('208617432@qq.com' , 'Joe User' ); $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 '邮件已发送' ; } catch (Exception $e ) { echo "邮件未发送 Mailer Error: {$mail->ErrorInfo} " ; }
测试一下,可成功收到邮件。
最终实现代码 zuizhong.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 <?php use PHPMailer \PHPMailer \PHPMailer ;use PHPMailer \PHPMailer \Exception ;require 'PHPMailer/src/Exception.php' ;require 'PHPMailer/src/PHPMailer.php' ;require 'PHPMailer/src/SMTP.php' ;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' ] ?? '' ; date_default_timezone_set ('Your_Timezone' ); $currentTime = date ('Y-m-d H:i:s' ); $data = "Time: $currentTime \nName: $name \nEmail: $email \nPhone: $phone \nCompany: $company \nMessage: $message \n\n" ; $file = fopen ("user_data.php" , "a" ); if ($file ) { $decodedData = mb_convert_encoding (html_entity_decode ($data , ENT_QUOTES | ENT_HTML5, 'UTF-8' ), 'UTF-8' ); fwrite ($file , "\xEF\xBB\xBF" ); fwrite ($file , $decodedData ); fclose ($file ); $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>" ; $mail = new PHPMailer (true ); try { $mail ->isSMTP (); $mail ->Host = 'smtp.qq.com' ; $mail ->SMTPAuth = true ; $mail ->Username = '1836360247@qq.com' ; $mail ->Password = 'eqj******haa' ; $mail ->SMTPSecure = PHPMailer ::ENCRYPTION_SMTPS ; $mail ->Port = 465 ; $mail ->setFrom ('1836360247@qq.com' , 'haiyong' ); $mail ->addAddress ('208617432@qq.com' , 'hy2' ); $mail ->addAddress ('haiyong314@163.com' , 'hy3' ); $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} " ; } header ("Location: contactsave.html" ); exit (); } else { echo "Error opening file." ; } } ?>
表单填写内容
后台 user_data.php
文件内显示
QQ邮箱收到的内容
成功接收邮件,统计放入了 user_data.php
文件,并显示出了此时时间。到这里我们就完整实现了使用 PHPMailer 进行邮件的实时发送,希望本篇文章能帮助到大家。