mail.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. require_once(dirname(__FILE__).'/vendor/PHPMailer/class.phpmailer.php');
  3. require_once(dirname(__FILE__).'/vendor/PHPMailer/class.smtp.php');
  4. $smtpConfDefault = array('host' => "mail.global-cube.com", 'secure' => true, 'auth' => true, 'port' => 465, 'from' => "versand@global-cube.com", 'username' => "versand", 'password' => "y6!avXX3tQvr");
  5. $config = parse_ini_file(dirname(__FILE__) . "/../../GAPS.ini");
  6. $smtpConfXml = getSmtpConfFromIni($config);
  7. function checkProtocolDirectory($path, $all = false)
  8. {
  9. $result = array();
  10. $protDir = dir($path);
  11. while ($filename = $protDir->read()) {
  12. if (!preg_match('/\.log$/i', $filename))
  13. continue;
  14. $log = new Logfile("{$path}\\{$filename}");
  15. if ($all || $log->ErrorLevel < 3 || $log->LastChangedDays >= 4) {
  16. $result[] = $log;
  17. }
  18. }
  19. return $result;
  20. }
  21. function getSmtpConfFromIni($config) {
  22. $conf = array();
  23. $conf['host'] = (@$config['SMTP_FEHLERBERICHT'] != "N") ? $config['SMTP_HOST'] : "";
  24. $conf['port'] = $config['SMTP_PORT'];
  25. $conf['from'] = $config['SMTP_FROM'];
  26. $conf['username'] = $config['SMTP_USER'];
  27. $conf['password'] = $config['SMTP_PW'];
  28. $conf['secure'] = ($config['SMTP_SSL'] == "J");
  29. $conf['auth'] = ($conf['username'] != "");
  30. return $conf;
  31. }
  32. /**
  33. * @param $mail PHPMailer
  34. * @param $conf array
  35. * @param $mailFrom string
  36. * @return bool
  37. */
  38. function setSmtpConf($mail, $conf, $mailFrom) {
  39. $mail->IsSMTP();
  40. $mail->Host = $conf['host'];
  41. $mail->SMTPAuth = $conf['auth'];
  42. $mail->Port = $conf['port'];
  43. $mail->Username = $conf['username'];
  44. $mail->Password = $conf['password'];
  45. $mail->SMTPSecure = ($conf['secure']) ? "ssl" : false;
  46. $mail->Sender = '';
  47. $mail->SetFrom($conf['from'], $mailFrom);
  48. //$mail->SMTPDebug = true;
  49. return true;
  50. }
  51. function mailFormat($list) {
  52. $result = "";
  53. $addresses = explode(";", $list);
  54. foreach ($addresses as $address) {
  55. $result .= "<a href=\"mailto:{$address}\">{$address}</a><br/>";
  56. }
  57. return $result;
  58. }
  59. function sendMail($logs, $mailFrom)
  60. {
  61. $today = date("d.m.Y");
  62. $total = count($logs);
  63. ob_start();
  64. include('fehlerbericht_template.php');
  65. $body = ob_get_clean();
  66. $mail = new PHPMailer();
  67. $mail->AddAddress('fehlerbericht@global-cube.de', 'Global Cube');
  68. $mail->Subject = "{$mailFrom}: Fehlerbericht vom {$today}, {$total} Fehler";
  69. $mail->AltBody = json_encode($logs);
  70. $mail->MsgHTML($body);
  71. foreach ($logs as $logfile) {
  72. if ($logfile->Type == "Modell") {
  73. $mail->AddAttachment($logfile->Filename);
  74. }
  75. }
  76. if (send($mail, $mailFrom)) {
  77. echo " Fehlerbericht gesendet!\r\n";
  78. }
  79. }
  80. /**
  81. * @param $mail PHPMailer
  82. * @param $mailFrom string
  83. * @return bool
  84. */
  85. function send ($mail, $mailFrom) {
  86. global $smtpConfDefault;
  87. global $smtpConfXml;
  88. setSmtpConf($mail, $smtpConfDefault, $mailFrom);
  89. if ($mail->Send()) {
  90. $mail->SmtpClose();
  91. return true;
  92. }
  93. $mail->SmtpClose();
  94. echo " Fehler im Mailversand: " . $mail->ErrorInfo . "\r\n";
  95. return false;
  96. }
  97. function sendStatusMail($mailFrom, $startTime, $endTime, $errorCount, $batchFile, $attachments) {
  98. global $config;
  99. $mail = new PHPMailer();
  100. $mail->AddAddress('status@global-cube.com', 'Global Cube');
  101. $today = date("Y-m-d");
  102. $content = array('subject' => "{$mailFrom};{$startTime};{$endTime};{$today};{$errorCount};{$batchFile}");
  103. $mail->Subject = $content['subject'];
  104. $mail->isHTML(false);
  105. $mail->Body = "[]";
  106. foreach($attachments as $key => $file) {
  107. if (file_exists($file)) {
  108. $content[$key] = file_get_contents($file);
  109. $mail->AddAttachment($file);
  110. }
  111. }
  112. if (send($mail, $mailFrom)) {
  113. echo " Statusbericht gesendet!\r\n";
  114. } else {
  115. $opts = array('http' =>
  116. array(
  117. 'method' => 'POST',
  118. 'header' => "Content-type: application/json\r\n",
  119. 'content' => json_encode($content),
  120. 'timeout' => 60
  121. )
  122. );
  123. if (isset($config['PROXY'])) {
  124. $opt['http']['proxy'] = $config['PROXY'];
  125. $opt['http']['request_fulluri'] = true;
  126. }
  127. $context = stream_context_create($opts);
  128. $result = file_get_contents('http://dev.global-cube.com/statusmail/', false, $context, -1, 40000);
  129. echo " " . $result;
  130. }
  131. return true;
  132. }
  133. function checkStarterLogs($path, $dirMatch)
  134. {
  135. $today = date("d.m.Y");
  136. $result = array();
  137. $protDir = dir($path);
  138. while ($filename = $protDir->read()) {
  139. if (!preg_match('/\.' . $dirMatch . '\.log$/i', $filename))
  140. continue;
  141. foreach (file("{$path}\\{$filename}") as $line) {
  142. if (strpos($line, $today)) {
  143. $result[] = json_decode($line);
  144. }
  145. }
  146. }
  147. return $result;
  148. }
  149. function checkStarter($path)
  150. {
  151. $result = array();
  152. $logPath = $path . "\\logs";
  153. $protDir = dir($logPath);
  154. while ($filename = $protDir->read()) {
  155. if (!preg_match('/\.log$/i', $filename) || $filename == "gcstarter.log") {
  156. continue;
  157. }
  158. $result[$filename] = json_decode("[" . implode(",", file("{$logPath}\\{$filename}")) . "]");
  159. }
  160. $result['gcstarter.xml'] = file_get_contents($path . "\\config\gcstarter.xml");
  161. return $result;
  162. }
  163. function sendUpdateMail($logs)
  164. {
  165. global $smtpConfDefault;
  166. global $smtpConfXml;
  167. global $config;
  168. $today = date("d.m.Y");
  169. ob_start();
  170. include('statusbericht_template.php');
  171. $body = ob_get_clean();
  172. $mail = new PHPMailer();
  173. $mail->AddAddress($config['STATUSBERICHT']);
  174. $mail->Subject = "{$config['KUNDE']} - Kontenaktualisierung GAPS";
  175. $mail->MsgHTML($body);
  176. if (send($mail, "Global Cube")) {
  177. echo " Updatebericht gesendet!\r\n";
  178. return true;
  179. }
  180. return false;
  181. }