结合TP框架实现的发送邮件功能
在这里我们采用的是phpmail类文件
具体使用代码如下
1.引入phpmail类文件
include("./ThinkPHP/Library/Vendor/phpmailer/class.phpmailer.php"); include("./ThinkPHP/Library/Vendor/phpmailer/class.smtp.php");
2.
/* * 邮箱回复给用户 * */ public function EmailToUser() { $id = $_POST['report_id']; $touser = $_POST['email']; $title = $_POST['title']; $content = $_POST['content']; $mail = new \PHPMailer(true); $mail->IsSMTP(); $mail->CharSet='UTF-8'; //设置邮件的字符编码,这很重要,不然中文乱码 $mail->SMTPAuth = true; //开启认证 $mail->Port = 25; $mail->Host = "smtp.163.com"; $mail->Username = "peitanet@163.com"; $mail->Password = "txhl2016"; //$mail->IsSendmail(); //如果没有sendmail组件就注释掉,否则出现“Could not execute: /var/qmail/bin/sendmail ”的错误提示 $mail->AddReplyTo("peitanet@163.com");//回复地址 $mail->From = "peitanet@163.com"; $to = $touser; $mail->AddAddress($to); $mail->Subject = $title; $mail->Body = "<h5>".$content."</h5>"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示,可以省略 $mail->WordWrap = 80; // 设置每行字符串的长度 //$mail->AddAttachment("f:/test.png"); //可以添加附件 $mail->IsHTML(true); if($mail->Send()){ $report = M('report'); $data['status'] = 1; $res = $report->where('id='.$id)->save($data); if($res){ echo json_encode(['code'=>200,'msg'=>'邮件已发送!']); }else{ echo json_encode(['code'=>201,'msg'=>'您的网络中途掉线!']); } }else{ echo json_encode(['code'=>500,'msg'=>'网络开小差了,请您重新提交请求!']); } }
注意在这里邮箱要开启smtp功能,其中邮箱密码是另外设置的虚拟密码,并不是实际的邮箱登录密码!
通往牛逼的路上,在意的只有远方!