当前位置: 代码网 > it编程>编程语言>Php > 使用PHPMailer实现邮件的实时发送功能

使用PHPMailer实现邮件的实时发送功能

2024年05月18日 Php 我要评论
今天我们利用github上20k+星星的项目 phpmailer 实现一个接收询盘并实时同步到指定邮箱的功能。实现基本的html+css首先我们用 html+css 做一个简单的 form 表单<

今天我们利用github上20k+星星的项目 phpmailer 实现一个接收询盘并实时同步到指定邮箱的功能。

实现基本的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

我是直接下载上面的这个压缩包,下载后解压,层级一定要放对,不然无法调用。

获取邮箱授权码

这里我就以国内使用最多的qq邮箱为例,当然其他邮箱也都类似,首先登录网页版qq邮箱,找到设置——账号

翻到下面找到pop3/imap/smtp/exchange/carddav/caldav服务,点击管理服务,有的可能没开启,需要先开启服务

点击生成授权码,记得保存一下,后面需要用到

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';                               //就是刚刚的授权码,用你的替换
    $mail->smtpsecure = phpmailer::encryption_smtps;            
    $mail->port       = 465;                                    //默认都是465

    //recipients
    $mail->setfrom('1836360247@qq.com', 'haiyong');
    $mail->addaddress('208617432@qq.com', 'joe user');     //添加收件人
    // $mail->addaddress('208617432@qq.com');               //名字可加可不加,需要多个收件人,在后面增加就行

    //邮件内容
    $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

<?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"); // 'a' 模式表示追加写入
    // if ($file) {
    //     // 写入数据到文件
    //     fwrite($file, $data);
    //     fclose($file);
    if ($file) {
    // 解码 html 实体编码,并转换为 utf-8 编码,然后将数据直接写入文件
    $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);
        
        // 构建 html 内容,每个字段后添加 <br> 标签来换行
        $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> 产生两行间隔

        // 发送邮件
        $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';                               //你的授权码
            $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');     //收件人,可无限加
            
            //邮件内容
            $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}";
        }
        
        // 如果邮件发送成功或失败,重定向到 contactsave.html 页面
        header("location: contactsave.html");
        exit();
    } else {
        echo "error opening file.";
    }
}
?>

表单填写内容

后台 user_data.php 文件内显示

qq邮箱收到的内容

成功接收邮件,统计放入了 user_data.php 文件,并显示出了此时时间。

到此这篇关于使用phpmailer实现邮件的实时发送功能的文章就介绍到这了,更多相关phpmailer邮件发送内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com