MailController.php 6.5 KB

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