Пример #1
0
 /**
  *
  * @todo Save to sent items should be implemented as a Swift outputstream for better memory management
  * @param type $params
  * @return boolean
  */
 protected function actionSend($params)
 {
     //HARDCODE FIX FOR RV ERROR
     $params['forward_mailbox'] = "";
     $params['forward_uid'] = 0;
     GO::session()->closeWriting();
     $response['success'] = true;
     $response['feedback'] = '';
     $alias = \GO\Email\Model\Alias::model()->findByPk($params['alias_id']);
     $account = Account::model()->findByPk($alias->account_id);
     $message = new \GO\Base\Mail\SmimeMessage();
     $tag = $this->_createAutoLinkTagFromParams($params, $account);
     if (!empty($tag)) {
         if ($params['content_type'] == 'html') {
             $params['htmlbody'] .= '<div style="display:none">' . $tag . '</div>';
         } else {
             $params['plainbody'] .= "\n\n" . $tag . "\n\n";
         }
     }
     $message->handleEmailFormInput($params);
     $recipientCount = $message->countRecipients();
     if (!$recipientCount) {
         throw new \Exception(GO::t('feedbackNoReciepent', 'email'));
     }
     $message->setFrom($alias->email, $alias->name);
     $mailer = \GO\Base\Mail\Mailer::newGoInstance(\GO\Email\Transport::newGoInstance($account));
     $logger = new \Swift_Plugins_Loggers_ArrayLogger();
     $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
     $this->fireEvent('beforesend', array(&$this, &$response, &$message, &$mailer, $account, $alias, $params));
     $failedRecipients = array();
     $success = $mailer->send($message, $failedRecipients);
     // Update "last mailed" time of the emailed contacts.
     if ($success && GO::modules()->addressbook) {
         $toAddresses = $message->getTo();
         if (empty($toAddresses)) {
             $toAddresses = array();
         }
         $ccAddresses = $message->getCc();
         if (empty($ccAddresses)) {
             $ccAddresses = array();
         }
         $bccAddresses = $message->getBcc();
         if (empty($bccAddresses)) {
             $bccAddresses = array();
         }
         $emailAddresses = array_merge($toAddresses, $ccAddresses);
         $emailAddresses = array_merge($emailAddresses, $bccAddresses);
         foreach ($emailAddresses as $emailAddress => $fullName) {
             $findCriteria = \GO\Base\Db\FindCriteria::newInstance()->addCondition('email', $emailAddress, '=', 't', false)->addCondition('email2', $emailAddress, '=', 't', false)->addCondition('email3', $emailAddress, '=', 't', false);
             $findParams = \GO\Base\Db\FindParams::newInstance()->criteria($findCriteria);
             $contactsStmt = \GO\Addressbook\Model\Contact::model()->find($findParams);
             if ($contactsStmt) {
                 foreach ($contactsStmt as $contactModel) {
                     if ($contactModel->name == $fullName) {
                         $contactLastMailTimeModel = \GO\Email\Model\ContactMailTime::model()->findSingleByAttributes(array('contact_id' => $contactModel->id, 'user_id' => GO::user()->id));
                         if (!$contactLastMailTimeModel) {
                             $contactLastMailTimeModel = new \GO\Email\Model\ContactMailTime();
                             $contactLastMailTimeModel->contact_id = $contactModel->id;
                             $contactLastMailTimeModel->user_id = GO::user()->id;
                         }
                         $contactLastMailTimeModel->last_mail_time = time();
                         $contactLastMailTimeModel->save();
                     }
                 }
             }
         }
     }
     if (!empty($params['reply_uid'])) {
         //set \Answered flag on IMAP message
         GO::debug("Reply");
         $account2 = Account::model()->findByPk($params['reply_account_id']);
         $imap = $account2->openImapConnection($params['reply_mailbox']);
         $imap->set_message_flag(array($params['reply_uid']), "\\Answered");
     }
     if (!empty($params['forward_uid'])) {
         //set forwarded flag on IMAP message
         $account2 = Account::model()->findByPk($params['forward_account_id']);
         $imap = $account2->openImapConnection($params['forward_mailbox']);
         $imap->set_message_flag(array($params['forward_uid']), "\$Forwarded");
     }
     /**
      * if you want ignore default sent folder message will be store in
      * folder wherefrom user sent it
      */
     if ($account->ignore_sent_folder && !empty($params['reply_mailbox'])) {
         $account->sent = $params['reply_mailbox'];
     }
     if ($account->sent && $recipientCount > count($failedRecipients)) {
         GO::debug("Sent");
         //if a sent items folder is set in the account then save it to the imap folder
         $imap = $account->openImapConnection($account->sent);
         if (!$imap->append_message($account->sent, $message, "\\Seen")) {
             $response['success'] = false;
             $response['feedback'] .= 'Failed to save send item to ' . $account->sent;
         }
     }
     if (!empty($params['draft_uid'])) {
         //remove drafts on send
         $imap = $account->openImapConnection($account->drafts);
         $imap->delete(array($params['draft_uid']));
     }
     if (count($failedRecipients)) {
         $msg = GO::t('failedRecipients', 'email') . ': ' . implode(', ', $failedRecipients) . '<br /><br />';
         $logStr = $logger->dump();
         preg_match('/<< 55[0-9] .*>>/s', $logStr, $matches);
         if (isset($matches[0])) {
             $logStr = trim(substr($matches[0], 2, -2));
         }
         throw new \Exception($msg . nl2br($logStr));
     }
     //if there's an autolink tag in the message we want to link outgoing messages too.
     $tags = $this->_findAutoLinkTags($params['content_type'] == 'html' ? $params['htmlbody'] : $params['plainbody'], $account->id);
     $this->_link($params, $message, false, $tags);
     $response['unknown_recipients'] = $this->_findUnknownRecipients($params);
     return $response;
 }
