123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
-
-
- namespace app\common\logic;
-
- use think\Db;
-
-
- class EmailLogic
- {
- private $config;
- private $home_lang;
-
- public function __construct($smtp_config = [])
- {
- $this->config = !empty($smtp_config) ? $smtp_config : tpCache('smtp');
- $this->home_lang = get_home_lang();
- }
-
-
-
- private function replaceContent($scene = '', $params = '')
- {
- if (0 == intval($scene)) {
- $msg = $params;
- } else {
- $params_arr = [];
- $emailTemp = Db::name('smtp_tpl')->where([
- 'send_scene'=> $scene,
- 'lang' => $this->home_lang,
- ])->find();
- if (!empty($emailTemp)) {
- $msg = $emailTemp['tpl_content'];
- preg_match_all('/\${([^\}]+)}/i', $msg, $matchs);
- if (!empty($matchs[1])) {
- foreach ($matchs[1] as $key => $val) {
- if (is_array($params)) {
- $params_arr[$val] = $params[$val];
- } else {
- $params_arr[$val] = $params;
- }
- }
- }
-
-
- foreach ($params_arr as $k => $v) {
- $msg = str_replace('${' . $k . '}', $v, $msg);
- }
- } else {
- return '';
- }
- }
-
- return $msg;
- }
-
-
-
- public function send_email($to='', $subject='', $data='', $scene=1, $library = 'phpmailer'){
- if (0 < intval($scene)) {
- $smtp_tpl_row = Db::name('smtp_tpl')->where([
- 'send_scene'=> $scene,
- 'lang' => $this->home_lang,
- ])->find();
- if (empty($smtp_tpl_row)) {
- return ['code'=>0,'msg'=>'找不到相关邮件模板!'];
- } else if (empty($smtp_tpl_row['is_open'])) {
- return ['code'=>0,'msg'=>'该功能待开放,请先启用邮件模板('.$smtp_tpl_row['tpl_name'].')'];
- } else {
- empty($subject) && $subject = $smtp_tpl_row['tpl_title'];
- }
- }
-
- switch ($library) {
- case 'phpmailer':
- return $this->send_phpmailer($to, $subject, $data, $scene);
- break;
-
- case 'swiftmailer':
- return $this->send_swiftmailer($to, $subject, $data, $scene);
- break;
-
- default:
- return $this->send_phpmailer($to, $subject, $data, $scene);
- break;
- }
- }
-
-
-
- private function send_swiftmailer($to='', $subject='', $data='', $scene=1){
- vendor('swiftmailer.lib.swift_required');
-
- try {
-
- $openssl_funcs = get_extension_funcs('openssl');
- if(!$openssl_funcs){
- return array('code'=>0 , 'msg'=>'请先开启php的openssl扩展');
- }
-
-
-
-
-
-
-
- empty($to) && $to = $this->config['smtp_from_eamil'];
- $to = explode(',', $to);
-
-
- $host = $this->config['smtp_server'];
-
- $port = intval($this->config['smtp_port']);
-
- $user = $this->config['smtp_user'];
-
- $pwd = $this->config['smtp_pwd'];
-
- $from = $this->config['smtp_user'];
-
- $from_name = $user;
-
- $encryption_type = null;
- switch ($port) {
- case 465:
- $encryption_type = 'ssl';
- break;
-
- case 587:
- $encryption_type = 'tls';
- break;
-
- default:
-
- break;
- }
-
- $body = $this->replaceContent($scene, $data);
-
-
- $transport = (new \Swift_SmtpTransport($host, $port, $encryption_type))
- ->setUsername($user)
- ->setPassword($pwd);
-
-
- $mailer = new \Swift_Mailer($transport);
-
-
- $message = (new \Swift_Message($subject))
- ->setFrom([$from=>$from_name])
-
- ->setTo($to)
- ->setContentType("text/html")
- ->setBody($body);
-
-
- $result = $mailer->send($message);
- if (!$result) {
- return array('code'=>0 , 'msg'=>'发送失败');
- } else {
- return array('code'=>1 , 'msg'=>'发送成功');
- }
- } catch (\Exception $e) {
- return array('code'=>0 , 'msg'=>'发送失败: '.$e->errorMessage());
- }
- }
-
-
-
- private function send_phpmailer($to='', $subject='', $data='', $scene=1){
- vendor('phpmailer.PHPMailerAutoload');
- try {
-
- $openssl_funcs = get_extension_funcs('openssl');
- if(!$openssl_funcs){
- return array('code'=>0 , 'msg'=>'请先开启php的openssl扩展');
- }
-
-
-
-
-
-
-
- $mail = new \PHPMailer;
- $mail->CharSet = 'UTF-8';
- $mail->isSMTP();
-
-
-
-
- $mail->SMTPDebug = 0;
-
-
-
- empty($to) && $to = $this->config['smtp_from_eamil'];
- $to = explode(',', $to);
-
- $mail->Host = $this->config['smtp_server'];
-
- $mail->Port = intval($this->config['smtp_port']);
-
- switch ($mail->Port) {
- case 465:
- $mail->SMTPSecure = 'ssl';
- break;
-
- case 587:
- $mail->SMTPSecure = 'tls';
- break;
-
- default:
-
- break;
- }
-
- $mail->SMTPAuth = true;
-
- $mail->Username = $this->config['smtp_user'];
-
- $mail->Password = $this->config['smtp_pwd'];
-
- $mail->setFrom($this->config['smtp_user']);
-
-
-
- if(is_array($to)){
- foreach ($to as $v){
- $mail->addAddress($v);
- }
- }else{
- $mail->addAddress($to);
- }
-
- $mail->isHTML(true);
-
- $mail->Subject = $subject;
-
- $content = $this->replaceContent($scene, $data);
- $mail->msgHTML($content);
-
-
-
-
-
- $result = $mail->send();
- if (!$result) {
- $msg = $mail->ErrorInfo;
- if (stristr($msg, 'smtp connect() failed')) {
- if (465 == $mail->Port) {
- $msg = '<br/>1.请检查配置是否正确<br/>2.空间是否开启函数fsockopen<br/>3.尝试切换PHP不同版本重试';
- } else {
- $msg = '请检查SMTP端口填写是否正确,一般默认是465端口,其次25端口,具体请参看各STMP服务商的设置说明。';
- }
- }
- return array('code'=>0 , 'msg'=>'发送失败:'.$msg);
- } else {
- return array('code'=>1 , 'msg'=>'发送成功');
- }
- } catch (\Exception $e) {
- return array('code'=>0 , 'msg'=>'发送失败: '.$e->errorMessage());
- }
- }
- }
|