利用PHPMailer发送邮件


一、PHPMailer、composer下载以及使用

下载连接

 https://github.com/PHPMailer/PHPMailer

https://getcomposer.org/Composer-Setup.exe

PHPMailer使用

将src中的PHPMailer.php、SMTP.php、Exception.php放在项目文件之中

检查php中openssl和sockets是否开启

cmd 命令  php -m

检查php环境

cmd 命令 php -v

二、邮箱开启POP3/SMTP/IMAP服务


记住授权码后面会用

三、代码

 1.前端界面1

<!doctype html>
<html lang="en">
<head>
    <meta charset="">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<div class="box">
    <div class="top">
        
    </div>
    <form action="reset_password.php" method="POST">
        <input type="text" name="user" placeholder="用户名" class="text" value="<?php echo empty($_COOKIE["user"])?"":$_COOKIE["user"]?>">
        <label for="email">邮箱:</label>
        <input type="email" id="email" name="email" value="<?php echo empty($_COOKIE["email"])?"":$_COOKIE["email"]?>" required>

        <button type="submit">确定</button>


    </form>






    <div>
        <div class="bottom">
        </div>
    </div>

</div>
<script type="text/javascript">
    function check(){
    }
</script>
<style type="text/css">

    body{
        padding: 0;
        margin: 0;
        /*background-image: linear-gradient(to bottom,lightblue,cornflowerblue);*/
        display: flex;  /*弹性布局*/
        justify-content: center;
        align-items: center;
        height: 100vh;
        background-image: url("path/bg1.png");
    }
    .box{
        width: 240px;
        height: 500px;
        /*background-color: white;*/
        padding: 0 50px ;
        border-radius: 15px;/*圆角边框*/
    }
    .top{
        font-size: 35px;
        color: red;
        font-weight: bold;
        text-align: center;
        line-height: 170px;
    }
    .text{
        width: 100%;
        border: 0;
        border-bottom: solid 1px papayawhip;
        font-size: 20px;
        margin-bottom: 30px ;
        outline: none;
    }
    .button{
        width: 100%;
        border: 0;
        font-size: 25px;
        color: wheat;
        background-image: linear-gradient(to right,palevioletred,indianred);
        cursor:pointer; /*光标的类型*/
    }
    .bottom{
        line-height: 60px;
        font-size: 20px;
        text-align: center;
    }
    .expire{
        font-size: 20px;
        color: #1eacae;
        cursor: pointer;
    }
</style>
</body>
</html>

前端界面2

<!doctype html>
<html lang="en">
<head>
    <meta charset="">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<div class="box">
    <div class="top">
        登录
    </div>

   echo '发送成功';

    <div>
        <div class="bottom">
        </div>
    </div>

</div>

<style type="text/css">
    body{
        padding: 0;
        margin: 0;
        /*background-image: linear-gradient(to bottom,lightblue,cornflowerblue);*/
        display: flex;  /*弹性布局*/
        justify-content: center;
        align-items: center;
        height: 100vh;
        background-image: url("path/bg1.png");
    }
    .box{
        width: 240px;
        height: 500px;
        /*background-color: white;*/
        padding: 0 50px ;
        border-radius: 15px;/*圆角边框*/
    }
    .top{
        font-size: 35px;
        color: red;
        font-weight: bold;
        text-align: center;
        line-height: 170px;
    }
    .text{
        width: 100%;
        border: 0;
        border-bottom: solid 1px papayawhip;
        font-size: 20px;
        margin-bottom: 30px ;
        outline: none;
    }
    .button{
        width: 100%;
        border: 0;
        font-size: 25px;
        color: wheat;
        background-image: linear-gradient(to right,palevioletred,indianred);
        cursor:pointer; /*光标的类型*/
    }
    .bottom{
        line-height: 60px;
        font-size: 20px;
        text-align: center;
    }
    .expire{
        font-size: 20px;
        color: #1eacae;
        cursor: pointer;
    }
