ci中使用mail类
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Index extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('index');
}
// 注册
public function receive(){
$act=isset($_GET['act']) ? $_GET['act'] : '';
$pdo=new PDO("mysql:host=127.0.0.1;dbname=news","root","root");
$pdo->exec("set names utf8");
if ($act=="zhuce") {
// 用户注册
$name=$this->input->post("name");
$pwd=md5($this->input->post("pwd"));
$em=$this->input->post("email");
$key=md5("hello world");
$start=0;
$time=time();
// 添加用户信息
$sql="insert into user value(null,'$name','$pwd','$em','$start','$key')";
if ($pdo->exec($sql)) {
// 添加成功发送激活邮箱
$url="http://localhost/day9.22/index.php/index/receive?act=jihuo&name=".base64_encode($name)."&pwd=$pwd&key=$key&start=$start&time=$time";
$title="用户注册激活";
$content="请点击下面链接完成注册:<a href='$url'>$url</a>";
$user="来自";
// $address=$email;
// 引入发送邮箱类
$this->load->library('email');
// 配置邮件参数
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.163.com'; //使用邮箱地址
$config['smtp_user'] = '15175881112@163.com'; //发送人邮箱
$config['smtp_pass'] = '1593578qqQQ'; //发送人邮箱pop3密码
$config['smtp_port'] = '25'; //使用端口号
$config['mailtype'] = 'html'; //发送邮箱类型
$config['charset'] = 'utf-8';
// $config['wordwrap'] = TRUE;
$this->email->initialize($config);
$this->email->from('15175881112@163.com', $user); //发送人邮箱
$this->email->to($em);
// $this->email->cc('another@another-example.com'); //抄送
// $this->email->bcc('them@their-example.com'); //密送
$this->email->subject($title); //邮件标题
$this->email->message($content); //邮件内容
if($this->email->send()){
echo "发送成功,请验证";
header("mail.qq.com");
}
else{
echo $this->email->print_debugger();
}
}
}elseif ($act=="jihuo") {
// 用户验证邮箱
$name=base64_decode($this->input->get('name'));
$pwd=$this->input->get('pwd');
$key=$this->input->get('key');
$time=$this->input->get('time');
$sql="select * from user where name='$name'";
$res=$pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
if ($res['key']!=$key) {
echo "请不要恶意注册";
die;
}
// if (time()-$time>1800) {
// die("激活已经失效,请重新注册");
// }
if ($res['start']==1) {
echo "已经激活过,请登陆";
header("Location:receive?act=denglu");
die;
// redirect('act=denglu');
}
$sql2="update user set start=1 where name='$name'";
if ($pdo->exec($sql2)) {
echo "激活成功,请登陆";
header("receive?act=denglu");
die;
}else{
echo "激活失败,请重新注册";
header("refresh:3;receive");
die;
}
}elseif ($act=="denglu") {
// 用户登陆提供登陆表单
echo "denglu";
}else{
// 注册页面
$this->load->view('index');
}
}
}
defined('BASEPATH') OR exit('No direct script access allowed');
class Index extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('index');
}
// 注册
public function receive(){
$act=isset($_GET['act']) ? $_GET['act'] : '';
$pdo=new PDO("mysql:host=127.0.0.1;dbname=news","root","root");
$pdo->exec("set names utf8");
if ($act=="zhuce") {
// 用户注册
$name=$this->input->post("name");
$pwd=md5($this->input->post("pwd"));
$em=$this->input->post("email");
$key=md5("hello world");
$start=0;
$time=time();
// 添加用户信息
$sql="insert into user value(null,'$name','$pwd','$em','$start','$key')";
if ($pdo->exec($sql)) {
// 添加成功发送激活邮箱
$url="http://localhost/day9.22/index.php/index/receive?act=jihuo&name=".base64_encode($name)."&pwd=$pwd&key=$key&start=$start&time=$time";
$title="用户注册激活";
$content="请点击下面链接完成注册:<a href='$url'>$url</a>";
$user="来自";
// $address=$email;
// 引入发送邮箱类
$this->load->library('email');
// 配置邮件参数
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.163.com'; //使用邮箱地址
$config['smtp_user'] = '15175881112@163.com'; //发送人邮箱
$config['smtp_pass'] = '1593578qqQQ'; //发送人邮箱pop3密码
$config['smtp_port'] = '25'; //使用端口号
$config['mailtype'] = 'html'; //发送邮箱类型
$config['charset'] = 'utf-8';
// $config['wordwrap'] = TRUE;
$this->email->initialize($config);
$this->email->from('15175881112@163.com', $user); //发送人邮箱
$this->email->to($em);
// $this->email->cc('another@another-example.com'); //抄送
// $this->email->bcc('them@their-example.com'); //密送
$this->email->subject($title); //邮件标题
$this->email->message($content); //邮件内容
if($this->email->send()){
echo "发送成功,请验证";
header("mail.qq.com");
}
else{
echo $this->email->print_debugger();
}
}
}elseif ($act=="jihuo") {
// 用户验证邮箱
$name=base64_decode($this->input->get('name'));
$pwd=$this->input->get('pwd');
$key=$this->input->get('key');
$time=$this->input->get('time');
$sql="select * from user where name='$name'";
$res=$pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
if ($res['key']!=$key) {
echo "请不要恶意注册";
die;
}
// if (time()-$time>1800) {
// die("激活已经失效,请重新注册");
// }
if ($res['start']==1) {
echo "已经激活过,请登陆";
header("Location:receive?act=denglu");
die;
// redirect('act=denglu');
}
$sql2="update user set start=1 where name='$name'";
if ($pdo->exec($sql2)) {
echo "激活成功,请登陆";
header("receive?act=denglu");
die;
}else{
echo "激活失败,请重新注册";
header("refresh:3;receive");
die;
}
}elseif ($act=="denglu") {
// 用户登陆提供登陆表单
echo "denglu";
}else{
// 注册页面
$this->load->view('index');
}
}
}