clear() public method

Clear the log contents.
public clear ( )
Ejemplo n.º 1
0
 public function testLogCanBeCleared()
 {
     $logger = new Swift_Plugins_Loggers_ArrayLogger();
     $logger->add(">> FOO\r\n");
     $logger->add("<< 502 That makes no sense\r\n");
     $logger->add(">> RSET\r\n");
     $logger->add("<< 250 OK\r\n");
     $this->assertEqual(">> FOO\r\n" . PHP_EOL . "<< 502 That makes no sense\r\n" . PHP_EOL . ">> RSET\r\n" . PHP_EOL . "<< 250 OK\r\n", $logger->dump());
     $logger->clear();
     $this->assertEqual('', $logger->dump());
 }
Ejemplo n.º 2
0
 /**
  * Log exception
  *
  * @param \Exception|string $error
  */
 protected function logError($error)
 {
     if ($error instanceof \Exception) {
         $error = $error->getMessage();
         $this->fatal = true;
     }
     $logDump = $this->logger->dump();
     if (!empty($logDump) && strpos($error, $logDump) === false) {
         $error .= " Log data: {$logDump}";
     }
     $this->errors[] = $error;
     $this->logger->clear();
     $this->factory->getLogger()->log('error', '[MAIL ERROR] ' . $error);
 }
Ejemplo n.º 3
0
 /**
  * Log exception
  *
  * @param \Exception|string $error
  */
 private function logError($error)
 {
     if ($error instanceof \Exception) {
         $error = $error->getMessage();
         $this->fatal = true;
     }
     $this->errors[] = $error;
     $logDump = $this->logger->dump();
     if (!empty($logDump)) {
         $error .= "; {$logDump}";
         $this->logger->clear();
     }
     $this->factory->getLogger()->log('error', '[MAIL ERROR] ' . $error);
 }
Ejemplo n.º 4
0
 function sendMail($smtp_server, $to, $from, $subject, $body, $cc, $bcc, $attachments = null, $smtp_port = 25, $smtp_username = null, $smtp_password = '', $type = 'text/plain', $transport = 0, $message_id = null, $in_reply_to = null, $inline_images = null, &$complete_mail, $att_version)
 {
     //Load in the files we'll need
     Env::useLibrary('swift');
     try {
         $mail_transport = Swift_SmtpTransport::newInstance($smtp_server, $smtp_port, $transport);
         $smtp_authenticate = $smtp_username != null;
         if ($smtp_authenticate) {
             $mail_transport->setUsername($smtp_username);
             $mail_transport->setPassword(self::ENCRYPT_DECRYPT($smtp_password));
         }
         $mailer = Swift_Mailer::newInstance($mail_transport);
         // init Swift logger
         if (defined('LOG_SWIFT') && LOG_SWIFT > 0) {
             $swift_logger = new Swift_Plugins_Loggers_ArrayLogger();
             $mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($swift_logger));
             $swift_logger_level = LOG_SWIFT;
             // 0: no log, 1: log only errors, 2: log everything
         } else {
             $swift_logger_level = 0;
         }
         if (is_string($from)) {
             $pos = strrpos($from, "<");
             if ($pos !== false) {
                 $sender_name = trim(substr($from, 0, $pos));
                 $sender_address = str_replace(array("<", ">"), array("", ""), trim(substr($from, $pos, strlen($from) - 1)));
             } else {
                 $sender_name = "";
                 $sender_address = $from;
             }
             $from = array($sender_address => $sender_name);
         }
         //Create a message
         $message = Swift_Message::newInstance($subject)->setFrom($from)->setContentType($type);
         $to = self::prepareEmailAddresses($to);
         $cc = self::prepareEmailAddresses($cc);
         $bcc = self::prepareEmailAddresses($bcc);
         foreach ($to as $address) {
             $message->addTo(array_var($address, 0), array_var($address, 1, ""));
         }
         foreach ($cc as $address) {
             $message->addCc(array_var($address, 0), array_var($address, 1, ""));
         }
         foreach ($bcc as $address) {
             $message->addBcc(array_var($address, 0), array_var($address, 1, ""));
         }
         if ($in_reply_to) {
             if (str_starts_with($in_reply_to, "<")) {
                 $in_reply_to = substr($in_reply_to, 1, -1);
             }
             $validator = new SwiftHeaderValidator();
             if ($validator->validate_id_header_value($in_reply_to)) {
                 $message->getHeaders()->addIdHeader("In-Reply-To", $in_reply_to);
             }
         }
         if ($message_id) {
             if (str_starts_with($message_id, "<")) {
                 $message_id = substr($message_id, 1, -1);
             }
             $message->setId($message_id);
         }
         // add attachments
         if (is_array($attachments)) {
             foreach ($attachments as $att) {
                 if ($att_version < 2) {
                     $swift_att = Swift_Attachment::newInstance($att["data"], $att["name"], $att["type"]);
                 } else {
                     $swift_att = Swift_Attachment::fromPath($att['path'], $att['type']);
                     $swift_att->setFilename($att["name"]);
                 }
                 if (substr($att['name'], -4) == '.eml') {
                     $swift_att->setEncoder(Swift_Encoding::get7BitEncoding());
                     $swift_att->setContentType('message/rfc822');
                 }
                 $message->attach($swift_att);
             }
         }
         // add inline images
         if (is_array($inline_images)) {
             foreach ($inline_images as $image_url => $image_path) {
                 $cid = $message->embed(Swift_Image::fromPath($image_path));
                 $body = str_replace($image_url, $cid, $body);
             }
         }
         self::adjustBody($message, $type, $body);
         $message->setBody($body);
         //Send the message
         $complete_mail = self::retrieve_original_mail_code($message);
         $result = $mailer->send($message);
         if ($swift_logger_level >= 2 || $swift_logger_level > 0 && !$result) {
             file_put_contents(CACHE_DIR . "/swift_log.txt", "\n" . gmdate("Y-m-d H:i:s") . " DEBUG:\n" . $swift_logger->dump() . "----------------------------------------------------------------------------", FILE_APPEND);
             $swift_logger->clear();
         }
         return $result;
     } catch (Exception $e) {
         Logger::log("ERROR SENDING EMAIL: " . $e->getTraceAsString(), Logger::ERROR);
         //if there is an error with the connection, let the user know about it
         $mail_error = $e->getMessage();
         $mail_error = stristr($mail_error, 'Log data:', true);
         flash_error(lang('mail not sent') . " '" . $mail_error . "'");
         if ($swift_logger_level > 0) {
             $dump = $swift_logger->dump();
             if ($dump != '') {
                 file_put_contents(CACHE_DIR . "/swift_log.txt", "\n" . gmdate("Y-m-d H:i:s") . " DEBUG:\n" . $dump . "----------------------------------------------------------------------------", FILE_APPEND);
                 $swift_logger->clear();
             }
         }
         throw $e;
     }
 }