MailHelper.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: User
  5. * Date: 2019/6/19
  6. * Time: 下午 5:03
  7. */
  8. namespace backend\helpers;
  9. use backend\models\Config;
  10. use backend\models\MailRecord;
  11. use backend\models\MailConfig;
  12. use common\helpers\HashMapHelper;
  13. use yii\helpers\VarDumper;
  14. use yii\httpclient\Client;
  15. use Yii;
  16. class MailHelper
  17. {
  18. const DEF_CONFIG_ID = 1;
  19. const CACHE_CODE = 'cache_code';
  20. const CACHE_TIME = 'cache_time';
  21. /**
  22. * 发送简单邮件
  23. * @param string $subject 主题/标题 如:'找回密码验证'
  24. * @param string $to 接收方邮箱地址
  25. * @param array $paramArray 需要替换的模版内容参数,如:找回密码中的${code} 数组传递['code' => $code]
  26. * @param string $fromName 发送人姓名
  27. * @param string $content 内容模板
  28. * @return bool 成功/失败
  29. */
  30. public static function sendMail($subject, $to, $paramArray, $fromName, $content)
  31. {
  32. $cache = \Yii::$app->cache;
  33. $config = $cache->get('config');
  34. if ($config == null) {
  35. $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
  36. $cache->set('config', $config, 86400);
  37. }
  38. // 本地和测试环境目前禁止发邮件
  39. return self::sendSimpleMail($subject, $to, $config->smtp_from_mail, $content, $paramArray, $fromName);
  40. }
  41. /**
  42. * 发送短信
  43. * @param $mobile
  44. * @return null|string
  45. */
  46. public static function sendCodeSms($mobile)
  47. {
  48. $cache = \Yii::$app->cache;
  49. $lastCode = $cache->get($mobile.self::CACHE_CODE);
  50. if (!empty($lastCode)) {
  51. $time = $cache->get($mobile.self::CACHE_TIME);
  52. if ($time != null && (DateTimeHelper::microtime_float() - $time <= 120000)) {
  53. return null;
  54. }
  55. }
  56. $code = RandomHelper::getRandomNo(6);
  57. $cache->set($mobile.self::CACHE_CODE, $code);
  58. $cache->set($mobile.self::CACHE_TIME, DateTimeHelper::microtime_float());
  59. return $code;
  60. // 本地和测试环境目前禁止发短信
  61. /*
  62. $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
  63. $temp = self::sendBasicSms($mobile, $config->juhe_sms_tpl_id, $code);
  64. if ($temp) {
  65. $cache->set($mobile.self::CACHE_CODE, $code);
  66. $cache->set($mobile.self::CACHE_TIME, DateTimeHelper::microtime_float());
  67. return $code;
  68. } else {
  69. return null;
  70. }
  71. */
  72. }
  73. /**
  74. * 验证短信code
  75. * @param $mobile
  76. * @param $code
  77. * @return bool
  78. */
  79. public static function validSmsCode($mobile, $code)
  80. {
  81. $cache = \Yii::$app->cache;
  82. $lastCode = $cache->get($mobile.self::CACHE_CODE);
  83. if (!empty($lastCode)) {
  84. if ($lastCode != $code) {
  85. return false;
  86. } else {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. /**
  93. * 发送MT4sms/RegSms
  94. * @param $mobile
  95. * @param $tplId
  96. * @param $name
  97. * @param $login
  98. * @param $pwd
  99. * @return \yii\httpclient\Response
  100. */
  101. public static function sendSms($mobile, $tplId, $name, $login, $pwd = '')
  102. {
  103. $map = new HashMapHelper();
  104. $config = Config::findOne([['id' => self::DEF_CONFIG_ID]]);
  105. $map->put('mobile', $mobile);
  106. $map->put('tpl_id', $tplId."");
  107. $tplValue = "#name#=".$name."&#login#=".$login;
  108. if ($pwd) {
  109. $tplValue .= "&#pwd#=".$pwd;
  110. }
  111. $a = urlencode("#");
  112. $b = urlencode("&");
  113. $c = urlencode("=");
  114. $tplValue = str_replace("#", $a, $tplValue);
  115. $tplValue = str_replace("&", $b, $tplValue);
  116. $tplValue = str_replace("=", $c, $tplValue);
  117. $map->put('tpl_value', $tplValue);
  118. $map->put('dtype', "json");
  119. $map->put('key', $config->juhe_sms_key);
  120. return true;
  121. $url = "http://v.juhe.cn/sms/send";
  122. $client = (new Client(['baseUrl' => $url]));
  123. $json = $client->get($url, self::buildParams($map->H_table))->send();
  124. return $json;
  125. }
  126. /**
  127. * @param string $mobile
  128. * @return array
  129. */
  130. public static function getCodeSms($mobile)
  131. {
  132. $result = [
  133. 'code' => '',
  134. 'created' => 0,
  135. ];
  136. $code = \Yii::$app->getCache()->get($mobile . self::CACHE_CODE);
  137. $created = \Yii::$app->getCache()->get($mobile . self::CACHE_TIME);
  138. if ($code && $created) {
  139. $result['code'] = $code;
  140. $result['created'] = $created;
  141. }
  142. return $result;
  143. }
  144. /**
  145. * @param $mobile
  146. * @param $tplId
  147. * @param $code
  148. * @return mixed
  149. */
  150. private static function sendBasicSms($mobile, $tplId, $code)
  151. {
  152. $map = new HashMapHelper();
  153. $config = Config::findOne([['id' => self::DEF_CONFIG_ID]]);
  154. $map->put('mobile', $mobile);
  155. $map->put('tpl_id', $tplId."");
  156. $tplValue = "#code#=".$code;
  157. $map->put('tpl_value', urlencode($tplValue));
  158. $map->put('dtype', "json");
  159. $map->put('key', $config->juhe_sms_key);
  160. $url = "http://v.juhe.cn/sms/send";
  161. $client = (new Client(['baseUrl' => $url]));
  162. $json = $client->get($url, self::buildParams($map->H_table))->send();
  163. return $json;
  164. }
  165. /**
  166. * 生成token
  167. * @param array $param
  168. * @return mixed
  169. */
  170. private static function buildParams($param)
  171. {
  172. ksort($param);//升序排序
  173. $strOauth = "";
  174. foreach ($param as $key => $val) {
  175. if ($key == 'token' || is_array($val) || $val === null) {
  176. continue;
  177. }
  178. $val = strval($val);
  179. $strOauth .= $key . '=' . $val . '&';
  180. }
  181. $strOauth = rtrim($strOauth, '&');
  182. $param['token'] = md5($strOauth);
  183. return $param;
  184. }
  185. /**
  186. * 发送简单邮件封装方法
  187. * @param $subject
  188. * @param $to
  189. * @param $from
  190. * @param $content
  191. * @param $paramArray
  192. * @param $fromName
  193. * @return bool
  194. */
  195. private static function sendSimpleMail($subject, $to, $from, $content, $paramArray, $fromName)
  196. {
  197. $isSuccess = false;
  198. $replace_content = self::replaceVariable($content, $paramArray);
  199. $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
  200. $mailConfig = new MailConfig();
  201. $configArr = $mailConfig->getMailConfig($to);
  202. if(!$configArr){
  203. $connection=Yii::$app->db;
  204. $time = time() - 300;
  205. //将5分钟内发送给相同收件人和相同主题的发件记录设置为不可发送状态
  206. $sql = "update crm_mail_record set status = 2 where subject = $subject and receiver = $to and in_time > $time";
  207. $command=$connection->createCommand($sql);
  208. $rowCount=$command->execute();
  209. }else{
  210. Yii::$app->set('mailer', [
  211. 'class' => 'yii\swiftmailer\Mailer',
  212. 'useFileTransport' => false,
  213. 'transport' => [
  214. 'class' => 'Swift_SmtpTransport',
  215. 'host' => $configArr['host'],
  216. 'username' => $configArr['smtp_username'],
  217. 'password' => $configArr['password'],
  218. 'port' => $configArr['port'],
  219. 'encryption' => 'ssl',
  220. ],
  221. 'messageConfig' => [
  222. 'charset' => 'UTF-8',
  223. 'from' => [$configArr['send_mail'] => 'Vegard']
  224. ],
  225. ]);
  226. }
  227. // 配置邮箱的使用
  228. // if($config->mail_number){
  229. // $number = intval($config->mail_number);
  230. // switch ($number){
  231. // case 1:
  232. // $smtp_username = "admin@notify-online.com";
  233. // $host = 'hwsmtp1.exmail.qq.com';
  234. // $password="Notifk99";
  235. // $port = '4650';
  236. // break;
  237. // case 2:
  238. // $smtp_username = "admin@notify2-online.com";
  239. // $host = 'hwsmtp2.exmail.qq.com';
  240. // $password="Notifk99";
  241. // $port = '4650';
  242. // break;
  243. // case 3:
  244. // $smtp_username = "admin@notify3-online.com";
  245. // $host = 'hwsmtp3.exmail.qq.com';
  246. // $password="Notifk99";
  247. // $port = '4650';
  248. // break;
  249. // case 4:
  250. // $smtp_username = "admin@notify4-online.com";
  251. // $host = 'hwsmtp4.exmail.qq.com';
  252. // $password="Notifk99";
  253. // $port = '4650';
  254. // break;
  255. // case 5:
  256. // $smtp_username = "info@vegardmkt.com";
  257. // $host = 'fast1.fastmail.com';
  258. // $password="q7yka9tdhgym9zuy";
  259. // $port = '465';
  260. // break;
  261. // case 6:
  262. // $smtp_username = "info2@vegardmkt.com";
  263. // $host = 'smtp.fastmail.com';
  264. // $password="4emg7ux97qxz4fc7";
  265. // $port = '465';
  266. // break;
  267. // case 7:
  268. // $pay_mail = "admin1@vegardmkt.com";
  269. // $smtp_username = "apikey";
  270. // $host = 'smtp.sendgrid.net';
  271. // $password="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";
  272. // $port = '465';
  273. // break;
  274. // case 8:
  275. // $pay_mail = "cs2@vegardmkt.com";
  276. // $smtp_username = "apikey";
  277. // $host = 'smtp.sendgrid.net';
  278. // $password="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";
  279. // $port = '465';
  280. // break;
  281. // case 9:
  282. // $pay_mail = "cs3@vegardmkt.com";
  283. // $smtp_username = "apikey";
  284. // $host = 'smtp.sendgrid.net';
  285. // $password="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";
  286. // $port = '465';
  287. // break;
  288. // case 10:
  289. // $pay_mail = "cs4@vegardmkt.com";
  290. // $smtp_username = "apikey";
  291. // $host = 'smtp.sendgrid.net';
  292. // $password="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";
  293. // $port = '465';
  294. // break;
  295. // default:
  296. // $pay_mail = "admin@vegardmkt.com";
  297. // $smtp_username = "apikey";
  298. // $host = 'smtp.sendgrid.net';
  299. // $password="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";
  300. // $port = '465';
  301. // break;
  302. // }
  303. // if($number==999 || $number==7 || $number==8 || $number==9 || $number==10){
  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' => $password,
  313. // 'port' => $port,
  314. // 'encryption' => 'ssl',
  315. // ],
  316. // 'messageConfig' => [
  317. // 'charset' => 'UTF-8',
  318. // 'from' => [$pay_mail => 'Vegard']
  319. // ],
  320. // ]);
  321. // }else{
  322. //
  323. // Yii::$app->set('mailer', [
  324. // 'class' => 'yii\swiftmailer\Mailer',
  325. // 'useFileTransport' => false,
  326. // 'transport' => [
  327. // 'class' => 'Swift_SmtpTransport',
  328. // 'host' => $host,
  329. // 'username' => $smtp_username,
  330. // 'password' => $password,
  331. // 'port' => $port,
  332. // 'encryption' => 'ssl',
  333. // ],
  334. // 'messageConfig' => [
  335. // 'charset' => 'UTF-8',
  336. // 'from' => [$smtp_username => 'Vegard']
  337. // ],
  338. // ]);
  339. // }
  340. //
  341. // }
  342. $mailid = self::saveRecord($isSuccess, $to, $subject, $replace_content,$configArr['id']);
  343. //如果是订单信息就开始设置为paycenter的邮箱地址发送
  344. if($subject=="訂單信息"){
  345. $pay_mail_num = intval($config->pay_mail_num);
  346. if($pay_mail_num == 0){
  347. $smtp_username = "admin@fastpaypro.net";
  348. $host = 'hwsmtppay.exmail.qq.com';
  349. $port = '4650';
  350. // 设置新的邮箱地址发送
  351. Yii::$app->set('mailer', [
  352. 'class' => 'yii\swiftmailer\Mailer',
  353. 'useFileTransport' => false,
  354. 'transport' => [
  355. 'class' => 'Swift_SmtpTransport',
  356. 'host' => $host,
  357. 'username' => $smtp_username,
  358. 'password' => 'Fastpak99',
  359. 'port' => $port,
  360. 'encryption' => 'ssl',
  361. ],
  362. 'messageConfig' => [
  363. 'charset' => 'UTF-8',
  364. 'from' => [$smtp_username => 'fastpaypro']
  365. ],
  366. ]);
  367. }else{
  368. $pay_mail = $config->pay_mail;
  369. $smtp_username = "apikey";
  370. $host = 'smtp.sendgrid.net';
  371. $port = '465';
  372. // 设置新的邮箱地址发送
  373. Yii::$app->set('mailer', [
  374. 'class' => 'yii\swiftmailer\Mailer',
  375. 'useFileTransport' => false,
  376. 'transport' => [
  377. 'class' => 'Swift_SmtpTransport',
  378. 'host' => $host,
  379. 'username' => $smtp_username,
  380. 'password' => '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',
  381. 'port' => $port,
  382. 'encryption' => 'ssl',
  383. ],
  384. 'messageConfig' => [
  385. 'charset' => 'UTF-8',
  386. 'from' => [$pay_mail => $pay_mail]
  387. ],
  388. ]);
  389. }
  390. }
  391. $mail = \Yii::$app->mailer->compose();
  392. $mail->setTo($to);
  393. $mail->setSubject($subject);
  394. $mail->setHtmlBody( '<html>'.$replace_content);
  395. $result = $mail->send();
  396. if ($result) {
  397. $isSuccess = true;
  398. } else {
  399. self::sendSimpleMail($subject, $to, $from, $content, $paramArray, $fromName);
  400. Yii::warning('邮件发送失败' . VarDumper::dumpAsString($result), __METHOD__);
  401. }
  402. $status = ($isSuccess) ? 1 : 2;
  403. MailRecord::updateAll(['status' => $status], "id={$mailid}" );
  404. return $isSuccess;
  405. }
  406. /**
  407. * 将模板中的变量替换为入参
  408. * @param $content string
  409. * @param $paramArray array
  410. * @return string
  411. */
  412. public static function replaceVariable($content, $paramArray)
  413. {
  414. if (!empty($content) && !empty($paramArray)) {
  415. foreach ($paramArray as $key => $value) {
  416. $variable='${'.$key.'}';
  417. $variable1 = '#{'.$key.'}';
  418. $variable2 = '#'.$key.'#';
  419. if (is_null($value)) {
  420. continue;
  421. }
  422. while (strpos($content, $variable, 0) > -1) {
  423. $content = str_replace($variable, $value, $content);
  424. }
  425. while (strpos($content, $variable1, 0) > -1) {
  426. $content = str_replace($variable1, $value, $content);
  427. }
  428. while (strpos($content, $variable2, 0) > -1) {
  429. $content = str_replace($variable2, $value, $content);
  430. }
  431. }
  432. }
  433. return $content;
  434. }
  435. /**
  436. * 保存发送的邮件记录到crm_mail_record表
  437. * @param $status
  438. * @param $receiver
  439. * @param $subject
  440. * @param $content
  441. * @return bool
  442. */
  443. private static function saveRecord($status, $receiver, $subject, $content,$num)
  444. {
  445. $mailRecord = new MailRecord();
  446. $mailRecord->content = $content;
  447. $mailRecord->receiver = $receiver;
  448. if ($status) {
  449. $mailRecord->status = 1;
  450. } else {
  451. $mailRecord->status = 0;
  452. }
  453. $mailRecord->subject = $subject;
  454. $mailRecord->in_time = DateTimeHelper::microtime_float();
  455. $mailRecord->mail_num = $num;
  456. $mailRecord->save();
  457. return $mailRecord->id;
  458. }
  459. /**
  460. * 发送email邮件
  461. * @param $subject
  462. * @param $to
  463. * @param $from
  464. * @param $body
  465. * @param $mid
  466. * @return bool
  467. */
  468. public static function sendButNotSaveRecord($subject, $to, $from, $body, $mid)
  469. {
  470. $isSuccess = false;
  471. $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
  472. /*
  473. $smtp_username = $config->smtp_username;
  474. $smtp_password = $config->smtp_password;
  475. \Yii::$app->mail->apiUser = $smtp_username;
  476. \Yii::$app->mail->apiKey = $smtp_password;
  477. $result = \Yii::$app->mail->sendNormalMail($subject, '<html>'.$body, $to, $from);
  478. if ($result['result'] === true) {
  479. $isSuccess = true;
  480. }
  481. return $isSuccess;
  482. */
  483. $mailConfig = new MailConfig();
  484. $configArr = $mailConfig->getMailConfig($to);
  485. if(!$configArr){
  486. $connection=Yii::$app->db;
  487. $time = time() - 300;
  488. //将5分钟内发送给相同收件人和相同主题的发件记录设置为不可发送状态
  489. $sql = "update crm_mail_record set status = 2 where subject = $subject and receiver = $to and in_time > $time";
  490. $command=$connection->createCommand($sql);
  491. $rowCount=$command->execute();
  492. return false;
  493. }else{
  494. Yii::$app->set('mailer', [
  495. 'class' => 'yii\swiftmailer\Mailer',
  496. 'useFileTransport' => false,
  497. 'transport' => [
  498. 'class' => 'Swift_SmtpTransport',
  499. 'host' => $configArr['host'],
  500. 'username' => $configArr['smtp_username'],
  501. 'password' => $configArr['password'],
  502. 'port' => $configArr['port'],
  503. 'encryption' => 'ssl',
  504. ],
  505. 'messageConfig' => [
  506. 'charset' => 'UTF-8',
  507. 'from' => [$configArr['send_mail'] => 'Vegard']
  508. ],
  509. ]);
  510. }
  511. // 配置邮箱的使用
  512. // if($config->mail_number){
  513. // $number = intval($config->mail_number);
  514. // switch ($number){
  515. // case 1:
  516. // $smtp_username = "admin@notify-online.com";
  517. // $host = 'hwsmtp1.exmail.qq.com';
  518. // $password="Notifk99";
  519. // $port = '4650';
  520. // break;
  521. // case 2:
  522. // $smtp_username = "admin@notify2-online.com";
  523. // $host = 'hwsmtp2.exmail.qq.com';
  524. // $password="Notifk99";
  525. // $port = '4650';
  526. // break;
  527. // case 3:
  528. // $smtp_username = "admin@notify3-online.com";
  529. // $host = 'hwsmtp3.exmail.qq.com';
  530. // $password="Notifk99";
  531. // $port = '4650';
  532. // break;
  533. // case 4:
  534. // $smtp_username = "admin@notify4-online.com";
  535. // $host = 'hwsmtp4.exmail.qq.com';
  536. // $password="Notifk99";
  537. // $port = '4650';
  538. // break;
  539. // case 5:
  540. // $smtp_username = "info@vegardmkt.com";
  541. // $host = 'smtp.fastmail.com';
  542. // $password="q7yka9tdhgym9zuy";
  543. // $port = '465';
  544. // break;
  545. // case 6:
  546. // $smtp_username = "info2@vegardmkt.com";
  547. // $host = 'smtp.fastmail.com';
  548. // $password="4emg7ux97qxz4fc7";
  549. // $port = '465';
  550. // break;
  551. // case 7:
  552. // $pay_mail = "adminemail1@vegardmkt.com";
  553. // $smtp_username = "apikey";
  554. // $host = 'smtp.sendgrid.net';
  555. // $password="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";
  556. // $port = '465';
  557. // break;
  558. // case 8:
  559. // $pay_mail = "adminemail2@vegardmkt.com";
  560. // $smtp_username = "apikey";
  561. // $host = 'smtp.sendgrid.net';
  562. // $password="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";
  563. // $port = '465';
  564. // break;
  565. // case 9:
  566. // $pay_mail = "adminemail3@vegardmkt.com";
  567. // $smtp_username = "apikey";
  568. // $host = 'smtp.sendgrid.net';
  569. // $password="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";
  570. // $port = '465';
  571. // break;
  572. // case 10:
  573. // $pay_mail = "adminemail4@vegardmkt.com";
  574. // $smtp_username = "apikey";
  575. // $host = 'smtp.sendgrid.net';
  576. // $password="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";
  577. // $port = '465';
  578. // break;
  579. // default:
  580. // $pay_mail = "admin@vegardmkt.com";
  581. // $smtp_username = "apikey";
  582. // $host = 'smtp.sendgrid.net';
  583. // $password="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";
  584. // $port = '465';
  585. // }
  586. // if($number==999 || $number==7 || $number==8 || $number==9 || $number==10){
  587. // // 设置新的邮箱地址发送
  588. // Yii::$app->set('mailer', [
  589. // 'class' => 'yii\swiftmailer\Mailer',
  590. // 'useFileTransport' => false,
  591. // 'transport' => [
  592. // 'class' => 'Swift_SmtpTransport',
  593. // 'host' => $host,
  594. // 'username' => $smtp_username,
  595. // 'password' => $password,
  596. // 'port' => $port,
  597. // 'encryption' => 'ssl',
  598. // ],
  599. // 'messageConfig' => [
  600. // 'charset' => 'UTF-8',
  601. // 'from' => [$pay_mail => 'Vegard']
  602. // ],
  603. // ]);
  604. // }else{
  605. // // 设置新的邮箱地址发送
  606. // Yii::$app->set('mailer', [
  607. // 'class' => 'yii\swiftmailer\Mailer',
  608. // 'useFileTransport' => false,
  609. // 'transport' => [
  610. // 'class' => 'Swift_SmtpTransport',
  611. // 'host' => $host,
  612. // 'username' => $smtp_username,
  613. // 'password' => $password,
  614. // 'port' => $port,
  615. // 'encryption' => 'ssl',
  616. // ],
  617. // 'messageConfig' => [
  618. // 'charset' => 'UTF-8',
  619. // 'from' => [$smtp_username => 'Vegard']
  620. // ],
  621. // ]);
  622. // }
  623. //
  624. // }
  625. if($subject=="訂單信息"){
  626. $pay_mail_num = intval($config->pay_mail_num);
  627. if($pay_mail_num == 0){
  628. $smtp_username = "admin@fastpaypro.net";
  629. $host = 'hwsmtppay.exmail.qq.com';
  630. $port = '4650';
  631. // 设置新的邮箱地址发送
  632. Yii::$app->set('mailer', [
  633. 'class' => 'yii\swiftmailer\Mailer',
  634. 'useFileTransport' => false,
  635. 'transport' => [
  636. 'class' => 'Swift_SmtpTransport',
  637. 'host' => $host,
  638. 'username' => $smtp_username,
  639. 'password' => 'Fastpak99',
  640. 'port' => $port,
  641. 'encryption' => 'ssl',
  642. ],
  643. 'messageConfig' => [
  644. 'charset' => 'UTF-8',
  645. 'from' => [$smtp_username => 'fastpaypro']
  646. ],
  647. ]);
  648. }else{
  649. $pay_mail = $config->pay_mail;
  650. $smtp_username = "apikey";
  651. $host = 'smtp.sendgrid.net';
  652. $port = '465';
  653. // 设置新的邮箱地址发送
  654. Yii::$app->set('mailer', [
  655. 'class' => 'yii\swiftmailer\Mailer',
  656. 'useFileTransport' => false,
  657. 'transport' => [
  658. 'class' => 'Swift_SmtpTransport',
  659. 'host' => $host,
  660. 'username' => $smtp_username,
  661. 'password' => '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',
  662. 'port' => $port,
  663. 'encryption' => 'ssl',
  664. ],
  665. 'messageConfig' => [
  666. 'charset' => 'UTF-8',
  667. 'from' => [$pay_mail => $pay_mail]
  668. ],
  669. ]);
  670. }
  671. }
  672. $mail = \Yii::$app->mailer->compose();
  673. $mail->setTo($to);
  674. $mail->setSubject($subject);
  675. $mail->setHtmlBody( '<html>'.$body);
  676. $result = $mail->send();
  677. if ($result) {
  678. MailRecord::updateAll(['status' => 1], "id={$mid}" );
  679. return true;
  680. }else{
  681. self::sendButNotSaveRecord($subject, $to, $from, $body, $mid);
  682. Yii::warning('邮件发送失败'.VarDumper::dumpAsString($result),__METHOD__);
  683. }
  684. }
  685. }