$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]); $mailConfig = new MailConfig(); $configArr = $mailConfig->getMailConfig($to); if(!$configArr){ $connection=Yii::$app->db; $time = time() - 300; //将5分钟内发送给相同收件人和相同主题的发件记录设置为不可发送状态 $sql = "update crm_mail_record set status = 2 where subject = '$subject' and receiver = '$to' and in_time > $time"; $command=$connection->createCommand($sql); $rowCount=$command->execute(); }else{ Yii::$app->set('mailer', [ 'class' => 'yii\swiftmailer\Mailer', 'useFileTransport' => false, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => $configArr['host'], 'username' => $configArr['smtp_username'], 'password' => $configArr['password'], 'port' => $configArr['port'], 'encryption' => 'ssl', ], 'messageConfig' => [ 'charset' => 'UTF-8', 'from' => [$configArr['send_mail'] => 'Siyade'] ], ]); } $mailid = self::saveRecord($isSuccess, $to, $subject, $replace_content,$configArr['id']); //如果是订单信息就开始设置为paycenter的邮箱地址发送 if($subject=="訂單信息"){ $pay_mail_num = intval($config->pay_mail_num); if($pay_mail_num == 0){ $smtp_username = "admin@fastpaypro.net"; $host = 'hwsmtppay.exmail.qq.com'; $port = '4650'; // 设置新的邮箱地址发送 Yii::$app->set('mailer', [ 'class' => 'yii\swiftmailer\Mailer', 'useFileTransport' => false, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => $host, 'username' => $smtp_username, 'password' => 'Fastpak99', 'port' => $port, 'encryption' => 'ssl', ], 'messageConfig' => [ 'charset' => 'UTF-8', 'from' => [$smtp_username => 'fastpaypro'] ], ]); }else{ $pay_mail = $config->pay_mail; $smtp_username = "apikey"; $host = 'smtp.sendgrid.net'; $port = '465'; // 设置新的邮箱地址发送 Yii::$app->set('mailer', [ 'class' => 'yii\swiftmailer\Mailer', 'useFileTransport' => false, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => $host, 'username' => $smtp_username, 'password' => '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', 'port' => $port, 'encryption' => 'ssl', ], 'messageConfig' => [ 'charset' => 'UTF-8', 'from' => [$pay_mail => $pay_mail] ], ]); } } $mail = \Yii::$app->mailer->compose(); $mail->setTo($to); $mail->setSubject($subject); $mail->setHtmlBody( ''.$replace_content); $result = $mail->send(); if ($result) { $isSuccess = true; } else { self::sendSimpleMail($subject, $to, $from, $content, $paramArray, $fromName); 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,$num) { $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->mail_num = $num; $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; */ $mailConfig = new MailConfig(); $configArr = $mailConfig->getMailConfig($to); if(!$configArr){ $connection=Yii::$app->db; $time = time() - 300; //将5分钟内发送给相同收件人和相同主题的发件记录设置为不可发送状态 $sql = "update crm_mail_record set status = 2 where subject = '$subject' and receiver = '$to' and in_time > $time"; $command=$connection->createCommand($sql); $rowCount=$command->execute(); return false; }else{ $transport = [ 'class' => 'Swift_SmtpTransport', 'host' => $configArr['host'], 'username' => $configArr['smtp_username'], 'password' => $configArr['password'], 'port' => $configArr['port'], 'encryption' => 'ssl', ]; if($configArr['port'] == 587){ $transport['encryption'] = 'tls'; }else{ $transport['encryption'] = 'ssl'; } Yii::$app->set('mailer', [ 'class' => 'yii\swiftmailer\Mailer', 'useFileTransport' => false, 'transport' => $transport, 'messageConfig' => [ 'charset' => 'UTF-8', 'from' => [$configArr['send_mail'] => 'Siyade'] ], ]); } // 配置邮箱的使用 // if($config->mail_number){ // $number = intval($config->mail_number); // switch ($number){ // case 1: // $smtp_username = "admin@notify-online.com"; // $host = 'hwsmtp1.exmail.qq.com'; // $password="Notifk99"; // $port = '4650'; // break; // case 2: // $smtp_username = "admin@notify2-online.com"; // $host = 'hwsmtp2.exmail.qq.com'; // $password="Notifk99"; // $port = '4650'; // break; // case 3: // $smtp_username = "admin@notify3-online.com"; // $host = 'hwsmtp3.exmail.qq.com'; // $password="Notifk99"; // $port = '4650'; // break; // case 4: // $smtp_username = "admin@notify4-online.com"; // $host = 'hwsmtp4.exmail.qq.com'; // $password="Notifk99"; // $port = '4650'; // break; // case 5: // $smtp_username = "info@vegardmkt.com"; // $host = 'smtp.fastmail.com'; // $password="q7yka9tdhgym9zuy"; // $port = '465'; // break; // case 6: // $smtp_username = "info2@vegardmkt.com"; // $host = 'smtp.fastmail.com'; // $password="4emg7ux97qxz4fc7"; // $port = '465'; // break; // case 7: // $pay_mail = "adminemail1@vegardmkt.com"; // $smtp_username = "apikey"; // $host = 'smtp.sendgrid.net'; // $password="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"; // $port = '465'; // break; // case 8: // $pay_mail = "adminemail2@vegardmkt.com"; // $smtp_username = "apikey"; // $host = 'smtp.sendgrid.net'; // $password="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"; // $port = '465'; // break; // case 9: // $pay_mail = "adminemail3@vegardmkt.com"; // $smtp_username = "apikey"; // $host = 'smtp.sendgrid.net'; // $password="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"; // $port = '465'; // break; // case 10: // $pay_mail = "adminemail4@vegardmkt.com"; // $smtp_username = "apikey"; // $host = 'smtp.sendgrid.net'; // $password="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"; // $port = '465'; // break; // default: // $pay_mail = "admin@vegardmkt.com"; // $smtp_username = "apikey"; // $host = 'smtp.sendgrid.net'; // $password="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"; // $port = '465'; // } // if($number==999 || $number==7 || $number==8 || $number==9 || $number==10){ // // 设置新的邮箱地址发送 // Yii::$app->set('mailer', [ // 'class' => 'yii\swiftmailer\Mailer', // 'useFileTransport' => false, // 'transport' => [ // 'class' => 'Swift_SmtpTransport', // 'host' => $host, // 'username' => $smtp_username, // 'password' => $password, // 'port' => $port, // 'encryption' => 'ssl', // ], // 'messageConfig' => [ // 'charset' => 'UTF-8', // 'from' => [$pay_mail => 'Vegard'] // ], // ]); // }else{ // // 设置新的邮箱地址发送 // Yii::$app->set('mailer', [ // 'class' => 'yii\swiftmailer\Mailer', // 'useFileTransport' => false, // 'transport' => [ // 'class' => 'Swift_SmtpTransport', // 'host' => $host, // 'username' => $smtp_username, // 'password' => $password, // 'port' => $port, // 'encryption' => 'ssl', // ], // 'messageConfig' => [ // 'charset' => 'UTF-8', // 'from' => [$smtp_username => 'Vegard'] // ], // ]); // } // // } if($subject=="訂單信息"){ $pay_mail_num = intval($config->pay_mail_num); if($pay_mail_num == 0){ $smtp_username = "admin@fastpaypro.net"; $host = 'hwsmtppay.exmail.qq.com'; $port = '4650'; // 设置新的邮箱地址发送 Yii::$app->set('mailer', [ 'class' => 'yii\swiftmailer\Mailer', 'useFileTransport' => false, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => $host, 'username' => $smtp_username, 'password' => 'Fastpak99', 'port' => $port, 'encryption' => 'ssl', ], 'messageConfig' => [ 'charset' => 'UTF-8', 'from' => [$smtp_username => 'fastpaypro'] ], ]); }else{ $pay_mail = $config->pay_mail; $smtp_username = "apikey"; $host = 'smtp.sendgrid.net'; $port = '465'; // 设置新的邮箱地址发送 Yii::$app->set('mailer', [ 'class' => 'yii\swiftmailer\Mailer', 'useFileTransport' => false, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => $host, 'username' => $smtp_username, 'password' => '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', 'port' => $port, 'encryption' => 'ssl', ], 'messageConfig' => [ 'charset' => 'UTF-8', 'from' => [$pay_mail => $pay_mail] ], ]); } } $mail = \Yii::$app->mailer->compose(); $mail->setTo($to); $mail->setSubject($subject); $mail->setHtmlBody( ''.$body); $result = $mail->send(); if ($result) { MailRecord::updateAll(['status' => 1], "id={$mid}" ); return true; }else{ self::sendButNotSaveRecord($subject, $to, $from, $body, $mid); Yii::warning('邮件发送失败'.VarDumper::dumpAsString($result),__METHOD__); } } }