/**
  * the singleton pattern
  *
  * @return Expressomail_Controller_Message_Send
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new Expressomail_Controller_Message_Send();
     }
     return self::$_instance;
 }
 /**
  *
  * @param array $source
  * @param resource | string $inputStream
  * @param string $flag
  * @throws Zend_Mail_Protocol_Exception
  */
 public function parseAndSendMessage($source, $inputStream, $flag = NULL)
 {
     $originalMessage = $this->getCompleteMessage($source['itemId'], null, false);
     $user = Tinebase_Core::getUser();
     if (!is_resource($inputStream)) {
         $stream = fopen("php://temp", 'r+');
         fwrite($stream, $inputStream);
         $inputStream = $stream;
         rewind($inputStream);
     }
     $incomingMessage = new Zend_Mail_Message(array('file' => $inputStream));
     $headers = $incomingMessage->getHeaders();
     $body = $headers['content-transfer-encoding'] == 'base64' ? base64_decode($incomingMessage->getContent()) : $incomingMessage->getContent();
     $isTextPlain = strpos($headers['content-type'], 'text/plain');
     $bodyLines = preg_split('/\\r\\n|\\r|\\n/', $body);
     $body = '';
     if ($isTextPlain !== false) {
         foreach ($bodyLines as &$line) {
             $body .= htmlentities($line) . '<br>';
         }
     } else {
         foreach ($bodyLines as &$line) {
             $body .= $line . '<br>';
         }
     }
     $body = '<div>' . $body . '</div>';
     $bodyOrigin = $originalMessage['body'];
     preg_match("/<body[^>]*>(.*?)<\\/body>/is", $bodyOrigin, $matches);
     $bodyOrigin = count($matches) > 1 ? $matches[1] : $bodyOrigin;
     $body .= '<div>' . $bodyOrigin . '</div>';
     $attachments = array();
     foreach ($originalMessage['attachments'] as &$att) {
         try {
             $att['name'] = $att['filename'];
             $att['type'] = $att['content-type'];
         } catch (Exception $e) {
         }
         array_push($attachments, $att);
     }
     $recordData = array();
     $recordData['note'] = '';
     $recordData['content_type'] = 'text/html';
     $recordData['account_id'] = $originalMessage->account_id;
     $recordData['to'] = is_array($headers['to']) ? $headers['to'] : array($headers['to']);
     $recordData['cc'] = array();
     $recordData['bcc'] = array();
     $recordData['subject'] = $headers['subject'];
     $recordData['body'] = $body;
     //$recordData['flags'] = array_merge($incomingMessage->getFlags(), $originalMessage['flags']);
     $recordData['flags'] = $flag != NULL ? $flag : '';
     $recordData['original_id'] = $source['itemId'];
     $recordData['embedded_images'] = array();
     $recordData['attachments'] = $attachments;
     $recordData['from_email'] = $user->accountEmailAddress;
     $recordData['from_name'] = $user->accountFullName;
     $recordData['customfields'] = array();
     $message = new Expressomail_Model_Message();
     $message->setFromJsonInUsersTimezone($recordData);
     try {
         Expressomail_Controller_Message_Send::getInstance()->sendMessage($message);
     } catch (Zend_Mail_Protocol_Exception $zmpe) {
         Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not send message: ' . $zmpe->getMessage());
         throw $zmpe;
     }
 }
 /**
  * test converting to punycode
  */
 public function testEncodeToPunycode()
 {
     $message = new Expressomail_Model_Message(array('to' => array('albert@östermänn.org'), 'subject' => 'punycode test'));
     $mail = Expressomail_Controller_Message_Send::getInstance()->createMailForSending($message, $this->_account);
     $recipients = $mail->getRecipients();
     $this->assertEquals('*****@*****.**', $recipients[0]);
 }
 /**
  * send email
  *
  * @param resource $inputStream
  * @param boolean $saveInSent
  * @throws Syncroton_Exception
  */
 public function sendEmail($inputStream, $saveInSent)
 {
     $defaultAccountId = Tinebase_Core::getPreference('Expressomail')->{Expressomail_Preference::DEFAULTACCOUNT};
     try {
         $account = Expressomail_Controller_Account::getInstance()->get($defaultAccountId);
     } catch (Tinebase_Exception_NotFound $ten) {
         if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) {
             Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . " no email account configured");
         }
         throw new Syncroton_Exception('no email account configured');
     }
     if (empty(Tinebase_Core::getUser()->accountEmailAddress)) {
         throw new Syncroton_Exception('no email address set for current user');
     }
     if (!is_resource($inputStream)) {
         $stream = fopen("php://temp", 'r+');
         fwrite($stream, $inputStream);
         $inputStream = $stream;
         rewind($inputStream);
     }
     if ($this->_debugEmail == true) {
         $debugStream = fopen("php://temp", 'r+');
         stream_copy_to_stream($inputStream, $debugStream);
         rewind($debugStream);
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " email to send:" . stream_get_contents($debugStream));
         }
         //replace original stream wirh debug stream, as php://input can't be rewinded
         $inputStream = $debugStream;
         rewind($inputStream);
     }
     $incomingMessage = new Zend_Mail_Message(array('file' => $inputStream));
     $subject = $incomingMessage->headerExists('subject') ? $incomingMessage->getHeader('subject') : null;
     if (Tinebase_Mail::isiMIPMail($incomingMessage)) {
         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
             Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Do not send iMIP message with subject "' . $subject . '". The server should handle those.');
         }
     } else {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Send Message with subject " . $subject . " (saveInSent: " . $saveInSent . ")");
         }
         $mail = Tinebase_Mail::createFromZMM($incomingMessage);
         Expressomail_Controller_Message_Send::getInstance()->sendZendMail($account, $mail, (bool) $saveInSent);
     }
 }
 /**
  * report message(s) as phishing
  *
  * @param  array $msgIds
  * @param  array $recordData
  * @return array
  */
 public function reportPhishing($msgIds, $recordData)
 {
     $message = new Expressomail_Model_Message();
     $message->setFromJsonInUsersTimezone($recordData);
     $zipFile = Expressomail_Controller_Message::getInstance()->zipMessages($msgIds);
     $message = Expressomail_Controller_Message::getInstance()->parsePhishingNotification($message, $zipFile);
     $message = Expressomail_Controller_Message_Send::getInstance()->sendMessage($message);
     $result = $this->_recordToJson($message);
     return $result;
 }