首先在公共函数库写一个发送邮件的函数:
function sendMail($to, $title, $content) {
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP(); // 启用SMTP
$mail->Host=C('MAIL_HOST'); //smtp服务器的名称(这里以QQ邮箱为例)
$mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证
$mail->Username = C('MAIL_USERNAME'); //发件人邮箱名
$mail->Password = C('MAIL_PASSWORD') ; //163邮箱发件人授权密码
$mail->From = C('MAIL_FROM'); //发件人地址(也就是你的邮箱地址)
$mail->FromName = C('MAIL_FROMNAME'); //发件人姓名
$mail->AddAddress($to,"尊敬的客户");
$mail->WordWrap = 50; //设置每行字符长度
$mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件
$mail->CharSet=C('MAIL_CHARSET'); //设置邮件编码
$mail->Subject =$title; //邮件主题
$mail->Body = $content; //邮件内容
$mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
return($mail->Send());
}
发送验证码:
public function getEmailSign(){
if(IS_AJAX){
//接收账号信息
$data['name'] = I('get.name');
if(!$data['name']) $this->ajaxReturn("用户名不能为空",0);
//接收邮箱
$data['email'] = I('get.email');
if(!$data['email']) $this->ajaxReturn("请填写邮箱账号",0);
//验证邮箱格式
$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
if(preg_match( $pattern, $data['email'])){
//邮箱格式正确下,判断用户是否存在
$where['user_name'] = I('get.name');
$user = M('user')->where($where)->find();
if($user){
//核对用户账号与邮箱是否一致
if($user['email'] == $data['email']){
//设置参数
$title = "邮箱验证"; //邮件标题
$font = "此邮件为修改密码验证邮件,请勿回复,您的验证码为"; //邮件内容
$code = rand(1000,9999); //随机码
$content = $font.$code; //拼接
if(SendMail($data['email'],$title,$content)) {
//存验证码
session('email_code',$code);
session('email_date',time());
$this->ajaxReturn("发送成功,请在5分钟之内验证",0);
}else{
$this->ajaxReturn("发送失败",0);
}
}else{
$this->ajaxReturn("请核对用户信息的正确性",0);
}
}else{
$this->ajaxReturn("请核对用户信息",0);
}
}else{
$this->ajaxReturn("请核对邮箱信息",0);
}
}else{
$this->ajaxReturn(0);
}
}
发送邮件(和上面一个意思):
public function bindEmail(){
if(IS_AJAX){
//接收账号信息
$user_id = session('user_id');
$name = I('get.name');
$email = I('get.email');
$data['name'] = $name;
if(!$data['name']) $this->ajaxReturn("用户名不能为空",0);
//接收邮箱
$data['email'] = $email;
if(!$data['email']) $this->ajaxReturn("请填写邮箱账号",0);
//验证邮箱格式
$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
if(preg_match( $pattern, $data['email'])){
//邮箱格式正确下,判断用户是否存在
$where['user_name'] = I('get.name');
$user = M('user')->where($where)->find();
if($user){
//核对用户账号与邮箱是否一致
if($user['email'] == $data['email']){
//设置参数
$title = "绑定邮箱"; //邮件标题
$date = time();
$key = 'yushi';
$info = substr(MD5($key),0,6);
$message=<<<str
你好!{$name}
<h2>欢迎注册本网站</h2>
绑定邮箱请点击如下地址:<br/><br/>
<a href="http://127.0.0.1/hyzx/index.php/Home/Login/bindEmail/date/{$date}/info/{$info}/i/{$user_id}/email/{$email}">
http://127.0.0.1/hyzx/index.php/Home/Login/bindEmail/date/{$date}/info/{$info}/i/{$user_id}/email/{$email}</a>
<br/><br/>
str;
if(SendMail($data['email'],$title,$message)) {
$this->ajaxReturn("发送成功,请去您的邮箱查看",0);
}else{
$this->ajaxReturn("发送失败",0);
}
}else{
$this->ajaxReturn("请核对用户信息的正确性",0);
}
}else{
$this->ajaxReturn("请核对用户信息",0);
}
}else{
$this->ajaxReturn("请核对邮箱信息",0);
}
}else{
$user_id = session('user_id');
$data['user_id'] = $user_id;
$user = M('user')->where($data)->find();
$this->assign('user',$user);
$this->display();
}
}