/**
  * @brief send email 
  **/
 function procContactSendEmail()
 {
     $logged_info = Context::get('logged_info');
     if ($this->module_info->send_grant_all != 'Y' && !$logged_info) {
         return new Object(-1, 'msg_logged_can_send_mail');
     }
     if (!$this->module_info->admin_mail) {
         return new Object(-1, 'msg_do_set_admin_mail');
     }
     $oMail = new Mail();
     $oMail->setContentType("plain");
     // get form variables submitted
     $obj = Context::getRequestVars();
     if ($obj->enable_terms == 'Y' && !$obj->check_agree) {
         return new Object(-1, 'msg_terms_of_license_agreement');
     }
     $obj->email = $obj->Email;
     $obj->subject = $obj->Subject;
     $obj->comment = $obj->Comment;
     $oDocumentModel =& getModel('document');
     $extra_keys = $oDocumentModel->getExtraKeys($obj->module_srl);
     $mail_content = array();
     $filter_lang = Context::getLang('filter');
     $content = '';
     if (count($extra_keys)) {
         $oModuleController =& getController('module');
         foreach ($extra_keys as $idx => $extra_item) {
             $value = '';
             if (isset($obj->{'extra_vars' . $idx})) {
                 $value = $obj->{'extra_vars' . $idx};
             } elseif (isset($obj->{$extra_item->eid})) {
                 $value = $obj->{$extra_item->eid};
             }
             if (!is_array($value)) {
                 $value = trim($value);
             }
             if (!isset($value)) {
                 continue;
             }
             //check if extra item is required
             $oModuleController->replaceDefinedLangCode($extra_item->name);
             if ($extra_item->is_required == 'Y' && $value == "") {
                 return new Object(-1, sprintf($filter_lang->invalid, $extra_item->name));
             }
             //if the type of form component is email address
             if ($extra_item->type == 'email_address' && !$oMail->isVaildMailAddress($value)) {
                 return new Object(-1, sprintf($filter_lang->invalid_email, $extra_item->name));
             }
             if ($extra_item->type == "tel") {
                 $mail_content[$extra_item->eid] = $obj->{'extra_vars' . $idx}[2];
                 $content .= $extra_item->name . ':  ' . $obj->{'extra_vars' . $idx}[2] . "\r\n";
             } elseif (is_array($obj->{'extra_vars' . $idx})) {
                 $mail_content[$extra_item->eid] = implode(",", $obj->{'extra_vars' . $idx});
                 $content .= $extra_item->name . ':  ' . implode(",", $obj->{'extra_vars' . $idx}) . "\r\n";
             } else {
                 $mail_content[$extra_item->eid] = $value;
                 $content .= $extra_item->name . ':  ' . $value . "\r\n";
             }
             $mail_title[$extra_item->eid] = htmlspecialchars($extra_item->name);
         }
     }
     if (!$oMail->isVaildMailAddress($obj->email)) {
         return new Object(-1, sprintf($filter_lang->invalid_email, Context::getLang('email_address')));
     }
     $oMail->setTitle($obj->subject);
     $content_all = $content . "\r\nComments:\r\n" . htmlspecialchars($obj->comment);
     $mail_content['Comments'] = $obj->comment;
     $oMail->setContent(htmlspecialchars($content_all));
     //$oMail->setSender("XE Contact Us", $obj->email);
     $oMail->setSender($obj->email . "(" . $_SERVER['REMOTE_ADDR'] . ")", $obj->email);
     $target_mail = explode(',', $this->module_info->admin_mail);
     for ($i = 0; $i < count($target_mail); $i++) {
         $email_address = trim($target_mail[$i]);
         if (!$email_address || !$oMail->isVaildMailAddress($email_address)) {
             continue;
         }
         $oMail->setReceiptor($email_address, $email_address);
         if ($logged_info->is_admin != 'Y') {
             if ($this->module_info->module_srl) {
                 $oModuleModel =& getModel('module');
                 $moduleExtraVars = $oModuleModel->getModuleExtraVars($this->module_info->module_srl);
                 if ($moduleExtraVars[$this->module_info->module_srl]->interval) {
                     $interval = $moduleExtraVars[$this->module_info->module_srl]->interval;
                     //transfer interval to mins
                     $interval = $interval * 60;
                     $oContactModel =& getModel('contact');
                     $output = $oContactModel->checkLimited($interval);
                     if (!$output->toBool()) {
                         return $output;
                     }
                 }
             }
         }
         $oMail->send();
     }
     if (isset($_SESSION['mail_content'])) {
         unset($_SESSION['mail_content']);
     }
     if (isset($_SESSION['mail_title'])) {
         unset($_SESSION['mail_title']);
     }
     $_SESSION['mail_content'] = $mail_content;
     $_SESSION['mail_title'] = $mail_title;
     if ($logged_info->is_admin != 'Y') {
         $oSpamController =& getController('spamfilter');
         $oSpamController->insertLog();
     }
     $this->add('mid', Context::get('mid'));
     $this->setMessage('msg_email_send_successfully');
     if (!in_array(Context::getRequestMethod(), array('XMLRPC', 'JSON'))) {
         $returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'act', 'dispCompleteSendMail', 'mid', $obj->mid);
         header('location:' . $returnUrl);
         return;
     }
 }
 /**
  * Sends the newsletters.
  */
 protected function sendNewsletters()
 {
     $templateName = 'newsletterMail';
     //Sends mail to all subscribers.
     foreach ($this->outstandingNewsletters as $id => $newsletter) {
         $text = $newsletter['text'];
         //workaround to make sure that the template is found
         $templatePaths = array(WCF_DIR . 'templates/', WCF_DIR . 'acp/templates/');
         WCF::getTPL()->setTemplatePaths($templatePaths);
         $newsletterObj = new ViewableNewsletter($id);
         $emailText = $newsletterObj->getFormattedMessage();
         WCF::getTPL()->assign(array('subject' => $newsletter['subject'], 'text' => $emailText));
         $content = WCF::getTPL()->fetch($templateName);
         $i = 0;
         usleep(1);
         //sending one mail per subscriber
         //is longer, but safer
         foreach ($this->subscribersList as $subscriber) {
             //sleep 2 seconds after 10 sent mails
             if (fmod($i, 10) == 0) {
                 usleep(2000000);
             }
             $unsubscribeToken = '';
             if (!isset($this->unsubscribeTokens[$subscriber['subscriberID']])) {
                 $unsubscribeToken = StringUtil::getRandomID();
                 $sql = 'INSERT INTO wcf' . WCF_N . '_' . $this->unsubscriptionTable . '
             			(subscriberID, token)
             		VALUES
             			(' . intval($subscriber['subscriberID']) . ", '" . escapeString($unsubscribeToken) . "')";
                 WCF::getDB()->sendQuery($sql);
             } else {
                 $unsubscribeToken = $this->unsubscribeTokens[$subscriber['subscriberID']]['token'];
             }
             $recipient = null;
             if ($subscriber['userID']) {
                 $recipient = new User($subscriber['userID']);
                 // check for non receiving groups
                 if (!NewsletterUtil::canReceiveNewsletters($recipient)) {
                     continue;
                 }
             }
             // {$username} stands for the username of the specific subscriber
             if (is_null($recipient) || $recipient->getUserOption('acceptNewsletterAsEmail')) {
                 $tmpContent = str_replace('{$username}', $subscriber['username'], $content);
                 $tmpContent = str_replace('subscriberID', $subscriber['subscriberID'], $tmpContent);
                 $tmpContent = str_replace('token', $unsubscribeToken, $tmpContent);
                 $email = $subscriber['email'];
                 $mail = new Mail($email, $newsletter['subject'], $tmpContent, MESSAGE_NEWSLETTERSYSTEM_GENERAL_FROM);
                 //$mail->addBCC(MAIL_ADMIN_ADDRESS); would result in x mails
                 $mail->setContentType('text/html');
                 $mail->send();
             }
             if (!is_null($recipient) && $recipient->getUserOption('acceptNewsletterAsPM')) {
                 $recipientArray = array();
                 $recipientArray[] = array('userID' => $subscriber['userID'], 'username' => $subscriber['username']);
                 $admin = new User(MESSAGE_NEWSLETTERSYSTEM_GENERAL_ADMIN);
                 $options = array('enableSmilies' => $newsletter['enableSmilies'], 'enableHtml' => $newsletter['enableHtml'], 'enableBBCodes' => $newsletter['enableBBCodes']);
                 $tmpText = str_replace('{$username}', $subscriber['username'], $text);
                 $pm = PMEditor::create(false, $recipientArray, array(), $newsletter['subject'], $tmpText, $admin->userID, $admin->username, $options);
             }
             $i++;
         }
     }
     WCF::getCache()->clearResource('newsletter-subscriber-' . PACKAGE_ID);
 }
 /**
  * Sends a validation email.
  *
  * @param object $eventObj
  */
 protected function sendValidationEmail($eventObj)
 {
     //save activation token into database
     $token = StringUtil::getRandomID();
     $sql = 'INSERT INTO wcf' . WCF_N . '_' . $this->activationTable . '
     		(userID, token)
     			VALUES
     		(' . intval($eventObj->user->userID) . ", '" . escapeString($token) . "')";
     WCF::getDB()->sendQuery($sql);
     $url = PAGE_URL . '/index.php?action=NewsletterActivate&id=' . $eventObj->user->userID . '&t=' . $token;
     $subject = WCF::getLanguage()->get('wcf.acp.newsletter.optin.subject');
     $content = WCF::getLanguage()->getDynamicVariable('wcf.acp.newsletter.optin.text', array('username' => $eventObj->user->username, 'url' => $url));
     WCF::getTPL()->assign(array('subject' => $subject, 'content' => $content));
     $output = WCF::getTPL()->fetch('validationEmail');
     $mail = new Mail($eventObj->user->email, $subject, $output, MESSAGE_NEWSLETTERSYSTEM_GENERAL_FROM);
     $mail->setContentType('text/html');
     $mail->send();
 }
 /**
  * Sends a testmail of the given newsletter.
  *
  * @param Newsletter $newsletter
  */
 protected function sendTestmail(Newsletter $newsletter)
 {
     $newsletterID = $newsletter->newsletterID;
     //workaround to make sure that the template is found
     $templatePaths = array(WCF_DIR . 'templates/', WCF_DIR . 'acp/templates/');
     WCF::getTPL()->setTemplatePaths($templatePaths);
     $newsletterObj = new ViewableNewsletter($newsletterID);
     $emailText = $newsletterObj->getFormattedMessage();
     WCF::getTPL()->assign(array('subject' => $newsletter->subject, 'text' => $emailText));
     $templateName = 'newsletterMail';
     $content = WCF::getTPL()->fetch($templateName);
     $admin = new User(MESSAGE_NEWSLETTERSYSTEM_GENERAL_ADMIN);
     $tmpContent = str_replace('{$username}', $admin->username, $content);
     $tmpContent = str_replace('subscriberID', 1, $tmpContent);
     $tmpContent = str_replace('token', 'test', $tmpContent);
     $email = $admin->email;
     $mail = new Mail($email, $newsletter->subject, $tmpContent, MESSAGE_NEWSLETTERSYSTEM_GENERAL_FROM);
     $mail->setContentType('text/html');
     $mail->send();
     //resetting cache
     $cacheName = 'newsletter-' . PACKAGE_ID;
     WCF::getCache()->clear(WCF_DIR . 'cache/', 'cache.' . $cacheName . '.php');
     HeaderUtil::redirect('index.php?form=NewsletterEdit&newsletterID=' . $newsletterID . '&packageID=' . PACKAGE_ID . SID_ARG_2ND_NOT_ENCODED);
     exit;
 }
 /**
  * Sends the mail to given user.
  * 
  * @param	User		$user
  */
 protected function sendMail(User $user)
 {
     // send mail
     try {
         $mail = new Mail(array($user->username => $user->email), $this->userMailData['subject'], StringUtil::replace('{$username}', $user->username, $this->userMailData['text']), $this->userMailData['from']);
         if ($this->userMailData['enableHTML']) {
             $mail->setContentType('text/html');
         }
         $mail->send();
     } catch (SystemException $e) {
     }
     // ignore errors
 }
 /**
  * @see Form::save()
  */
 public function save()
 {
     parent::save();
     //save activation token into database
     $token = StringUtil::getRandomID();
     $sql = 'INSERT INTO wcf' . WCF_N . '_' . $this->subscriberTable . "\n        \t\t(email)\n        \t\t\tVALUES\n        \t\t('" . escapeString($this->email) . "')";
     WCF::getDB()->sendQuery($sql);
     $subscriberID = WCF::getDB()->getInsertID();
     //clears cache
     WCF::getCache()->clear(WCF_DIR . 'cache/', 'cache.newsletter-subscriber-' . PACKAGE_ID . '.php', true);
     $sql = 'INSERT INTO wcf' . WCF_N . '_' . $this->activationTable . '
     		(subscriberID, token)
     			VALUES
     		(' . intval($subscriberID) . ", '" . escapeString($token) . "')";
     WCF::getDB()->sendQuery($sql);
     $url = PAGE_URL . '/index.php?action=NewsletterGuestActivate&id=' . $subscriberID . '&t=' . $token;
     $subject = WCF::getLanguage()->get('wcf.acp.newsletter.optin.subject');
     $content = WCF::getLanguage()->getDynamicVariable('wcf.acp.newsletter.optin.text', array('username' => WCF::getLanguage()->get('wcf.acp.newsletter.optin.hello'), 'url' => $url));
     WCF::getTPL()->assign(array('subject' => $subject, 'content' => $content));
     $output = WCF::getTPL()->fetch('validationEmail');
     $mail = new Mail($this->email, $subject, $output, MESSAGE_NEWSLETTERSYSTEM_GENERAL_FROM);
     $mail->setContentType('text/html');
     $mail->send();
     $this->saved();
     WCF::getTPL()->assign(array('message' => WCF::getLanguage()->get('wcf.acp.newsletter.optin.activationPending'), 'url' => PAGE_URL . '/index.php?page=Index' . SID_ARG_2ND));
     WCF::getTPL()->display('redirect');
     exit;
 }
 /**
  * Sends a guest validation email.
  *
  * @param NewsletterSubscriber $subscriber
  */
 public static function sendGuestValidationEmail(NewsletterSubscriber $subscriber)
 {
     //check if an activation has already been started
     $sqlCheck = 'SELECT COUNT(token) AS count
     			FROM wcf' . WCF_N . '_' . self::$activationGuestTable . '
     			WHERE subscriberID = ' . intval($subscriber->subscriberID);
     $row = WCF::getDB()->getFirstRow($sqlCheck);
     if (intval($row['count'])) {
         $sqlDelete = 'DELETE FROM wcf' . WCF_N . '_' . self::$activationGuestTable . '
         			WHERE subscriberID = ' . intval($subscriber->subscriberID);
         WCF::getDB()->sendQuery($sqlDelete);
     }
     //save activation token into database
     $token = StringUtil::getRandomID();
     $sql = 'INSERT INTO wcf' . WCF_N . '_' . self::$activationGuestTable . '
     		(subscriberID, token)
     			VALUES
     		(' . intval($subscriber->subscriberID) . ", '" . escapeString($token) . "')";
     WCF::getDB()->sendQuery($sql);
     $url = PAGE_URL . '/index.php?action=NewsletterGuestActivate&id=' . $subscriber->subscriberID . '&t=' . $token;
     $subject = WCF::getLanguage()->get('wcf.acp.newsletter.optin.subject');
     $content = WCF::getLanguage()->getDynamicVariable('wcf.acp.newsletter.optin.text', array('username' => WCF::getLanguage()->get('wcf.acp.newsletter.optin.hello'), 'url' => $url));
     WCF::getTPL()->assign(array('subject' => $subject, 'content' => $content));
     $templatePathsOrig = WCF::getTPL()->getTemplatePaths();
     $templatePaths = array(WCF_DIR . 'templates/');
     WCF::getTPL()->setTemplatePaths(array_merge($templatePathsOrig, $templatePaths));
     $output = WCF::getTPL()->fetch('validationEmail');
     $mail = new Mail($subscriber->email, $subject, $output, MESSAGE_NEWSLETTERSYSTEM_GENERAL_FROM);
     $mail->setContentType('text/html');
     $mail->send();
 }