利用宝塔邮件系统发送邮件或phpmailer发送邮件及邮件验证

本文介绍了如何通过PHPMailer库和宝塔面板内置的邮件系统来发送邮件,包括设置和验证过程。
摘要由CSDN通过智能技术生成
<?php

class Ems
{
    /**
     * @title 发送验证码
     * @description 发送验证码
     *
     *@param name:mail type:string require:1  other:邮箱账号
     *@param name:event type:int require:0  other:事件,如注册regist,登录login,忘记密码forgetpwd
     *
     */
    public function sendMail()
    {

        //接收数据
        $data = [
            'mail' => input("mail") ?: 'abc',
        ];
        $event = input("event") ?: 'regist';
        $uid = input("uid") ?:0;

        //验证收据有效性
        $rules = [
            'mail|邮箱地址' => 'regex:^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$',
        ];
        $validate = new Validate($rules);
        if (!$validate->check($data)) {
            $this->error($validate->getError(),'',0);
        }


        //发送手机验证码
        return $this->sendEMailCode($data['mail'],$uid,$event);
    }

    protected function sendEMailCode($email,$uid,$event)
    {
        //判断发送条件
        $sendornot = $this-> sendMailCode($email,$uid);
        if(!$sendornot['issend']){
            $this->error($sendornot['info'],'',0);
        }


        if ($event) {
            $UserInfo = new UserInfo();
            $userinfo = $UserInfo->findUserByWhere(['email'=>$email]);
            if ($event == 'regist' && $userinfo) {
                //已被注册
                $this->error('已被注册','',0);
            } elseif (in_array($event, ['forgetpwd','login']) && !$userinfo) {
                //未注册
                $this->error('未注册','',0);
            }
        }

        //发送短信
        $subject = "邮箱验证";
        $content = '您好' . $email . '!您的****邮箱验证码为' . $sendornot['code'] . '。';
        $res = $this->send_mail($email, $subject, $content);
        if ($res) {
            $this->success('邮件发送成功,请注意查收!','',1);
        } else {
            $this->error('邮件发送失败请重试!','',0);
        }
    }

    protected function sendMailCode($email,$uid){
        //从数据库取值
        $EmailCodeLog = new EmailCodeLog();
        $mobile_code_mobile = $EmailCodeLog-> seletCodeByEmail($email);

        //如果表中存在该邮箱且有效并未使用的验证码时,返回该验证码
        if($mobile_code_mobile && $mobile_code_mobile['is_use'] == 0){
            $sendornot['issend'] = 1;
            $sendornot['code'] = $mobile_code_mobile['code'];
            $sendornot['id'] = $mobile_code_mobile['id'];
            return $sendornot;
        }

        //判断该邮箱请求的验证码的次数,在一个小时内是否达到了最大的请求次数,返回错误信息
        $email_code_mobile_count = $EmailCodeLog-> countCodeNumByEmailInHour($email);
        if(count($email_code_mobile_count)>=30){
            $this->error('手机号码请求过于频繁,请稍后再试','',0);
        }

        //数据库中没有值的话发送短信验证码
        $code = rand(100000, 999999) . '';//产生随机数
        $sendornot['issend'] = 1;
        $sendornot['code'] = $code;


        //添加数据库信息
        $insert_data = [
            'email' => $email,
            'code' => $code,
            'status' => 0,
        ];

        $addDataResult = $EmailCodeLog-> insertEmailSendLog($insert_data);
        $sendornot['id'] = $addDataResult;

        return $sendornot;
    }

    //利用phpmailer发送邮件
    protected function send_email($address, $subject, $content)
    {
        $mail = new \app\api\common\Emsend();
        $mail->setServer("smtp.126.com", "XXX@126.com", "XXX");
        //如果不需要到服务器的SSL连接,这样设置服务器:$mail->setServer("smtp.126.com", "XXX@126.com", "XXX");
        $mail->setFrom("邮箱账号");
        $mail->setReceiver($address);

        $mail->setMail($subject, $content);
        $state = $mail->sendMail();

        if (!$state) {

            //var_dump($phpmailer->ErrorInfo);
            $phpmailererror = $phpmailer->ErrorInfo;
            return array("error" => 1, "message" => $phpmailererror);
        } else {
            return array("error" => 0);
        }
    }

    //验证邮箱验证码
    public function erification($mail,$code)
    {
        $EmailCodeLog = new EmailCodeLog();
        $mailCodeInfo = $EmailCodeLog-> findCodeByEmail($mail);


        //判断验证码存不存在
        if(!$mailCodeInfo){
            $msg['status'] = 0;
            $msg['info'] = '请先获取验证码';

            return $msg;
        }

        //判断是否失效
        if($mailCodeInfo['is_use']){
            $msg['status'] = 0;
            $msg['info'] = '验证码失效,请重新获取';

            return $msg;
        }

        //判断是否过期
        if($mailCodeInfo['expire_time'] < time()){
            $msg['status'] = 0;
            $msg['info'] = '验证码过
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值