MailHelper.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chenkuan
  5. * Date: 2017/11/13
  6. * Time: 下午1:54
  7. */
  8. namespace backend\helpers;
  9. use backend\models\Config;
  10. use backend\models\MailRecord;
  11. use common\helpers\HashMapHelper;
  12. use yii\helpers\VarDumper;
  13. use yii\httpclient\Client;
  14. use Yii;
  15. class MailHelper
  16. {
  17. const DEF_CONFIG_ID = 1;
  18. const CACHE_CODE = 'cache_code';
  19. const CACHE_TIME = 'cache_time';
  20. /**
  21. * 发送简单邮件
  22. * @param string $subject 主题/标题 如:'找回密码验证'
  23. * @param string $to 接收方邮箱地址
  24. * @param array $paramArray 需要替换的模版内容参数,如:找回密码中的${code} 数组传递['code' => $code]
  25. * @param string $fromName 发送人姓名
  26. * @param string $content 内容模板
  27. * @return bool 成功/失败
  28. */
  29. public static function sendMail($subject, $to, $paramArray, $fromName, $content)
  30. {
  31. $cache = \Yii::$app->cache;
  32. $config = $cache->get('config');
  33. if ($config == null) {
  34. $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
  35. $cache->set('config', $config, 86400);
  36. }
  37. // 本地和测试环境目前禁止发邮件
  38. return self::sendSimpleMail($subject, $to, $config->smtp_from_mail, $content, $paramArray, $fromName);
  39. }
  40. /**
  41. * 发送短信
  42. * @param $mobile
  43. * @return null|string
  44. */
  45. public static function sendCodeSms($mobile)
  46. {
  47. $cache = \Yii::$app->cache;
  48. $lastCode = $cache->get($mobile.self::CACHE_CODE);
  49. if (!empty($lastCode)) {
  50. $time = $cache->get($mobile.self::CACHE_TIME);
  51. if ($time != null && (DateTimeHelper::microtime_float() - $time <= 120000)) {
  52. return null;
  53. }
  54. }
  55. $code = RandomHelper::getRandomNo(6);
  56. $cache->set($mobile.self::CACHE_CODE, $code);
  57. $cache->set($mobile.self::CACHE_TIME, DateTimeHelper::microtime_float());
  58. return $code;
  59. // 本地和测试环境目前禁止发短信
  60. /*
  61. $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
  62. $temp = self::sendBasicSms($mobile, $config->juhe_sms_tpl_id, $code);
  63. if ($temp) {
  64. $cache->set($mobile.self::CACHE_CODE, $code);
  65. $cache->set($mobile.self::CACHE_TIME, DateTimeHelper::microtime_float());
  66. return $code;
  67. } else {
  68. return null;
  69. }
  70. */
  71. }
  72. /**
  73. * 验证短信code
  74. * @param $mobile
  75. * @param $code
  76. * @return bool
  77. */
  78. public static function validSmsCode($mobile, $code)
  79. {
  80. $cache = \Yii::$app->cache;
  81. $lastCode = $cache->get($mobile.self::CACHE_CODE);
  82. if (!empty($lastCode)) {
  83. if ($lastCode != $code) {
  84. return false;
  85. } else {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. /**
  92. * 发送MT4sms/RegSms
  93. * @param $mobile
  94. * @param $tplId
  95. * @param $name
  96. * @param $login
  97. * @param $pwd
  98. * @return \yii\httpclient\Response
  99. */
  100. public static function sendSms($mobile, $tplId, $name, $login, $pwd = '')
  101. {
  102. $map = new HashMapHelper();
  103. $config = Config::findOne([['id' => self::DEF_CONFIG_ID]]);
  104. $map->put('mobile', $mobile);
  105. $map->put('tpl_id', $tplId."");
  106. $tplValue = "#name#=".$name."&#login#=".$login;
  107. if ($pwd) {
  108. $tplValue .= "&#pwd#=".$pwd;
  109. }
  110. $a = urlencode("#");
  111. $b = urlencode("&");
  112. $c = urlencode("=");
  113. $tplValue = str_replace("#", $a, $tplValue);
  114. $tplValue = str_replace("&", $b, $tplValue);
  115. $tplValue = str_replace("=", $c, $tplValue);
  116. $map->put('tpl_value', $tplValue);
  117. $map->put('dtype', "json");
  118. $map->put('key', $config->juhe_sms_key);
  119. return true;
  120. $url = "http://v.juhe.cn/sms/send";
  121. $client = (new Client(['baseUrl' => $url]));
  122. $json = $client->get($url, self::buildParams($map->H_table))->send();
  123. return $json;
  124. }
  125. /**
  126. * @param string $mobile
  127. * @return array
  128. */
  129. public static function getCodeSms($mobile)
  130. {
  131. $result = [
  132. 'code' => '',
  133. 'created' => 0,
  134. ];
  135. $code = \Yii::$app->getCache()->get($mobile . self::CACHE_CODE);
  136. $created = \Yii::$app->getCache()->get($mobile . self::CACHE_TIME);
  137. if ($code && $created) {
  138. $result['code'] = $code;
  139. $result['created'] = $created;
  140. }
  141. return $result;
  142. }
  143. /**
  144. * @param $mobile
  145. * @param $tplId
  146. * @param $code
  147. * @return mixed
  148. */
  149. private static function sendBasicSms($mobile, $tplId, $code)
  150. {
  151. $map = new HashMapHelper();
  152. $config = Config::findOne([['id' => self::DEF_CONFIG_ID]]);
  153. $map->put('mobile', $mobile);
  154. $map->put('tpl_id', $tplId."");
  155. $tplValue = "#code#=".$code;
  156. $map->put('tpl_value', urlencode($tplValue));
  157. $map->put('dtype', "json");
  158. $map->put('key', $config->juhe_sms_key);
  159. $url = "http://v.juhe.cn/sms/send";
  160. $client = (new Client(['baseUrl' => $url]));
  161. $json = $client->get($url, self::buildParams($map->H_table))->send();
  162. return $json;
  163. }
  164. /**
  165. * 生成token
  166. * @param array $param
  167. * @return mixed
  168. */
  169. private static function buildParams($param)
  170. {
  171. ksort($param);//升序排序
  172. $strOauth = "";
  173. foreach ($param as $key => $val) {
  174. if ($key == 'token' || is_array($val) || $val === null) {
  175. continue;
  176. }
  177. $val = strval($val);
  178. $strOauth .= $key . '=' . $val . '&';
  179. }
  180. $strOauth = rtrim($strOauth, '&');
  181. $param['token'] = md5($strOauth);
  182. return $param;
  183. }
  184. /**
  185. * 发送简单邮件封装方法
  186. * @param $subject
  187. * @param $to
  188. * @param $from
  189. * @param $content
  190. * @param $paramArray
  191. * @param $fromName
  192. * @return bool
  193. */
  194. private static function sendSimpleMail($subject, $to, $from, $content, $paramArray, $fromName)
  195. {
  196. $isSuccess = false;
  197. $replace_content = self::replaceVariable($content, $paramArray);
  198. $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
  199. /*
  200. $smtp_username = $config->smtp_username;
  201. $smtp_password = $config->smtp_password;
  202. \Yii::$app->mail->apiUser = $smtp_username;
  203. \Yii::$app->mail->apiKey = $smtp_password;
  204. $result = \Yii::$app->mail->sendNormalMail($subject, '<html>'.$replace_content, [$to], $from, $fromName);
  205. if ($result['result'] === true) {
  206. self::saveRecord($result['result'], $to, $subject, $replace_content);
  207. $isSuccess = true;
  208. }
  209. */
  210. // 配置邮箱的使用
  211. if($config->mail_number){
  212. $number = intval($config->mail_number);
  213. switch ($number){
  214. case 1:
  215. $smtp_username = "admin@notify-online.com";
  216. $host = 'hwsmtp1.exmail.qq.com';
  217. $password="Notifk99";
  218. $port = '4650';
  219. break;
  220. case 2:
  221. $smtp_username = "admin@notify2-online.com";
  222. $host = 'hwsmtp2.exmail.qq.com';
  223. $password="Notifk99";
  224. $port = '4650';
  225. break;
  226. case 3:
  227. $smtp_username = "admin@notify3-online.com";
  228. $host = 'hwsmtp3.exmail.qq.com';
  229. $password="Notifk99";
  230. $port = '4650';
  231. break;
  232. case 4:
  233. $smtp_username = "admin@notify4-online.com";
  234. $host = 'hwsmtp4.exmail.qq.com';
  235. $password="Notifk99";
  236. $port = '4650';
  237. break;
  238. case 5:
  239. $smtp_username = "cs@helongfx.com";
  240. $host = 'smtp.fastmail.com';
  241. $password="kj28yz387p974u6t";
  242. $port = '465';
  243. break;
  244. case 6:
  245. $smtp_username = "info@helongfx.com";
  246. $host = 'smtp.fastmail.com';
  247. $password="c2l3ynl29r8kpxbn";
  248. $port = '465';
  249. break;
  250. default:
  251. $smtp_username = "admin@helongfx.com";
  252. $host = 'hwsmtp.exmail.qq.com';
  253. $password="Helonk99";
  254. $port = '465';
  255. }
  256. // 设置新的邮箱地址发送
  257. Yii::$app->set('mailer', [
  258. 'class' => 'yii\swiftmailer\Mailer',
  259. 'useFileTransport' => false,
  260. 'transport' => [
  261. 'class' => 'Swift_SmtpTransport',
  262. 'host' => $host,
  263. 'username' => $smtp_username,
  264. 'password' => $password,
  265. 'port' => $port,
  266. 'encryption' => 'ssl',
  267. ],
  268. 'messageConfig' => [
  269. 'charset' => 'UTF-8',
  270. 'from' => [$smtp_username => '和隆']
  271. ],
  272. ]);
  273. }
  274. $mailid = self::saveRecord($isSuccess, $to, $subject, $replace_content);
  275. //如果是订单信息就开始设置为paycenter的邮箱地址发送
  276. if($subject=="訂單信息"){
  277. $pay_mail_num = intval($config->pay_mail_num);
  278. if($pay_mail_num == 0){
  279. $smtp_username = "admin@fastpaypro.net";
  280. $host = 'hwsmtppay.exmail.qq.com';
  281. $port = '4650';
  282. // 设置新的邮箱地址发送
  283. Yii::$app->set('mailer', [
  284. 'class' => 'yii\swiftmailer\Mailer',
  285. 'useFileTransport' => false,
  286. 'transport' => [
  287. 'class' => 'Swift_SmtpTransport',
  288. 'host' => $host,
  289. 'username' => $smtp_username,
  290. 'password' => 'Fastpak99',
  291. 'port' => $port,
  292. 'encryption' => 'ssl',
  293. ],
  294. 'messageConfig' => [
  295. 'charset' => 'UTF-8',
  296. 'from' => [$smtp_username => 'fastpaypro']
  297. ],
  298. ]);
  299. }else{
  300. $pay_mail = $config->pay_mail;
  301. $smtp_username = "apikey";
  302. $host = 'smtp.sendgrid.net';
  303. $port = '465';
  304. // 设置新的邮箱地址发送
  305. Yii::$app->set('mailer', [
  306. 'class' => 'yii\swiftmailer\Mailer',
  307. 'useFileTransport' => false,
  308. 'transport' => [
  309. 'class' => 'Swift_SmtpTransport',
  310. 'host' => $host,
  311. 'username' => $smtp_username,
  312. 'password' => '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',
  313. 'port' => $port,
  314. 'encryption' => 'ssl',
  315. ],
  316. 'messageConfig' => [
  317. 'charset' => 'UTF-8',
  318. 'from' => [$pay_mail => $pay_mail]
  319. ],
  320. ]);
  321. }
  322. }
  323. $mail = \Yii::$app->mailer->compose();
  324. $mail->setTo($to);
  325. $mail->setSubject($subject);
  326. $mail->setHtmlBody( '<html>'.$replace_content);
  327. $result = $mail->send();
  328. if ($result) {
  329. $isSuccess = true;
  330. } else {
  331. Yii::warning('邮件发送失败' . VarDumper::dumpAsString($result), __METHOD__);
  332. }
  333. $status = ($isSuccess) ? 1 : 2;
  334. MailRecord::updateAll(['status' => $status], "id={$mailid}" );
  335. return $isSuccess;
  336. }
  337. /**
  338. * 将模板中的变量替换为入参
  339. * @param $content string
  340. * @param $paramArray array
  341. * @return string
  342. */
  343. public static function replaceVariable($content, $paramArray)
  344. {
  345. if (!empty($content) && !empty($paramArray)) {
  346. foreach ($paramArray as $key => $value) {
  347. $variable='${'.$key.'}';
  348. $variable1 = '#{'.$key.'}';
  349. $variable2 = '#'.$key.'#';
  350. if (is_null($value)) {
  351. continue;
  352. }
  353. while (strpos($content, $variable, 0) > -1) {
  354. $content = str_replace($variable, $value, $content);
  355. }
  356. while (strpos($content, $variable1, 0) > -1) {
  357. $content = str_replace($variable1, $value, $content);
  358. }
  359. while (strpos($content, $variable2, 0) > -1) {
  360. $content = str_replace($variable2, $value, $content);
  361. }
  362. }
  363. }
  364. return $content;
  365. }
  366. /**
  367. * 保存发送的邮件记录到crm_mail_record表
  368. * @param $status
  369. * @param $receiver
  370. * @param $subject
  371. * @param $content
  372. * @return bool
  373. */
  374. private static function saveRecord($status, $receiver, $subject, $content)
  375. {
  376. $mailRecord = new MailRecord();
  377. $mailRecord->content = $content;
  378. $mailRecord->receiver = $receiver;
  379. if ($status) {
  380. $mailRecord->status = 1;
  381. } else {
  382. $mailRecord->status = 0;
  383. }
  384. $mailRecord->subject = $subject;
  385. $mailRecord->in_time = DateTimeHelper::microtime_float();
  386. $mailRecord->save();
  387. file_put_contents('sendmail.txt',$mailRecord->id);
  388. return $mailRecord->id;
  389. }
  390. /**
  391. * 发送email邮件
  392. * @param $subject
  393. * @param $to
  394. * @param $from
  395. * @param $body
  396. * @param $mid
  397. * @return bool
  398. */
  399. public static function sendButNotSaveRecord($subject, $to, $from, $body, $mid)
  400. {
  401. $isSuccess = false;
  402. $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
  403. /*
  404. $smtp_username = $config->smtp_username;
  405. $smtp_password = $config->smtp_password;
  406. \Yii::$app->mail->apiUser = $smtp_username;
  407. \Yii::$app->mail->apiKey = $smtp_password;
  408. $result = \Yii::$app->mail->sendNormalMail($subject, '<html>'.$body, $to, $from);
  409. if ($result['result'] === true) {
  410. $isSuccess = true;
  411. }
  412. return $isSuccess;
  413. */
  414. // 配置邮箱的使用
  415. if($config->mail_number){
  416. $number = intval($config->mail_number);
  417. switch ($number){
  418. case 1:
  419. $smtp_username = "admin@notify-online.com";
  420. $host = 'hwsmtp1.exmail.qq.com';
  421. $password="Notifk99";
  422. $port = '4650';
  423. break;
  424. case 2:
  425. $smtp_username = "admin@notify2-online.com";
  426. $host = 'hwsmtp2.exmail.qq.com';
  427. $password="Notifk99";
  428. $port = '4650';
  429. break;
  430. case 3:
  431. $smtp_username = "admin@notify3-online.com";
  432. $host = 'hwsmtp3.exmail.qq.com';
  433. $password="Notifk99";
  434. $port = '4650';
  435. break;
  436. case 4:
  437. $smtp_username = "admin@notify4-online.com";
  438. $host = 'hwsmtp4.exmail.qq.com';
  439. $password="Notifk99";
  440. $port = '4650';
  441. break;
  442. /*case 5:
  443. $smtp_username = "admin@fusian-fx-mail4.com";
  444. $host = 'hwsmtp4.exmail.qq.com';
  445. $port = '4650';
  446. break;*/
  447. default:
  448. $smtp_username = "admin@helongfx.com";
  449. $host = 'hwsmtp.exmail.qq.com';
  450. $password="Helonk99";
  451. $port = '465';
  452. }
  453. // 设置新的邮箱地址发送
  454. Yii::$app->set('mailer', [
  455. 'class' => 'yii\swiftmailer\Mailer',
  456. 'useFileTransport' => false,
  457. 'transport' => [
  458. 'class' => 'Swift_SmtpTransport',
  459. 'host' => $host,
  460. 'username' => $smtp_username,
  461. 'password' => $password,
  462. 'port' => $port,
  463. 'encryption' => 'ssl',
  464. ],
  465. 'messageConfig' => [
  466. 'charset' => 'UTF-8',
  467. 'from' => [$smtp_username => '和隆']
  468. ],
  469. ]);
  470. }
  471. $mail = \Yii::$app->mailer->compose();
  472. $mail->setTo($to);
  473. $mail->setSubject($subject);
  474. $mail->setHtmlBody( '<html>'.$body);
  475. if ($mail->send()) {
  476. MailRecord::updateAll(['status' => 1], "id={$mid}" );
  477. return true;
  478. }
  479. return false;
  480. }
  481. }