Пример #2
0
 public static function beforeSend(\GO\Email\Controller\MessageController $controller, array &$response, \GO\Base\Mail\SmimeMessage $message, \GO\Base\Mail\Mailer $mailer, \GO\Email\Model\Account $account, \GO\Email\Model\Alias $alias, $params)
 {
     if (!empty($params['sign_smime'])) {
         //$password = trim(file_get_contents("/home/mschering/password.txt"));
         $password = GO::session()->values['smime']['passwords'][$account->id];
         $cert = Model\Certificate::model()->findByPk($account->id);
         $message->setSignParams($cert->cert, $password);
     }
     if (!empty($params['encrypt_smime'])) {
         if (!isset($cert)) {
             $cert = Model\Certificate::model()->findByPk($account->id);
         }
         $password = GO::session()->values['smime']['passwords'][$account->id];
         openssl_pkcs12_read($cert->cert, $certs, $password);
         if (!isset($certs['cert'])) {
             throw new \Exception("Failed to get your public key for encryption");
         }
         $to = $message->getTo();
         $cc = $message->getCc();
         $bcc = $message->getBcc();
         if (is_array($cc)) {
             $to = array_merge($to, $cc);
         }
         if (is_array($bcc)) {
             $to = array_merge($to, $bcc);
         }
         //lookup all recipients
         $failed = array();
         $publicCerts = array($certs['cert']);
         foreach ($to as $email => $name) {
             $pubCert = Model\PublicCertificate::model()->findSingleByAttributes(array('user_id' => GO::user()->id, 'email' => $email));
             if (!$pubCert) {
                 $failed[] = $email;
             } else {
                 $publicCerts[] = $pubCert->cert;
             }
         }
         if (count($failed)) {
             throw new \Exception(sprintf(GO::t('noPublicCertForEncrypt', 'smime'), implode(', ', $failed)));
         }
         $message->setEncryptParams($publicCerts);
     }
 }