使用phpmailer發送郵件,且需要激活完整代碼

1、需要下載phpMail包

2、數據庫需要字段爲token(郵箱激活碼),token_exptime(激活碼有效期),status(是否被激活0,1)

//頁面 代碼,

<body>
<h1>歡迎{$user}登錄</h1>
<form action="" method="post" id="form">
<table border='1'>
姓名:<input type="text" name="user" id="user"/><br/>
密碼:<input type="password" name="pwd"  id="pwd"/><br/>
確認密碼:<input type="password" name="confirmPwd" id="confirmPwd"/><br/>
性別:<input type="radio" value="男" id="sex" name="sex"/><input type="radio" id="sex" value="女" name="sex"/> <br/>
郵箱:<input type="text" name="mail"  id="mail"/>&nbsp;&nbsp;&nbsp; <input type="button" name="confirmMail" id="confirmMail" value="郵箱確認"><br/>
電話號碼:<input type="text" name="phone" id="phone" /><br/>
<input type="button" name="sub" id="sub" value="提交"/>
</table>
</form>

<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js">
</script>
<script>

$("#sub").on('click',function(){
  
    var confirmPwd = $("#confirmPwd").val();
    var user       = $("#user").val();
    var pwd        = $("#pwd").val();
    var sex           = $("#sex").val();
    var mail       = $("#mail").val();
    var phone      = $("#phone").val();
    var url           = "__URL__/addUser";
    if(user==''){
        alert("用戶名不能爲空");        
    }else if(pwd==''){
        alert("密碼不能爲空");
    }else if(confirmPwd==""){
        alert("確認密碼不能爲空");
    }else if(pwd!=confirmPwd){
        alert("密碼不一致,請重新確認密碼");
    }else if(mail==""){
        alert("郵箱地址不能爲空");
    }else{
        $.post("__URL__/addUser",{mail:mail,user:user,pwd:pwd,sex:sex,phone:phone},function(data){
            
            if(data==1){
                location.href = '__URL__/index';
            }else{
                alert('註冊失敗');
            }
            
        },'JSON');
    }
})

</script>
</body>


//控制器代碼

    //添加模塊,當點擊註冊時候,調用sendMail方法
    public function addUser(){
        $model = M();
        $phone = $_POST['phone'];
        $user  = $_POST["user"];
        $pwd   = $_POST["pwd"];
        $sex   = $_POST['sex'];
        $mail  = $_POST['mail'];
        
        //定義激活碼
        $token = md5($user.$pwd.time());
        $token_exptime = time()+60*60*24;
        
        $where = array(
                'token_exptime' =>$token_exptime,
                'password' => md5($pwd),
                'phone'    => $phone,
                'token'       => $token,
                'mail'     => $mail,
                'name'     => $user,
                'sex'      => $sex,
        );
        
        $add = $model->table('tp_user')->add($where);
        $this->sendMail($mail,$user,$token);
        if($add){
            echo '1';
        }else{
            echo "2";
        }
    }


/sendMail方法,調用think_send_mail方法執行發送郵件
    public function sendMail($mail,$user,$token){
    $body = "親愛的".$user.":<br/>感謝您在我站註冊了新帳號。<br/>請點擊鏈接激活您的帳號。<br/>
    <a href='http://localhost/thinkphp/index.php/Home/Index/update?token=$token' target='_blank'>http://localhost/thinkphp/index.php/Home/Index/update?token=$token</a><br/>如果以上鍊接無法點擊,請將它複製到你的瀏覽器地址欄中進入訪問,該鏈接24小時內有效。";
        $r = $this->think_send_mail($mail,$user,'歡迎註冊',$body);
    }


//發送郵件方法
    public function think_send_mail($to, $name, $subject = '', $body = '', $attachment = null){
            header("Content-Type: text/html; charset=utf-8");
        
            $config = C('THINK_EMAIL');
        
            vendor('PHPMailer.class#phpmailer'); //從PHPMailer目錄導class.phpmailer.php類文件
            $mail             = new \PHPMailer(); //PHPMailer對象
            $mail->CharSet    = 'UTF-8'; //設定郵件編碼,默認ISO-8859-1,如果發中文此項必須設置,否則亂碼
            $mail->IsSMTP();  // 設定使用SMTP服務
            $mail->SMTPDebug  = 1;                     // 關閉SMTP調試功能
            $mail->SMTPAuth   = true;                  // 啓用 SMTP 驗證功能
            $mail->Host       = $config['SMTP_HOST'];  // SMTP 服務器
            $mail->Port       = $config['SMTP_PORT'];  // SMTP服務器的端口號
            $mail->Username   = $config['SMTP_USER'];  // SMTP服務器用戶名
            $mail->Password   = $config['SMTP_PASS'];  // SMTP服務器密碼
            
            $mail->SetFrom($config['FROM_EMAIL'], $config['FROM_NAME']);
            $replyEmail       = $config['REPLY_EMAIL']?$config['REPLY_EMAIL']:$config['FROM_EMAIL'];
            $replyName        = $config['REPLY_NAME']?$config['REPLY_NAME']:$config['FROM_NAME'];
            $mail->AddReplyTo($replyEmail, $replyName);
            $mail->Subject    = $subject;
            $mail->AltBody    = "爲了查看該郵件,請切換到支持 HTML 的郵件客戶端";
            $username ='AAAA';
            $mail->MsgHTML($body);
            $mail->AddAddress($to, '342772383');
            if(is_array($attachment)){ // 添加附件
                foreach ($attachment as $file){
                    is_file($file) && $mail->AddAttachment($file);
                }
            }
            return  $mail->Send() ? true : $mail->ErrorInfo;
    }



//config中 郵件配置

    //郵件配置
        'THINK_EMAIL' => array(
                'SMTP_HOST'   => 'smtp.163.com', //SMTP服務器
                'SMTP_PORT'   => '25', //SMTP服務器端口
                'SMTP_USER'   => '[email protected]', //SMTP服務器用戶名
                'SMTP_PASS'   => 'aaa', //SMTP服務器密碼
                'FROM_EMAIL'  => '[email protected]', //發件人EMAIL  和SMTP服務器用戶名保持一致
                'FROM_NAME'   => '易享微代', //發件人名稱
                'REPLY_EMAIL' => '', //回覆EMAIL(留空則爲發件人EMAIL)
                'REPLY_NAME'  => 'AAAAA', //回覆名稱(留空則爲發件人名稱)
        ),

1、注意  需要把網易郵箱中STMP,POP3開啓

2、SMTP服務器用戶名需要和發件人EMAIL一致

3、關閉SSL安全認證



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章