tp5实现邮件发送(无注释)

 

 HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>邮箱注册</title>
    <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
</head>

<body>
<form action="/home/login/peroper" method="post" style="width: 300px">
    <div class="form-group">
        <label for="name">对方邮箱</label>
        <input type="text" class="form-control" name="email" id="name">
    </div>
    <div class="form-group">
        <label for="name">标题</label>
        <input type="text" class="form-control" name="title" id="name">
    </div>

    <div class="form-group">
        <label for="name">内容</label>
        <input type="textarea" class="form-control" name="content" id="name">
    </div>

  <button type="submit" class="btn btn-success">立即邀请</button>

</form>

</body>
</html>

控制器代码:

    public function peroper()
    {
        //    接受邮箱参数
        $params = input();
        $email = $params['email'];//接受对方邮箱号码
        $title = $params['title'];//邮箱主题
        $content = $params['content'];//邮箱内容
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //服务器配置
            $mail->CharSet = "UTF-8";                     //设定邮件编码
            $mail->SMTPDebug = 0;                        // 调试模式输出
            $mail->isSMTP();                             // 使用SMTP
            $mail->Host = 'smtp.qq.com';                // SMTP服务器
            $mail->SMTPAuth = true;                      // 允许 SMTP 认证
            $mail->Username = '910*****58@qq.com';                // SMTP 用户名  即邮箱的用户名
            $mail->Password = 'syqyafshccsabbhh';             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
            $mail->SMTPSecure = 'ssl';                    // 允许 TLS 或者ssl协议
            $mail->Port = 465;                            // 服务器端口 25 或者465 具体要看邮箱服务器支持
            $mail->setFrom('910****58@qq.com', 'yanbing');  //发件人
            $mail->addAddress($email);  // 收件人
            //$mail->addAddress('ellen@example.com');  // 可添加多个收件人
            $mail->addReplyTo('910*****8@qq.com', 'yanbing'); //回复的时候回复给哪个邮箱 建议和发件人一致
            //$mail->addCC('cc@example.com');                    //抄送
            //$mail->addBCC('bcc@example.com');                    //密送

            //发送附件
            // $mail->addAttachment('../xy.zip');         // 添加附件
            // $mail->addAttachment('../thumb-1.jpg', 'new.jpg');    // 发送附件并且重命名
            //Content
            $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
            $mail->Subject = $title . time();
            $mail->Body = $content . date('Y-m-d H:i:s');
            $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';
            $mail->send();
            echo '邮件发送成功';

        } catch (Exception $e) {
            echo '邀请失败: ', $mail->ErrorInfo;
        }

    }

 

posted @ 2021-09-29 19:10  王越666  阅读(44)  评论(0编辑  收藏  举报