MailHelper11.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 = "info@vegardmkt.com";
  240. $host = 'fast1.fastmail.com';
  241. $password="q7yka9tdhgym9zuy";
  242. $port = '465';
  243. break;
  244. case 6:
  245. $smtp_username = "info2@vegardmkt.com";
  246. $host = 'smtp.fastmail.com';
  247. $password="4emg7ux97qxz4fc7";
  248. $port = '465';
  249. break;
  250. case 7:
  251. $pay_mail = "admin1@vegardmkt.com";
  252. $smtp_username = "apikey";
  253. $host = 'smtp.sendgrid.net';
  254. $password="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";
  255. $port = '465';
  256. break;
  257. case 8:
  258. $pay_mail = "cs2@vegardmkt.com";
  259. $smtp_username = "apikey";
  260. $host = 'smtp.sendgrid.net';
  261. $password="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";
  262. $port = '465';
  263. break;
  264. case 9:
  265. $pay_mail = "cs3@vegardmkt.com";
  266. $smtp_username = "apikey";
  267. $host = 'smtp.sendgrid.net';
  268. $password="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";
  269. $port = '465';
  270. break;
  271. case 10:
  272. $pay_mail = "cs4@vegardmkt.com";
  273. $smtp_username = "apikey";
  274. $host = 'smtp.sendgrid.net';
  275. $password="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";
  276. $port = '465';
  277. break;
  278. default:
  279. $pay_mail = "admin@vegardmkt.com";
  280. $smtp_username = "apikey";
  281. $host = 'smtp.sendgrid.net';
  282. $password="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";
  283. $port = '465';
  284. break;
  285. }
  286. if($number==999 || $number==7 || $number==8 || $number==9 || $number==10){
  287. // 设置新的邮箱地址发送
  288. Yii::$app->set('mailer', [
  289. 'class' => 'yii\swiftmailer\Mailer',
  290. 'useFileTransport' => false,
  291. 'transport' => [
  292. 'class' => 'Swift_SmtpTransport',
  293. 'host' => $host,
  294. 'username' => $smtp_username,
  295. 'password' => $password,
  296. 'port' => $port,
  297. 'encryption' => 'ssl',
  298. ],
  299. 'messageConfig' => [
  300. 'charset' => 'UTF-8',
  301. 'from' => [$pay_mail => 'Vegard']
  302. ],
  303. ]);
  304. }else{
  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' => $password,
  313. 'port' => $port,
  314. 'encryption' => 'ssl',
  315. ],
  316. 'messageConfig' => [
  317. 'charset' => 'UTF-8',
  318. 'from' => [$smtp_username => 'Vegard']
  319. ],
  320. ]);
  321. }
  322. }
  323. $mailid = self::saveRecord($isSuccess, $to, $subject, $replace_content,$number);
  324. //如果是订单信息就开始设置为paycenter的邮箱地址发送
  325. if($subject=="訂單信息"){
  326. $pay_mail_num = intval($config->pay_mail_num);
  327. if($pay_mail_num == 0){
  328. $smtp_username = "admin@fastpaypro.net";
  329. $host = 'hwsmtppay.exmail.qq.com';
  330. $port = '4650';
  331. // 设置新的邮箱地址发送
  332. Yii::$app->set('mailer', [
  333. 'class' => 'yii\swiftmailer\Mailer',
  334. 'useFileTransport' => false,
  335. 'transport' => [
  336. 'class' => 'Swift_SmtpTransport',
  337. 'host' => $host,
  338. 'username' => $smtp_username,
  339. 'password' => 'Fastpak99',
  340. 'port' => $port,
  341. 'encryption' => 'ssl',
  342. ],
  343. 'messageConfig' => [
  344. 'charset' => 'UTF-8',
  345. 'from' => [$smtp_username => 'fastpaypro']
  346. ],
  347. ]);
  348. }else{
  349. $pay_mail = $config->pay_mail;
  350. $smtp_username = "apikey";
  351. $host = 'smtp.sendgrid.net';
  352. $port = '465';
  353. // 设置新的邮箱地址发送
  354. Yii::$app->set('mailer', [
  355. 'class' => 'yii\swiftmailer\Mailer',
  356. 'useFileTransport' => false,
  357. 'transport' => [
  358. 'class' => 'Swift_SmtpTransport',
  359. 'host' => $host,
  360. 'username' => $smtp_username,
  361. 'password' => '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',
  362. 'port' => $port,
  363. 'encryption' => 'ssl',
  364. ],
  365. 'messageConfig' => [
  366. 'charset' => 'UTF-8',
  367. 'from' => [$pay_mail => $pay_mail]
  368. ],
  369. ]);
  370. }
  371. }
  372. $mail = \Yii::$app->mailer->compose();
  373. $mail->setTo($to);
  374. $mail->setSubject($subject);
  375. $mail->setHtmlBody( '<html>'.$replace_content);
  376. $result = $mail->send();
  377. if ($result) {
  378. $isSuccess = true;
  379. } else {
  380. Yii::warning('邮件发送失败' . VarDumper::dumpAsString($result), __METHOD__);
  381. }
  382. $status = ($isSuccess) ? 1 : 2;
  383. MailRecord::updateAll(['status' => $status], "id={$mailid}" );
  384. return $isSuccess;
  385. }
  386. /**
  387. * 将模板中的变量替换为入参
  388. * @param $content string
  389. * @param $paramArray array
  390. * @return string
  391. */
  392. public static function replaceVariable($content, $paramArray)
  393. {
  394. if (!empty($content) && !empty($paramArray)) {
  395. foreach ($paramArray as $key => $value) {
  396. $variable='${'.$key.'}';
  397. $variable1 = '#{'.$key.'}';
  398. $variable2 = '#'.$key.'#';
  399. if (is_null($value)) {
  400. continue;
  401. }
  402. while (strpos($content, $variable, 0) > -1) {
  403. $content = str_replace($variable, $value, $content);
  404. }
  405. while (strpos($content, $variable1, 0) > -1) {
  406. $content = str_replace($variable1, $value, $content);
  407. }
  408. while (strpos($content, $variable2, 0) > -1) {
  409. $content = str_replace($variable2, $value, $content);
  410. }
  411. }
  412. }
  413. return $content;
  414. }
  415. /**
  416. * 保存发送的邮件记录到crm_mail_record表
  417. * @param $status
  418. * @param $receiver
  419. * @param $subject
  420. * @param $content
  421. * @return bool
  422. */
  423. private static function saveRecord($status, $receiver, $subject, $content,$num)
  424. {
  425. $mailRecord = new MailRecord();
  426. $mailRecord->content = $content;
  427. $mailRecord->receiver = $receiver;
  428. if ($status) {
  429. $mailRecord->status = 1;
  430. } else {
  431. $mailRecord->status = 0;
  432. }
  433. $mailRecord->subject = $subject;
  434. $mailRecord->in_time = DateTimeHelper::microtime_float();
  435. $mailRecord->mail_num = $num;
  436. $mailRecord->save();
  437. return $mailRecord->id;
  438. }
  439. /**
  440. * 发送email邮件
  441. * @param $subject
  442. * @param $to
  443. * @param $from
  444. * @param $body
  445. * @param $mid
  446. * @return bool
  447. */
  448. public static function sendButNotSaveRecord($subject, $to, $from, $body, $mid)
  449. {
  450. $isSuccess = false;
  451. $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
  452. /*
  453. $smtp_username = $config->smtp_username;
  454. $smtp_password = $config->smtp_password;
  455. \Yii::$app->mail->apiUser = $smtp_username;
  456. \Yii::$app->mail->apiKey = $smtp_password;
  457. $result = \Yii::$app->mail->sendNormalMail($subject, '<html>'.$body, $to, $from);
  458. if ($result['result'] === true) {
  459. $isSuccess = true;
  460. }
  461. return $isSuccess;
  462. */
  463. // 配置邮箱的使用
  464. if($config->mail_number){
  465. $number = intval($config->mail_number);
  466. switch ($number){
  467. case 1:
  468. $smtp_username = "admin@notify-online.com";
  469. $host = 'hwsmtp1.exmail.qq.com';
  470. $password="Notifk99";
  471. $port = '4650';
  472. break;
  473. case 2:
  474. $smtp_username = "admin@notify2-online.com";
  475. $host = 'hwsmtp2.exmail.qq.com';
  476. $password="Notifk99";
  477. $port = '4650';
  478. break;
  479. case 3:
  480. $smtp_username = "admin@notify3-online.com";
  481. $host = 'hwsmtp3.exmail.qq.com';
  482. $password="Notifk99";
  483. $port = '4650';
  484. break;
  485. case 4:
  486. $smtp_username = "admin@notify4-online.com";
  487. $host = 'hwsmtp4.exmail.qq.com';
  488. $password="Notifk99";
  489. $port = '4650';
  490. break;
  491. case 5:
  492. $smtp_username = "info@vegardmkt.com";
  493. $host = 'smtp.fastmail.com';
  494. $password="q7yka9tdhgym9zuy";
  495. $port = '465';
  496. break;
  497. case 6:
  498. $smtp_username = "info2@vegardmkt.com";
  499. $host = 'smtp.fastmail.com';
  500. $password="4emg7ux97qxz4fc7";
  501. $port = '465';
  502. break;
  503. case 7:
  504. $pay_mail = "adminemail1@vegardmkt.com";
  505. $smtp_username = "apikey";
  506. $host = 'smtp.sendgrid.net';
  507. $password="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";
  508. $port = '465';
  509. break;
  510. case 8:
  511. $pay_mail = "adminemail2@vegardmkt.com";
  512. $smtp_username = "apikey";
  513. $host = 'smtp.sendgrid.net';
  514. $password="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";
  515. $port = '465';
  516. break;
  517. case 9:
  518. $pay_mail = "adminemail3@vegardmkt.com";
  519. $smtp_username = "apikey";
  520. $host = 'smtp.sendgrid.net';
  521. $password="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";
  522. $port = '465';
  523. break;
  524. case 10:
  525. $pay_mail = "adminemail4@vegardmkt.com";
  526. $smtp_username = "apikey";
  527. $host = 'smtp.sendgrid.net';
  528. $password="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";
  529. $port = '465';
  530. break;
  531. default:
  532. $pay_mail = "admin@vegardmkt.com";
  533. $smtp_username = "apikey";
  534. $host = 'smtp.sendgrid.net';
  535. $password="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";
  536. $port = '465';
  537. }
  538. if($number==999 || $number==7 || $number==8 || $number==9 || $number==10){
  539. // 设置新的邮箱地址发送
  540. Yii::$app->set('mailer', [
  541. 'class' => 'yii\swiftmailer\Mailer',
  542. 'useFileTransport' => false,
  543. 'transport' => [
  544. 'class' => 'Swift_SmtpTransport',
  545. 'host' => $host,
  546. 'username' => $smtp_username,
  547. 'password' => $password,
  548. 'port' => $port,
  549. 'encryption' => 'ssl',
  550. ],
  551. 'messageConfig' => [
  552. 'charset' => 'UTF-8',
  553. 'from' => [$pay_mail => 'Vegard']
  554. ],
  555. ]);
  556. }else{
  557. // 设置新的邮箱地址发送
  558. Yii::$app->set('mailer', [
  559. 'class' => 'yii\swiftmailer\Mailer',
  560. 'useFileTransport' => false,
  561. 'transport' => [
  562. 'class' => 'Swift_SmtpTransport',
  563. 'host' => $host,
  564. 'username' => $smtp_username,
  565. 'password' => $password,
  566. 'port' => $port,
  567. 'encryption' => 'ssl',
  568. ],
  569. 'messageConfig' => [
  570. 'charset' => 'UTF-8',
  571. 'from' => [$smtp_username => 'Vegard']
  572. ],
  573. ]);
  574. }
  575. }
  576. if($subject=="訂單信息"){
  577. $pay_mail_num = intval($config->pay_mail_num);
  578. if($pay_mail_num == 0){
  579. $smtp_username = "admin@fastpaypro.net";
  580. $host = 'hwsmtppay.exmail.qq.com';
  581. $port = '4650';
  582. // 设置新的邮箱地址发送
  583. Yii::$app->set('mailer', [
  584. 'class' => 'yii\swiftmailer\Mailer',
  585. 'useFileTransport' => false,
  586. 'transport' => [
  587. 'class' => 'Swift_SmtpTransport',
  588. 'host' => $host,
  589. 'username' => $smtp_username,
  590. 'password' => 'Fastpak99',
  591. 'port' => $port,
  592. 'encryption' => 'ssl',
  593. ],
  594. 'messageConfig' => [
  595. 'charset' => 'UTF-8',
  596. 'from' => [$smtp_username => 'fastpaypro']
  597. ],
  598. ]);
  599. }else{
  600. $pay_mail = $config->pay_mail;
  601. $smtp_username = "apikey";
  602. $host = 'smtp.sendgrid.net';
  603. $port = '465';
  604. // 设置新的邮箱地址发送
  605. Yii::$app->set('mailer', [
  606. 'class' => 'yii\swiftmailer\Mailer',
  607. 'useFileTransport' => false,
  608. 'transport' => [
  609. 'class' => 'Swift_SmtpTransport',
  610. 'host' => $host,
  611. 'username' => $smtp_username,
  612. 'password' => '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',
  613. 'port' => $port,
  614. 'encryption' => 'ssl',
  615. ],
  616. 'messageConfig' => [
  617. 'charset' => 'UTF-8',
  618. 'from' => [$pay_mail => $pay_mail]
  619. ],
  620. ]);
  621. }
  622. }
  623. $mail = \Yii::$app->mailer->compose();
  624. $mail->setTo($to);
  625. $mail->setSubject($subject);
  626. $mail->setHtmlBody( '<html>'.$body);
  627. if ($mail->send()) {
  628. MailRecord::updateAll(['status' => 1], "id={$mid}" );
  629. return true;
  630. }
  631. return false;
  632. }
  633. }