mail.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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' => "smtp.1und1.de", 'secure' => true, 'auth' => true, 'port' => 465, 'from' => "smtp@global-cube.de", 'username' => "smtp@global-cube.de", 'password' => "gcbssmtp");
  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. sleep(10);
  95. $mail->Port = 25;
  96. $mail->SMTPSecure = false;
  97. if ($mail->Send()) {
  98. $mail->SmtpClose();
  99. return true;
  100. }
  101. $mail->SmtpClose();
  102. sleep(10);
  103. if ($smtpConfXml['host']!="") {
  104. setSmtpConf($mail, $smtpConfXml, $mailFrom);
  105. if ($mail->Send()) {
  106. $mail->SmtpClose();
  107. return true;
  108. }
  109. $mail->SmtpClose();
  110. }
  111. echo " Fehler im Mailversand: " . $mail->ErrorInfo . "\r\n";
  112. return false;
  113. }
  114. function sendStatusMail($mailFrom, $startTime, $endTime, $errorCount, $batchFile, $attachments) {
  115. global $config;
  116. $mail = new PHPMailer();
  117. $mail->AddAddress('status@global-cube.de', 'Global Cube');
  118. $today = date("Y-m-d");
  119. $content = array('subject' => "{$mailFrom};{$startTime};{$endTime};{$today};{$errorCount};{$batchFile}");
  120. $mail->Subject = $content['subject'];
  121. $mail->isHTML(false);
  122. $mail->Body = "[]";
  123. foreach($attachments as $key => $file) {
  124. if (file_exists($file)) {
  125. $content[$key] = file_get_contents($file);
  126. $mail->AddAttachment($file);
  127. }
  128. }
  129. if (send($mail, $mailFrom)) {
  130. echo " Statusbericht gesendet!\r\n";
  131. } else {
  132. $opts = array('http' =>
  133. array(
  134. 'method' => 'POST',
  135. 'header' => "Content-type: application/json\r\n",
  136. 'content' => json_encode($content),
  137. 'timeout' => 60
  138. )
  139. );
  140. if (isset($config['PROXY'])) {
  141. $opt['http']['proxy'] = $config['PROXY'];
  142. $opt['http']['request_fulluri'] = true;
  143. }
  144. $context = stream_context_create($opts);
  145. $result = file_get_contents('http://dev.global-cube.de/statusmail/', false, $context, -1, 40000);
  146. echo " " . $result;
  147. }
  148. return true;
  149. }
  150. function checkStarterLogs($path, $dirMatch)
  151. {
  152. $today = date("d.m.Y");
  153. $result = array();
  154. $protDir = dir($path);
  155. while ($filename = $protDir->read()) {
  156. if (!preg_match('/\.' . $dirMatch . '\.log$/i', $filename))
  157. continue;
  158. foreach (file("{$path}\\{$filename}") as $line) {
  159. if (strpos($line, $today)) {
  160. $result[] = json_decode($line);
  161. }
  162. }
  163. }
  164. return $result;
  165. }
  166. function checkStarter($path)
  167. {
  168. $result = array();
  169. $logPath = $path . "\\logs";
  170. $protDir = dir($logPath);
  171. while ($filename = $protDir->read()) {
  172. if (!preg_match('/\.log$/i', $filename) || $filename == "gcstarter.log") {
  173. continue;
  174. }
  175. $result[$filename] = json_decode("[" . implode(",", file("{$logPath}\\{$filename}")) . "]");
  176. }
  177. $result['gcstarter.xml'] = file_get_contents($path . "\\config\gcstarter.xml");
  178. return $result;
  179. }
  180. function sendUpdateMail($logs)
  181. {
  182. global $smtpConfDefault;
  183. global $smtpConfXml;
  184. global $config;
  185. $today = date("d.m.Y");
  186. ob_start();
  187. include('statusbericht_template.php');
  188. $body = ob_get_clean();
  189. $mail = new PHPMailer();
  190. $mail->AddAddress($config['STATUSBERICHT']);
  191. $mail->Subject = "{$config['KUNDE']} - Kontenaktualisierung GAPS";
  192. $mail->MsgHTML($body);
  193. if (send($mail, "Global Cube")) {
  194. echo " Updatebericht gesendet!\r\n";
  195. return true;
  196. }
  197. return false;
  198. }