EmailServer.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/6/11
  6. * Time: 上午9:28
  7. */
  8. namespace common\api;
  9. use Yii;
  10. use yii\log\FileTarget;
  11. class EmailServer {
  12. public function SendOut($emai,$title,$content)
  13. {
  14. $url = Yii::getAlias('@backend') . '/config/email.json';
  15. if (is_file($url))
  16. {
  17. try{
  18. $mail = Yii::$app->mailer->compose();
  19. $mail->setTo($emai);
  20. $mail->setSubject($title);
  21. $mail->setHtmlBody($content);
  22. if ($mail->send() == true){
  23. file_put_contents(Yii::getAlias('@common').'/log/success.email'.date('Y-m-d-H-i-s',$_SERVER['REQUEST_TIME']).'.'.rand(1,10).'.log',$content);
  24. return true;
  25. }
  26. }catch (\Exception $e){
  27. $log = new FileTarget();
  28. $log->logFile = Yii::getAlias('@common').'/log/email'.date('Ymd').'.log';
  29. $log->messages[] = [json_encode($e),1,'application',microtime(true)];
  30. $log->export();
  31. return false;
  32. }
  33. }
  34. $log = new FileTarget();
  35. $log->logFile = Yii::getAlias('@common').'/log/email'.date('Ymd').'.log';
  36. $log->messages[] = ['该文件不存在'.json_encode($url),1,'application',microtime(true)];
  37. $log->export();
  38. return false;
  39. }
  40. }