1.下载phpmailer文件库
2.引用文件,此处将代码放到 :函数库中,function.php
function sendConsultantMessage($sendData){ Vendor('PHPMailer.class#phpmailer'); Vendor('PHPMailer.class#smtp'); $mail = new PHPMailer(); //实例化 // var_dump($mail); // exit; $mail->IsSMTP(); // 启用SMTP $mail->Host = "smtp.exmail.qq.com"; //SMTP服务器 以163邮箱为例子 $mail->Port = 25; //邮件发送端口 $mail->SMTPAuth = true; //启用SMTP认证 $mail->CharSet = "UTF-8"; //字符集 $mail->Encoding = "base64"; //编码方式 $mail->Username = "smtp@test.com"; //你的邮箱 $mail->Password = "test123456"; //你的密码 $mail->Subject = $sendData['send_subject']; //邮件标题 $mail->From = "smtp@test.com"; //发件人地址(也就是你的邮箱) $mail->FromName = "smtp@test.com"; //发件人姓名 // $address = "";//收件人email $address = $sendData['send_receiver']; $mail->AddAddress($address, "");//添加收件人(地址,昵称) if($sendData['send_copy'] && $sendData['send_name']){ $path = $sendData['send_copy']; $file = $sendData['send_name']; $mail->AddAttachment($path,$file); // 添加附件,并指定名称 } $mail->IsHTML(true); //支持html格式内容 // $mail->AddEmbeddedImage("./Application/Public/logo.png", "my-attach", "logo.png"); //设置邮件中的图片 // $mail->Body = '你好, <b>朋友</b>! <br/>这是一封来自<a href="http://www.helloweba.com" target="_blank">helloweba.com</a>的邮件!<br/><img alt="helloweba" src="cid:my-attach">'; //邮件主体内容 $mail->Body = $sendData['send_contents']; if(!$mail->Send()){ return false; }else{ return true; } }
3.核心方法
public function sendInfo(){ header("Content-type: text/html; charset=utf-8"); date_default_timezone_set('PRC'); //设置中国时区 $consultantModel = M('consultant_entry'); $sendData['send_id'] = I('post.send_id'); $sendData['send_receiver'] = I('post.send_receiver'); $sendData['send_subject'] = I('post.send_subject'); $sendData['send_contents'] = I('post.send_contents'); $sendData['send_copy'] = ''; $sendData['send_name'] = ''; // $id = I('get.id',0,'intval'); //1111111111111111111111111111111111111111111111111 if($_FILES['send_copy']['name'] && $_FILES['send_copy']['error']==0){ $type = pathinfo($_FILES['send_copy']['name']); if(in_array(strtolower($type['extension']), array('jpg','jpeg','gif','png','bmp','zip','rar','pdf','txt','html','xml','doc','docx','xls','xlsx','ppt','ppts')) && $_FILES['send_copy']['size'] < 3145728000){ // 2016-1-12 添加开始 $upload = new ThinkUpload();// 实例化上传类 $upload->maxSize = 3145728000 ;// 设置附件上传大小 $upload->exts = array('jpg','jpeg','gif','png','bmp','zip','rar','pdf','txt','html','xml','doc','docx','xls','xlsx','ppt','ppts');// 设置附件上传类型 $upload->rootPath = './Application/Public/upload/'; // 设置附件上传根目录 $upload->savePath = 'email/'; $info = $upload->uploadOne($_FILES['send_copy']); if(!$info){ $this->error($upload->getError()); exit; }else{ $sendData['send_copy'] = $upload->rootPath.$info['savepath'].$info['savename']; $sendData['send_name'] = $_FILES['send_copy']['name']; } }else{ $this->error("上传文件各式不正确或者文件过大!"); } } //2222222222222222222222222222222222222222222222222 $resInfo = sendConsultantMessage($sendData); if(!$resInfo){ unlink($sendData['send_copy']); $this->error("邮件发送失败!",U("Consultant/getInfo")); exit; }else{ unlink($sendData['send_copy']); $_where['id'] = $sendData['send_id']; $_where['sale_id'] = $this->admin['id']; $data = array( 'state'=>3, ); $saveInfo = $consultantModel->where($_where)->save($data); $this->success("邮件发送成功!",U("Consultant/getInfo")); exit; } }
4.注意:的引用。
5.几点注意事项
a.此处发送邮箱的账号,密码要正确,同事确保已经开启了smtp服务。
b.发送附件,以及图片时,附件(图片)的路径要准确。(要定义为根目录下的相对路径,或者绝对路径)
项目中出现的问题:
e.网上的解决方案:
f.我的解决
g.其他问题以及解决方案
http://www.chinastor.com/a/jishu/mailserver/0G392262014.html