MailController.php 6.7 KB

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