public function testLegacyLogSupport()
 {
     Log::addEntry("this is my log entry.");
     $le = LogEntry::getByID(1);
     $this->assertEquals($le->getLevel(), Logger::DEBUG);
     $this->assertEquals($le->getLevelName(), 'DEBUG');
     $this->assertEquals($le->getMessage(), 'this is my log entry.');
     /*
      * old format here:
     $l = new Log(LOG_TYPE_EMAILS, true, true);
     $l->write('This is line one.');
     $l->write('This is line two');
     $l->close();
     */
     $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::DEBUG);
     $l->write('This is line one.');
     $l->write('This is line two.');
     $l2 = new GroupLogger('test', Logger::CRITICAL);
     $l2->write('OMG!');
     $l2->close();
     $l->close();
     $le2 = LogEntry::getByID(2);
     $le3 = LogEntry::getByID(3);
     $this->assertEquals($le2->getLevel(), Logger::CRITICAL);
     $this->assertEquals($le3->getLevel(), Logger::DEBUG);
     $this->assertEquals($le3->getMessage(), "This is line one.\nThis is line two.");
     $this->assertEquals($le2->getMessage(), "OMG!");
 }
Exemple #2
0
 /**
  * Sends the email.
  *
  * @param bool $resetData Whether or not to reset the service to its default values.
  *
  * @throws \Exception
  *
  * @return bool
  */
 public function sendMail($resetData = true)
 {
     $_from[] = $this->from;
     $fromStr = $this->generateEmailStrings($_from);
     $toStr = $this->generateEmailStrings($this->to);
     $replyStr = $this->generateEmailStrings($this->replyto);
     $zendMailData = self::getMailerObject();
     $mail = $zendMailData['mail'];
     $transport = $zendMailData['transport'];
     if (is_array($this->from) && count($this->from)) {
         if ($this->from[0] != '') {
             $from = $this->from;
         }
     }
     if (!isset($from)) {
         $from = array(Config::get('concrete.email.default.address'), Config::get('concrete.email.default.name'));
         $fromStr = Config::get('concrete.email.default.address');
     }
     // The currently included Zend library has a bug in setReplyTo that
     // adds the Reply-To address as a recipient of the email. We must
     // set the Reply-To before any header with addresses and then clear
     // all recipients so that a copy is not sent to the Reply-To address.
     if (is_array($this->replyto)) {
         foreach ($this->replyto as $reply) {
             $mail->setReplyTo($reply[0], $reply[1]);
         }
     }
     $mail->setFrom($from[0], $from[1]);
     $mail->setSubject($this->subject);
     foreach ($this->to as $to) {
         $mail->addTo($to[0], $to[1]);
     }
     if (is_array($this->cc) && count($this->cc)) {
         foreach ($this->cc as $cc) {
             $mail->addCc($cc[0], $cc[1]);
         }
     }
     if (is_array($this->bcc) && count($this->bcc)) {
         foreach ($this->bcc as $bcc) {
             $mail->addBcc($bcc[0], $bcc[1]);
         }
     }
     $headers = $mail->getHeaders();
     if ($headers->has('messageid')) {
         $messageIdHeader = $headers->get('messageid');
     } else {
         $messageIdHeader = new \Zend\Mail\Header\MessageId();
         $headers->addHeader($messageIdHeader);
     }
     $headers->addHeaders($this->headers);
     $messageIdHeader->setId();
     $body = new MimeMessage();
     if ($this->body !== false && $this->bodyHTML !== false) {
         $alternatives = new MimeMessage();
         $text = new MimePart($this->body);
         $text->type = 'text/plain';
         $text->charset = APP_CHARSET;
         $alternatives->addPart($text);
         $html = new MimePart($this->bodyHTML);
         $html->type = 'text/html';
         $html->charset = APP_CHARSET;
         $alternatives->addPart($html);
         $alternativesPath = new MimePart($alternatives->generateMessage());
         $alternativesPath->type = 'multipart/alternative;' . Mime::LINEEND . ' boundary="' . $alternatives->getMime()->boundary() . '"';
         $body->addPart($alternativesPath);
     } elseif ($this->body !== false) {
         $text = new MimePart($this->body);
         $text->type = 'text/plain';
         $text->charset = APP_CHARSET;
         $body->addPart($text);
     } elseif ($this->bodyHTML !== false) {
         $html = new MimePart($this->bodyHTML);
         $html->type = 'text/html';
         $html->charset = APP_CHARSET;
         $body->addPart($html);
     }
     foreach ($this->attachments as $att) {
         $body->addPart($att);
     }
     if (count($body->getParts()) === 0) {
         $text = new MimePart('');
         $text->type = 'text/plain';
         $text->charset = APP_CHARSET;
         $body->addPart($text);
     }
     $mail->setBody($body);
     $sent = false;
     try {
         if (Config::get('concrete.email.enabled')) {
             $transport->send($mail);
         }
         $sent = true;
     } catch (Exception $e) {
         if ($this->getTesting()) {
             throw $e;
         }
         $l = new GroupLogger(LOG_TYPE_EXCEPTIONS, Logger::CRITICAL);
         $l->write(t('Mail Exception Occurred. Unable to send mail: ') . $e->getMessage());
         $l->write($e->getTraceAsString());
         if (Config::get('concrete.log.emails')) {
             $l->write(t('Template Used') . ': ' . $this->template);
             $l->write(t('To') . ': ' . $toStr);
             $l->write(t('From') . ': ' . $fromStr);
             if (isset($this->replyto)) {
                 $l->write(t('Reply-To') . ': ' . $replyStr);
             }
             $l->write(t('Subject') . ': ' . $this->subject);
             $l->write(t('Body') . ': ' . $this->body);
         }
         $l->close();
     }
     // add email to log
     if (Config::get('concrete.log.emails') && !$this->getTesting()) {
         $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::INFO);
         if (Config::get('concrete.email.enabled')) {
             $l->write('**' . t('EMAILS ARE ENABLED. THIS EMAIL WAS SENT TO mail()') . '**');
         } else {
             $l->write('**' . t('EMAILS ARE DISABLED. THIS EMAIL WAS LOGGED BUT NOT SENT') . '**');
         }
         $l->write(t('Template Used') . ': ' . $this->template);
         $l->write(t('Mail Details: %s', $mail->toString()));
         $l->close();
     }
     // clear data if applicable
     if ($resetData) {
         $this->reset();
     }
     return $sent;
 }
 /**
  * Sends the email
  * @return void
  */
 public function sendMail($resetData = true)
 {
     $_from[] = $this->from;
     $fromStr = $this->generateEmailStrings($_from);
     $toStr = $this->generateEmailStrings($this->to);
     $replyStr = $this->generateEmailStrings($this->replyto);
     if (Config::get('concrete.email.enabled')) {
         $zendMailData = self::getMailerObject();
         $mail = $zendMailData['mail'];
         $transport = $zendMailData['transport'];
         if (is_array($this->from) && count($this->from)) {
             if ($this->from[0] != '') {
                 $from = $this->from;
             }
         }
         if (!isset($from)) {
             $from = array(Config::get('concrete.email.default.address'), Config::get('concrete.email.name'));
             $fromStr = Config::get('concrete.email.default.address');
         }
         // The currently included Zend library has a bug in setReplyTo that
         // adds the Reply-To address as a recipient of the email. We must
         // set the Reply-To before any header with addresses and then clear
         // all recipients so that a copy is not sent to the Reply-To address.
         if (is_array($this->replyto)) {
             foreach ($this->replyto as $reply) {
                 $mail->setReplyTo($reply[0], $reply[1]);
             }
         }
         $mail->setFrom($from[0], $from[1]);
         $mail->setSubject($this->subject);
         foreach ($this->to as $to) {
             $mail->addTo($to[0], $to[1]);
         }
         if (is_array($this->cc) && count($this->cc)) {
             foreach ($this->cc as $cc) {
                 $mail->addCc($cc[0], $cc[1]);
             }
         }
         if (is_array($this->bcc) && count($this->bcc)) {
             foreach ($this->bcc as $bcc) {
                 $mail->addBcc($bcc[0], $bcc[1]);
             }
         }
         if (is_array($this->attachments) && count($this->attachments)) {
             foreach ($this->attachments as $att) {
                 $natt = $mail->createAttachment($att->contents);
                 $fob = $att->object;
                 unset($att->object);
                 unset($att->contents);
                 foreach ((array) $att as $key => $value) {
                     $natt->{$key} = $value;
                 }
             }
         }
         $text = new MimePart($this->body);
         $text->type = "text/plain";
         $body = new MimeMessage();
         $body->setParts(array($text));
         if ($this->bodyHTML != false) {
             $html = new MimePart($this->bodyHTML);
             $html->type = "text/html";
             $body->addPart($html);
         }
         $mail->setBody($body);
         try {
             $transport->send($mail);
         } catch (Exception $e) {
             if ($this->getTesting()) {
                 throw $e;
             }
             $l = new GroupLogger(LOG_TYPE_EXCEPTIONS, Logger::CRITICAL);
             $l->write(t('Mail Exception Occurred. Unable to send mail: ') . $e->getMessage());
             $l->write($e->getTraceAsString());
             if (Config::get('concrete.log.emails')) {
                 $l->write(t('Template Used') . ': ' . $this->template);
                 $l->write(t('To') . ': ' . $toStr);
                 $l->write(t('From') . ': ' . $fromStr);
                 if (isset($this->replyto)) {
                     $l->write(t('Reply-To') . ': ' . $replyStr);
                 }
                 $l->write(t('Subject') . ': ' . $this->subject);
                 $l->write(t('Body') . ': ' . $this->body);
             }
             $l->close();
         }
     }
     // add email to log
     if (Config::get('concrete.log.emails') && !$this->getTesting()) {
         $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::INFO);
         if (Config::get('concrete.email.enabled')) {
             $l->write('**' . t('EMAILS ARE ENABLED. THIS EMAIL WAS SENT TO mail()') . '**');
         } else {
             $l->write('**' . t('EMAILS ARE DISABLED. THIS EMAIL WAS LOGGED BUT NOT SENT') . '**');
         }
         $l->write(t('Template Used') . ': ' . $this->template);
         $l->write(t('Mail Details: %s', $mail->toString()));
         $l->close();
     }
     // clear data if applicable
     if ($resetData) {
         $this->reset();
     }
 }