Sendemail.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Email as EmailModel;
  4. use addons\qingdongams\model\StaffSignIn;
  5. use addons\qingdongams\model\Contacts;
  6. use addons\qingdongams\model\File;
  7. use PHPMailer\PHPMailer\PHPMailer;
  8. use think\Db;
  9. use think\Log;
  10. /**
  11. * 发送邮件
  12. */
  13. class Sendemail extends StaffApi {
  14. protected $noNeedLogin = [];
  15. protected $noNeedRight = [];
  16. protected $formater = ['gif','jpeg','png','jpg','bmp'];
  17. // 草稿箱
  18. public function draftsEmail(){
  19. $ids = input('ids');
  20. $content = input('content','');// 内容
  21. $theme = input('theme','');// 主题
  22. $receiver = input('receiver','');// 收件人 ,隔开
  23. $copyor = input('copyor','');// 收件人 ,隔开
  24. $receiveIds = [];
  25. $model = new EmailModel();
  26. if($receiver){
  27. $receiveIds = Contacts::where('email','in',explode(',',$receiver))->column('id');
  28. }
  29. $save = [
  30. 'send_id'=>$this->auth->id,
  31. 'send_name'=>$this->auth->name,
  32. 'receiver'=>$receiver,
  33. 'receive_id'=>implode(',',$receiveIds),
  34. 'copyor'=>$copyor,
  35. 'theme'=>$theme,
  36. 'content'=>$content,
  37. 'is_send'=>0,
  38. 'file_ids'=>$ids,
  39. ];
  40. if($model->save($save) === false){
  41. Log::write('存库失败数据:'.$model->getLastSql());
  42. }
  43. $this->success('存库成功');
  44. }
  45. // 草稿箱列表
  46. public function draftsEmails(){
  47. $limit = input("limit/d", 10);
  48. $where['send_id'] = $this->auth->id;
  49. $where['is_send'] = 0;
  50. $email = new EmailModel();
  51. $list = $email->where($where)->field('createtime,updatetime,imgs,attach',true)->paginate($limit);
  52. $this->success('',$list);
  53. }
  54. // 发送邮箱
  55. public function sendEmail(){
  56. $emailId = input('id/d',0); // 邮件id
  57. $fileIds = input('ids');// 附件id
  58. $content = input('content','');// 内容
  59. $theme = input('theme','');// 主题
  60. $receiver = input('receiver');// 收件人 ,隔开
  61. $copyor = input('copyor','');// 收件人 ,隔开
  62. $params = $this->request->post();
  63. Log::write('发送参数'.json_encode($params));
  64. if(!isset($theme) || !$theme){
  65. $this->error('请输入邮箱发送主题');
  66. }
  67. if(!isset($content) || !$content){
  68. $content = $theme;
  69. }
  70. if(!isset($receiver) || !$receiver){
  71. $this->error('请输入接收人');
  72. }
  73. $imgs = $fujian = [];
  74. if($fileIds){
  75. $ids = explode(',',$fileIds);
  76. $filemodel = new File();
  77. $list = $filemodel->where('id','in',$ids)->column('file_path');
  78. foreach ($list as $k=>$v){
  79. $geshi = substr(strrchr($v, '.'), 1);
  80. $file = ROOT_PATH.'public'.$v;
  81. if(in_array($geshi,$this->formater)){
  82. $imgs[] = $file;
  83. }else{
  84. $fujian[] = $file;
  85. }
  86. }
  87. }
  88. Log::write('开始发送准备参数==='.$receiver.'---'.$theme.'===='.$content.'----'.$copyor);
  89. $res = $this->send_email(explode(',',$receiver),$theme,$content,$imgs,$fujian,explode(',',$copyor));
  90. Log::write('发送完毕结果==='.json_encode($res));
  91. if($res['error'] == 0){
  92. $receiveIds = $customerIds ='';
  93. $linkList = Contacts::where('email','in',explode(',',$receiver))->field('id,customer_id')->select();
  94. if(count($linkList)>0){
  95. $receiveIds = implode(',',array_column($linkList,'id'));
  96. $customerIds = implode(',',array_column($linkList,'customer_id'));
  97. }
  98. $model = new EmailModel();
  99. $save = [
  100. 'send_id'=>$this->auth->id,
  101. 'send_name'=>$this->auth->name,
  102. 'receiver'=>$receiver,
  103. 'receive_id'=>$receiveIds,
  104. 'copyor'=>$copyor,
  105. 'theme'=>$theme,
  106. 'content'=>$content,
  107. 'is_send'=>1,
  108. 'imgs'=>implode(',',$imgs),
  109. 'attach'=>implode(',',$fujian),
  110. 'file_ids'=>$fileIds,
  111. 'sendtime'=>date("Y-m-d H:i:s"),
  112. ];
  113. $data = [
  114. 'staff_id' => $this->auth->id,
  115. 'location' => '',
  116. 'lng' => '',
  117. 'lat' => '',
  118. 'files' => $fileIds,
  119. 'customer_id' => $customerIds,//客户id
  120. 'relation_type' => StaffSignIn::CUSTOMER_TYPE,//日程类型 客户
  121. 'relation_id' => $customerIds,//签到关联发送邮件id
  122. 'relation_process' => '邮箱',//进度
  123. 'content' => $theme,
  124. 'distance' => '',
  125. ];
  126. $res = $result = true;
  127. Db::startTrans();
  128. if($emailId){
  129. if(!$res = $model->isUpdate(true)->save($save,['id'=>$emailId])){
  130. Log::write('发送成功,存库失败数据:'.json_encode($save).'---sql语句:'.$model->getLastSql());
  131. }
  132. }else{
  133. if(!$res = $model->save($save)){
  134. Log::write('发送成功,存库失败数据:'.json_encode($save).'---sql语句:'.$model->getLastSql());
  135. }
  136. }
  137. if(!$result = StaffSignIn::createSignIn($data)){
  138. Log::write('发送成功,跟进记录失败数据:'.json_encode($save).'---sql语句:'.StaffSignIn::getLastSql());
  139. }
  140. if($res === false || $result === false){
  141. Db::rollback();
  142. $this->error('邮件发送成功,本地记录失败!');
  143. }else{
  144. Db::commit();
  145. $this->success('发送成功');
  146. }
  147. }else{
  148. $this->error('企业邮箱配置不正确');
  149. }
  150. }
  151. // 已发送列表
  152. public function getSendEmails()
  153. {
  154. $limit = input("limit/d", 10);
  155. $where['send_id'] = $this->auth->id;
  156. $where['is_send'] = 1;
  157. $email = new EmailModel();
  158. $list = $email->where($where)->field('createtime,updatetime,imgs,attach',true)->paginate($limit);
  159. $this->success('',$list);
  160. }
  161. /**
  162. * 发送邮件
  163. * @param string $address 需要发送的邮箱地址 发送给多个地址需要写成数组形式
  164. * @param string $subject 标题
  165. * @param string $content 内容
  166. * @param string $imgs 图片 发送给多个地址需要写成数组形式
  167. * @param string $attach 附件 发送给多个地址需要写成数组形式
  168. * @return boolean 是否成功
  169. */
  170. function send_email($address,$subject,$content,$imgs='',$attach='',$copyor = []){
  171. $email_from_name = $this->auth->name;// 发件人
  172. // $email_smtp='smtp.163.com';// smtp 网易一般邮箱服务器地址
  173. $email_smtp='smtphz.qiye.163.com';// smtp 网易企业邮箱服务器地址
  174. $email_username=$this->auth->email;// 账号
  175. $email_password=$this->auth->email_code; // 密码 注意: 163和QQ邮箱是授权码;不是登录的密码
  176. $email_smtp_secure='ssl';// 链接方式 如果使用QQ邮箱;需要把此项改为 ssl
  177. $email_port= 465;// 端口 王一邮箱:25; 如果使用QQ邮箱;需要把此项改为 465
  178. if(empty($email_smtp) || empty($email_username) || empty($email_password) || empty($email_from_name)){
  179. return array("error"=>1,"message"=>'邮箱配置不完整');
  180. }
  181. $phpmailer=new PHPMailer();
  182. // 设置PHPMailer使用SMTP服务器发送Email
  183. $phpmailer->IsSMTP();
  184. // 设置设置smtp_secure
  185. $phpmailer->SMTPSecure=$email_smtp_secure;
  186. // 设置port
  187. $phpmailer->Port=$email_port;
  188. // 设置为html格式
  189. $phpmailer->IsHTML(true);
  190. // 设置邮件的字符编码'
  191. $phpmailer->CharSet='UTF-8';
  192. // 设置SMTP服务器。
  193. $phpmailer->Host=$email_smtp;
  194. // 设置为"需要验证"
  195. $phpmailer->SMTPAuth=true;
  196. // 设置用户名
  197. $phpmailer->Username=$email_username;
  198. // 设置密码
  199. $phpmailer->Password=$email_password;
  200. // 设置邮件头的From字段。
  201. $phpmailer->From=$email_username;
  202. // 设置发件人名字
  203. $phpmailer->FromName=$email_from_name;
  204. // 添加收件人地址,可以多次使用来添加多个收件人
  205. foreach($address as $add){
  206. $phpmailer->AddAddress(htmlspecialchars($add));
  207. }
  208. // 抄送人
  209. foreach($copyor as $copy){
  210. $phpmailer->addCC(htmlspecialchars($copy));
  211. }
  212. // 发送图片
  213. if(is_array($imgs)){
  214. foreach($imgs as $k=>$img){
  215. $phpmailer->addEmbeddedImage($img,'img_'.$k);
  216. }
  217. }else{
  218. $phpmailer->addEmbeddedImage($imgs,'img');
  219. }
  220. // 发送附件
  221. if(is_array($attach)){
  222. foreach($attach as $att){
  223. $phpmailer->addAttachment($att);
  224. }
  225. }else{
  226. $phpmailer->addAttachment($attach);
  227. }
  228. // 设置邮件标题
  229. $phpmailer->Subject=$subject;
  230. // 设置邮件正文
  231. $phpmailer->Body=$content;
  232. // 发送邮件。
  233. if(!$phpmailer->Send()) {
  234. $phpmailererror=$phpmailer->ErrorInfo;
  235. return array("error"=>1,"message"=>$phpmailererror);
  236. }else{
  237. return array("error"=>0);
  238. }
  239. }
  240. }