</style>
</body>
</html>

2.脚本语言

<?php
use PHPMailer\PHPMailer\PHPMailer;
//使用自己的文件路径
require 'path/Exception.php';
require 'path/PHPMailer.php';
require 'path/SMTP.php';
//改成自己的数据库
 $conn=mysqli_connect("localhost","root","root","new");
    $email = mysqli_real_escape_string($conn, $_POST['email']);


    //检查邮箱是否存在数据库中
    $sql = "SELECT * FROM user WHERE email='$email'";
    $result = mysqli_query($conn,$sql);
    if(mysqli_num_rows($result)<=0){
        $_SESSION['status'] = "账号不存在";
    }else{
        //生成并保存验证码
        $code = rand(1000,9999);//生成随机验证码
        $sql = "INSERT INTO reset_password (email, code) VALUES ('$email', '$code')";
        mysqli_query($conn, $sql);
        $title="We heard that you lost your password ";
 $mail = new PHPMailer(true);
 try {
 $mail->isSMTP();
$mail->SMTPAuth = true;
//163的邮箱SMTP格式可以换成其它邮箱格式
 $mail->Host = 'smtp.163.com';
//自己的邮箱
 $mail->Username = 'xxxxx@163.com';
 // 这个就是之前得到的授权码
  $mail->Password = 'xxxxxxxxxxxxx';
//设置使用ssl加密方式登录鉴权
    $mail->SMTPSecure = 'ssl';
  // //设置ssl连接smtp服务器的远程服务器端口号,可选465或587
    $mail->Port = 465;
 //设置发送的邮件的编码 也可选 GB2312
     $mail->CharSet = 'UTF-8';
 //发送放的邮箱也就是自己的邮箱
     $mail->setFrom('xxxxxxx@163.com', '标题');
 $mail->addAddress($email);
   //邮件正文是否为html编码 注意此处是一个方法 不再是属性 true或false
    $mail->isHTML(true);
      // 该邮件的主题
     $mail->Subject = $title;
      // 该邮件的正文内容
      $mail->Body = $code;
 $mail->send();
            echo '发送成功';
        }catch (\Exception $exception){
            echo '邮件发送失败: ', $mail->ErrorInfo;
        }
header("Location: reset2.php");
  }
}
?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用PHPMailer发送异步邮件的示例代码: ```php // 引入PHPMailer类 use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; // 异步发送邮件函数 function sendMailAsync($to, $subject, $body) { // 新建一个PHPMailer对象 $mail = new PHPMailer(true); try { // 配置SMTP服务器 $mail->isSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = 'username'; $mail->Password = 'password'; $mail->SMTPSecure = 'ssl'; $mail->Port = 465; // 配置邮件内容 $mail->setFrom('[email protected]', 'From Name'); $mail->addAddress($to); $mail->Subject = $subject; $mail->Body = $body; // 发送邮件 $mail->send(); return true; } catch (Exception $e) { // 发送失败,记录错误日志 error_log($e->getMessage()); return false; } } // 调用异步发送邮件函数 $to = '[email protected]'; $subject = 'Test Subject'; $body = 'Test Body'; sendMailAsync($to, $subject, $body); ``` 在这个示例中,我们使用PHPMailer类来发送邮件。我们首先引入这个类,然后定义了一个`sendMailAsync`函数来异步发送邮件。该函数接受收件人地址、邮件主题和邮件正文作为参数。在函数内部,我们新建了一个PHPMailer对象,并使用SMTP服务器配置了邮件发送选项。然后,我们配置了邮件的内容,并使用`send`方法来实际发送邮件。如果发送成功,函数返回`true`,否则返回`false`并记录错误日志。最后,我们调用异步发送邮件函数来发送邮件。 请注意,这个示例中的`sendMailAsync`函数并没有使用PHP的异步机制。如果您想使用PHP的异步机制来发送邮件,您可以使用Swoole扩展或其他类似的工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值