Thinkphp中使用PHPmailer发送邮件

在ThinkPHP\Extend\Vendor\目录下放入PHPMailer文件夹,里面包含以下文件

重置密码发送邮件

public function recover(){
            if($this->isPost()){
                 $mail=trim($_POST['mail']);
                 $pattern='1234567890abcdefghijklmnopqrstuvwxyz';
                 for($i=0;$i<35;$i++)
                    {
                        $key .= $pattern[mt_rand(0,35)];
                    }
                $flag = M('m_auth_users') -> where(array(mail=>$mail))->setField('code',md5($key));
                if($flag){
                    $url="http://***/index.php/Public/reset/code/".md5($key);
                    $body='您好:<br>请点击此链接 <a href='.$url.' >'.$url.'</a> 进行重置密码!';
                    Vendor('PHPMailer.PHPMailerAutoload');
                    sendmail('重置密码',$mail,$body);//发送邮件
                        $this->success('已向该邮箱发送重置密码邮件',U("Public/login")); 
                    }else{
                         $this->error('该邮箱不存在');
                    }
                }else{
                    $this->display();
                }      
    }

发送邮件函数

function sendmail($subject,$address,$body){

        $mail = new PHPMailer;
        $mail ->CharSet='utf-8';
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = '邮件服务器地址';  // Specify main and backup server
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '用户名';                            // SMTP username
        $mail->Password = '密码'; // SMTP password
        //$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
        $mail->From = '发件人';
        $mail->FromName = '显示名称';
        $mail->addAddress($address);  // Add a recipient
        $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject =$subject;
        //ip是否ping通邮件正文
        $mail->Body    = $body;
        $mail->send();
/*
 * if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}
    echo 'Message has been sent';*/
}

 

 

posted @ 2014-03-06 09:03  茗迹周  阅读(542)  评论(0编辑  收藏  举报