$code] * @param string $fromName 发送人姓名 * @param string $content 内容模板 * @return bool 成功/失败 */ public static function sendMail($subject, $to, $paramArray, $fromName, $content) { $cache = \Yii::$app->cache; $config = $cache->get('config'); if ($config == null) { $config = Config::findOne(['id' => self::DEF_CONFIG_ID]); $cache->set('config', $config, 86400); } // 本地和测试环境目前禁止发邮件 return self::sendSimpleMail($subject, $to, $config->smtp_from_mail, $content, $paramArray, $fromName); } /** * 发送短信 * @param $mobile * @return null|string */ public static function sendCodeSms($mobile) { $cache = \Yii::$app->cache; $lastCode = $cache->get($mobile.self::CACHE_CODE); if (!empty($lastCode)) { $time = $cache->get($mobile.self::CACHE_TIME); if ($time != null && (DateTimeHelper::microtime_float() - $time <= 120000)) { return null; } } $code = RandomHelper::getRandomNo(6); $cache->set($mobile.self::CACHE_CODE, $code); $cache->set($mobile.self::CACHE_TIME, DateTimeHelper::microtime_float()); return $code; // 本地和测试环境目前禁止发短信 /* $config = Config::findOne(['id' => self::DEF_CONFIG_ID]); $temp = self::sendBasicSms($mobile, $config->juhe_sms_tpl_id, $code); if ($temp) { $cache->set($mobile.self::CACHE_CODE, $code); $cache->set($mobile.self::CACHE_TIME, DateTimeHelper::microtime_float()); return $code; } else { return null; } */ } /** * 验证短信code * @param $mobile * @param $code * @return bool */ public static function validSmsCode($mobile, $code) { $cache = \Yii::$app->cache; $lastCode = $cache->get($mobile.self::CACHE_CODE); if (!empty($lastCode)) { if ($lastCode != $code) { return false; } else { return true; } } return false; } /** * 发送MT4sms/RegSms * @param $mobile * @param $tplId * @param $name * @param $login * @param $pwd * @return \yii\httpclient\Response */ public static function sendSms($mobile, $tplId, $name, $login, $pwd = '') { $map = new HashMapHelper(); $config = Config::findOne([['id' => self::DEF_CONFIG_ID]]); $map->put('mobile', $mobile); $map->put('tpl_id', $tplId.""); $tplValue = "#name#=".$name."&#login#=".$login; if ($pwd) { $tplValue .= "&#pwd#=".$pwd; } $a = urlencode("#"); $b = urlencode("&"); $c = urlencode("="); $tplValue = str_replace("#", $a, $tplValue); $tplValue = str_replace("&", $b, $tplValue); $tplValue = str_replace("=", $c, $tplValue); $map->put('tpl_value', $tplValue); $map->put('dtype', "json"); $map->put('key', $config->juhe_sms_key); return true; $url = "http://v.juhe.cn/sms/send"; $client = (new Client(['baseUrl' => $url])); $json = $client->get($url, self::buildParams($map->H_table))->send(); return $json; } /** * @param string $mobile * @return array */ public static function getCodeSms($mobile) { $result = [ 'code' => '', 'created' => 0, ]; $code = \Yii::$app->getCache()->get($mobile . self::CACHE_CODE); $created = \Yii::$app->getCache()->get($mobile . self::CACHE_TIME); if ($code && $created) { $result['code'] = $code; $result['created'] = $created; } return $result; } /** * @param $mobile * @param $tplId * @param $code * @return mixed */ private static function sendBasicSms($mobile, $tplId, $code) { $map = new HashMapHelper(); $config = Config::findOne([['id' => self::DEF_CONFIG_ID]]); $map->put('mobile', $mobile); $map->put('tpl_id', $tplId.""); $tplValue = "#code#=".$code; $map->put('tpl_value', urlencode($tplValue)); $map->put('dtype', "json"); $map->put('key', $config->juhe_sms_key); $url = "http://v.juhe.cn/sms/send"; $client = (new Client(['baseUrl' => $url])); $json = $client->get($url, self::buildParams($map->H_table))->send(); return $json; } /** * 生成token * @param array $param * @return mixed */ private static function buildParams($param) { ksort($param);//升序排序 $strOauth = ""; foreach ($param as $key => $val) { if ($key == 'token' || is_array($val) || $val === null) { continue; } $val = strval($val); $strOauth .= $key . '=' . $val . '&'; } $strOauth = rtrim($strOauth, '&'); $param['token'] = md5($strOauth); return $param; } /** * 发送简单邮件封装方法 * @param $subject * @param $to * @param $from * @param $content * @param $paramArray * @param $fromName * @return bool */ private static function sendSimpleMail($subject, $to, $from, $content, $paramArray, $fromName) { $isSuccess = false; $replace_content = self::replaceVariable($content, $paramArray); $config = Config::findOne(['id' => self::DEF_CONFIG_ID]); /* $smtp_username = $config->smtp_username; $smtp_password = $config->smtp_password; \Yii::$app->mail->apiUser = $smtp_username; \Yii::$app->mail->apiKey = $smtp_password; $result = \Yii::$app->mail->sendNormalMail($subject, ''.$replace_content, [$to], $from, $fromName); if ($result['result'] === true) { self::saveRecord($result['result'], $to, $subject, $replace_content); $isSuccess = true; } */ $mailid = self::saveRecord($isSuccess, $to, $subject, $replace_content); $mail = \Yii::$app->mailer->compose(); $mail->setTo($to); $mail->setSubject($subject); $mail->setHtmlBody( ''.$replace_content); $result = $mail->send(); if ($result) { $isSuccess = true; } else { Yii::warning('邮件发送失败' . VarDumper::dumpAsString($result), __METHOD__); } $status = ($isSuccess) ? 1 : 2; MailRecord::updateAll(['status' => $status], "id={$mailid}" ); return $isSuccess; } /** * 将模板中的变量替换为入参 * @param $content string * @param $paramArray array * @return string */ public static function replaceVariable($content, $paramArray) { if (!empty($content) && !empty($paramArray)) { foreach ($paramArray as $key => $value) { $variable='${'.$key.'}'; $variable1 = '#{'.$key.'}'; $variable2 = '#'.$key.'#'; if (is_null($value)) { continue; } while (strpos($content, $variable, 0) > -1) { $content = str_replace($variable, $value, $content); } while (strpos($content, $variable1, 0) > -1) { $content = str_replace($variable1, $value, $content); } while (strpos($content, $variable2, 0) > -1) { $content = str_replace($variable2, $value, $content); } } } return $content; } /** * 保存发送的邮件记录到crm_mail_record表 * @param $status * @param $receiver * @param $subject * @param $content * @return bool */ private static function saveRecord($status, $receiver, $subject, $content) { $mailRecord = new MailRecord(); $mailRecord->content = $content; $mailRecord->receiver = $receiver; if ($status) { $mailRecord->status = 1; } else { $mailRecord->status = 0; } $mailRecord->subject = $subject; $mailRecord->in_time = DateTimeHelper::microtime_float(); $mailRecord->save(); return $mailRecord->id; } /** * 发送email邮件 * @param $subject * @param $to * @param $from * @param $body * @param $mid * @return bool */ public static function sendButNotSaveRecord($subject, $to, $from, $body, $mid) { $isSuccess = false; $config = Config::findOne(['id' => self::DEF_CONFIG_ID]); /* $smtp_username = $config->smtp_username; $smtp_password = $config->smtp_password; \Yii::$app->mail->apiUser = $smtp_username; \Yii::$app->mail->apiKey = $smtp_password; $result = \Yii::$app->mail->sendNormalMail($subject, ''.$body, $to, $from); if ($result['result'] === true) { $isSuccess = true; } return $isSuccess; */ $mail = \Yii::$app->mailer->compose(); $mail->setTo($to); $mail->setSubject($subject); $mail->setHtmlBody( ''.$body); if ($mail->send()) { MailRecord::updateAll(['status' => 1], "id={$mid}" ); return true; } return false; } }