MailController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. require_once(dirname(__FILE__).'/../models/Logfile.php');
  3. require_once(dirname(__FILE__).'/../models/MailConfig.php');
  4. require_once(dirname(__FILE__).'/../vendor/PHPMailer/class.phpmailer.php');
  5. require_once(dirname(__FILE__).'/../vendor/PHPMailer/class.smtp.php');
  6. // require_once(dirname(__FILE__).'/TemplateController.php');
  7. class MailController
  8. {
  9. private $config;
  10. /** @var MailConfig */
  11. private $smtpConfDefault;
  12. /** @var MailConfig */
  13. private $smtpConfXml;
  14. /** @var string */
  15. private $mailError;
  16. function __construct($config = array())
  17. {
  18. $this->config = $config;
  19. $this->smtpConfDefault = new MailConfig();
  20. $this->smtpConfXml = (isset($config['SMTP_HOST']) && $config['SMTP_HOST'] != "")
  21. ? $this->getSmtpConfFromIni($config)
  22. : $this->smtpConfDefault;
  23. }
  24. public function checkDirAndSendMail($logDir, $customer)
  25. {
  26. $logs = $this->checkProtocolDirectory($logDir);
  27. if (count($logs) > 0) {
  28. $this->sendErrorMail($logs, $customer);
  29. }
  30. }
  31. function checkProtocolDirectory($path)
  32. {
  33. $result = array();
  34. $logDir = dir($path);
  35. while ($filename = $logDir->read()) {
  36. if (!preg_match('/\.log$/i', $filename))
  37. continue;
  38. $log = new Logfile("{$path}\\{$filename}");
  39. if ($log->ErrorLevel < 3 || $log->LastChangedDays >= 4) {
  40. $result[] = $log;
  41. }
  42. }
  43. return $result;
  44. }
  45. function checkProtocolFile ($protocolFile)
  46. {
  47. $log = new Logfile($protocolFile);
  48. if ($log->ErrorLevel < 3) {
  49. $result = array("");
  50. foreach ($log->Errors as $error) {
  51. $result[] = "{$error->Number}: ({$error->Level}) {$error->Message}";
  52. }
  53. return implode("\r\n", $result);
  54. }
  55. return "";
  56. }
  57. public function checkStarterLogsAndSendStatusMail($logDir, $mailTo)
  58. {
  59. $logs = $this->checkStarterLogs($logDir);
  60. if (count($logs) > 0) {
  61. $this->sendStatusMail($logs, $mailTo);
  62. }
  63. }
  64. function checkStarterLogs($path)
  65. {
  66. $today = date("d.m.Y");
  67. $result = array();
  68. $protDir = dir($path);
  69. while ($filename = $protDir->read()) {
  70. if (!preg_match('/\.csv\.log$/i', $filename))
  71. continue;
  72. foreach (file("{$path}\\{$filename}") as $line) {
  73. if (strpos($line, $today)) {
  74. $result[] = json_decode($line);
  75. }
  76. }
  77. }
  78. return $result;
  79. }
  80. function getSmtpConfFromIni ($config) {
  81. $conf = new MailConfig();
  82. $conf->Host = $config['SMTP_HOST'];
  83. $conf->Port = $config['SMTP_PORT'];
  84. $conf->MailFrom = $config['SMTP_FROM'];
  85. $conf->Username = $config['SMTP_USER'];
  86. $conf->Password = $config['SMTP_PW'];
  87. $conf->Secure = ($config['SMTP_SSL'] == "J");
  88. $conf->Auth = ($conf->Username != "");
  89. return $conf;
  90. }
  91. /**
  92. * @param PHPMailer $mail
  93. * @param MailConfig $conf
  94. * @param string $mailFrom
  95. * @return bool
  96. */
  97. function setSmtpConf($mail, $conf) {
  98. $mail->IsSMTP();
  99. $mail->Host = $conf->Host;
  100. $mail->SMTPAuth = $conf->Auth;
  101. $mail->SMTPSecure = ($conf->Secure) ? "ssl" : false;
  102. $mail->Port = $conf->Port;
  103. $mail->Username = $conf->Username;
  104. $mail->Password = $conf->Password;
  105. $mail->Sender = '';
  106. $mail->SetFrom($conf->MailFrom);
  107. //$mail->SMTPDebug = true;
  108. return true;
  109. }
  110. function sendErrorMail($logs, $mailFrom)
  111. {
  112. $body = $this->getMailBody($logs, $mailFrom, "fehlerbericht");
  113. $today = date("d.m.Y");
  114. $total = count($logs);
  115. $subject = "{$mailFrom}: Fehlerbericht vom {$today}, {$total} Fehler";
  116. $mailTo = "fehlerbericht@global-cube.de";
  117. $attachments = array();
  118. foreach ($logs as $logfile) {
  119. if ($logfile->Type == "Modell") {
  120. $attachments[] = $logfile->Filename;
  121. }
  122. }
  123. return $this->sendMail($mailTo, $subject, $body, $mailFrom, $attachments);
  124. }
  125. function sendStatusMail($logs, $mailTo)
  126. {
  127. $body = $this->getMailBody($logs, $mailTo, "statusbericht");
  128. $mailFrom = "Global Cube Business Solutions";
  129. $subject = "Statusbericht GAPS";
  130. return $this->sendMail($mailTo, $subject, $body, $mailFrom);
  131. }
  132. /**
  133. * @param array $logs
  134. * @param string $customer
  135. * @param string $template
  136. * @return string
  137. */
  138. public function getMailBody($logs, $customer, $template)
  139. {
  140. $tpl = new RainTPL;
  141. $tpl->assign("logs", $logs);
  142. $tpl->assign("customer", $customer);
  143. $today = date("d.m.Y");
  144. $tpl->assign("today", $today);
  145. $tpl->assign("list", array("neu" => "Neue Konten", "akt" => "Aktualisierte Konten", "entf" => "Gel&ouml;schte Konten"));
  146. $body = $tpl->draw($template, true);
  147. return $body;
  148. }
  149. public function sendFiles ($csvfile)
  150. {
  151. $subject = basename($csvfile);
  152. $file = file($csvfile);
  153. array_shift($file); // Headline
  154. foreach ($file as $row) {
  155. $col = explode(";", $row);
  156. if (count($col) < 2 || $col[1] == "\r\n") continue;
  157. $attachment = realpath($this->config['PORTAL'] . "\\" . $col[0]);
  158. $body = basename($attachment);
  159. $this->sendMail($col[1], $subject, $body, "Global Cube", array($attachment));
  160. }
  161. }
  162. /**
  163. * @param string $mailTo
  164. * @param string $subject
  165. * @param string $body
  166. * @param string $mailFrom
  167. * @param array $attachments
  168. * @return bool
  169. */
  170. public function sendMail($mailTo, $subject, $body, $mailFrom, $attachments = array())
  171. {
  172. $mail = new PHPMailer();
  173. $mail->ClearAllRecipients();
  174. $mail->AddAddress($mailTo, $mailTo);
  175. $mail->Subject = $subject;
  176. $mail->MsgHTML($body);
  177. foreach ($attachments as $file) {
  178. if (is_array($file)) {
  179. $mail->AddAttachment($file[0], $file[1]);
  180. } else {
  181. $mail->AddAttachment($file);
  182. }
  183. }
  184. $this->mailError = " Mail gesendet!";
  185. $this->setSmtpConf($mail, $this->smtpConfXml);
  186. $res = $mail->Send();
  187. $mail->SmtpClose();
  188. echo $mail->ErrorInfo;
  189. if ($res) {
  190. return true;
  191. }
  192. if (false) {
  193. sleep(10);
  194. $this->setSmtpConf($mail, $this->smtpConfDefault, $mailFrom);
  195. $res = $mail->Send();
  196. $mail->SmtpClose();
  197. if ($res) {
  198. return true;
  199. }
  200. sleep(10);
  201. $mail->Port = 25;
  202. $res = $mail->Send();
  203. $mail->SmtpClose();
  204. if ($res) {
  205. return true;
  206. }
  207. }
  208. $this->mailError = " Fehler im Mailversand: " . $mail->ErrorInfo;
  209. return false;
  210. }
  211. }
  212. ?>