博主信息
博文 20
粉丝 0
评论 0
访问量 38341
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
在laravel中写的短信接口和邮箱接口
陈文鹏的博客
原创
1483人浏览过

<!--ValidateController类的开始-->

<?php


namespace App\Http\Controllers;



use App\Entity\TempPhone;

use App\Models\Result;

use App\Tool\SME\SendEmail;

use App\Tool\SMS\SendMessage;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;


class ValidateController extends Controller

{


    // 发送短信验证码

    public function sendSMS(Request $request)

    {

        //设置时区

        date_default_timezone_set('Asia/Shanghai');

        $Api_result = new Result();

        $phoneNumber = $request->input('phoneNumber', '');

        if ($phoneNumber == '') {

            $Api_result->status = 1;

            $Api_result->message = 'Phone number is empty';

            return $Api_result->toJson();

        }

//        if (strlen($phoneNumber) != 11 || !preg_match("/^1[34578]\d{9}$/", $phoneNumber)) {

//            $Api_result->status = 2;

//            $Api_result->message = '手机格式不正确';

//            return $Api_result->toJson();

//        }


        $sendMessage = new SendMessage();

        $code = '';

        $code =substr(str_shuffle("012345678901234567890123456789"), 0,6);

        $result = $sendMessage->sendMessage($phoneNumber,"{$code}");

//        echo "<pre>";

//        print_r($result);exit;

        $result = json_decode($result, JSON_UNESCAPED_UNICODE);

        DB::table('code_message')->insert(['yzmlog' => $result["message"], 'senddata' => date('Y-m-d H:i:s', time())]);

//        echo "<pre>";

//        print_r($result);exit;

        if ($result['status'] == 1) {

            $tempPhone = TempPhone::where('phoneNumber', $phoneNumber)->first();

            if ($tempPhone == null) {

                $tempPhone = new TempPhone;

            }

            $tempPhone->phoneNumber = $phoneNumber;

            $tempPhone->code = $code;

            $tempPhone->deadline = date('Y-m-d H:i:s', time() + 3 * 60);

            $tempPhone->save();

        }

        return $result['status'];

    }

    //邮件发送验证码

    public function sendEmail(Request $request){

        //设置时区

        date_default_timezone_set('Asia/Shanghai');

        $Api_result = new Result();

        $email = $request->input('email', '');

        $code =substr(str_shuffle("012345678901234567890123456789"), 0,6);

        $sendMail=new SendEmail();

        $sendMail->sendEmail($email,$code);

        $tempPhone = TempPhone::where('phoneNumber', $email)->first();

        if ($tempPhone == null) {

            $tempPhone = new TempPhone;

        }

        $tempPhone->phoneNumber = $email;

        $tempPhone->code = $code;

        $tempPhone->deadline = date('Y-m-d H:i:s', time() + 30 * 60);

        $tempPhone->save();

        $Api_result->status = 2;

        $Api_result->message = $email;

        return $Api_result->toJson();

    }

}

<!--ValidateController类的结束-->

<!--SendEmail类的开始封装了一个方法-->

<?php

/**

 * Created by PhpStorm.

 * User: Android-Dev

 * Date: 2018/1/17

 * Time: 12:13

 */


namespace App\Tool\SME;

use App\Models\EmailModel;

use Mail;


class SendEmail

{

    public function sendEmail($email,$code)

    {

        $emailModel = new EmailModel();

        $emailModel->to = $email;

        $emailModel->subject = 'HLWCHAIN ICO';

        $emailModel->content =$code;

//        $emailModel->content = 'We\'ve received your message and you will get our reply soon.';

//        $code =substr(str_shuffle("012345678901234567890123456789"), 0,6);;

        Mail::send('sendemail',['emailModel' => $emailModel],function($m) use ($emailModel) {

            $m->to($emailModel->to, 'Dear user')

                ->subject($emailModel->subject);

        });

    }

}

<!--SendEmail类的结束的方法-->

<!--EmailModel 模板的开始-->

<?php 


namespace App\Models;


class EmailModel {


  public $from;  // 发件人邮箱

  public $to; // 收件人邮箱

  public $cc; // 抄送

  public $attach; // 附件

  public $subject; // 主题

  public $content; // 内容


}

<!--EmailModel 模板的结束-->

<!--

自己定义个路由,然后注意命名空间有没有引用到,下面我用一个ajax的请求方式进行调用一下我们的接口

-->

<!--发送邮件开始-->

<script>

   var flag = true;

    $('#sendEmail').click(function (event) {

        var email = $('input[name=email]').val();

        if(email==""){

            alert("Email is not allowed to be empty!");

            return false;

        }

        if(email!=""){

            var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;

            if (!reg.test(email)) {

                alert("Email format is not correct");

                return;

            }

        }

        if (flag == false) {

            return;

        }

        flag = false;

        var num = 180;


        var interval = setInterval(function () {

            $("#sendEmail").css("background-color", "silver");

            $('#sendEmail').html(--num + 's Resend');

            if (num == 0) {

                flag = true;

                clearInterval(interval);

                $("#sendEmail").css("background-color", "white");

                $('#sendEmail').html('Send');

            }

        }, 1000);

        $.ajax({

            url: '/HlwChain/sendEmail',

            type: 'POST',

            dataType: 'json',

            cache: false,

            data: {

                email: email,

                _token: "{{csrf_token()}}"

            },

            success: function (data) {

                console.log(data);

                layer.msg('Verifying code has been sent');

                return false;

            }


        })

    });

</script>

<!--发送邮件结束-->

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学