function preDispatch()
 {
     ignore_user_abort(true);
     @set_time_limit(0);
     @ini_set('memory_limit', '128M');
     if ($this->queue_id = $this->getFiltered('queue_id')) {
         $this->saved = $this->getDi()->emailSentTable->load($this->queue_id);
         $this->_request->fromArray($this->saved->unserialize());
     } elseif ($id = $this->getInt('resend_id')) {
         $this->saved = $this->getDi()->emailSentTable->load($id);
         unset($_GET['resend_id']);
         $this->getRequest()->fromArray($_POST = $this->saved->unserialize());
     }
     $this->_request->set('format', $this->getParam('format', 'html'));
     $this->searchUi = new Am_Query_Ui();
     $this->searchUi->addDefaults();
     $this->searchUi->setFromRequest($this->_request);
 }
예제 #2
0
 public function batchSend(&$context, Am_BatchProcessor $batch)
 {
     if ($this->saved->count_users <= $this->saved->sent_users) {
         return true;
     }
     // we are done;
     $q = $this->searchUi->query($this->saved->sent_users, 10);
     $i = 0;
     $db = $this->getDi()->db;
     $foundrows = false;
     while ($r = $db->fetchRow($q)) {
         $foundrows = true;
         if (!$batch->checkLimits()) {
             return false;
         }
         $r['name'] = $r['name_f'] . ' ' . $r['name_l'];
         $this->saved->updateQuick(array('last_email' => $r['email'], 'sent_users' => $this->saved->sent_users + 1));
         if ($r['email'] == '') {
             continue;
         }
         $m = $this->getDi()->mail;
         $m->setPeriodic(Am_Mail::ADMIN_REQUESTED);
         $m->addHeader('X-Amember-Queue-Id', $this->_request->getFiltered('queue_id'));
         $m->addUnsubscribeLink(Am_Mail::LINK_USER);
         $m->addTo($r['email'], $r['name']);
         if ($reply_to = $this->getParam('reply_to')) {
             switch ($reply_to) {
                 case 'default':
                     $email = false;
                     break;
                 case 'other':
                     $email = $this->getParam('reply_to_other');
                     $name = null;
                     break;
                 default:
                     preg_match('/^admin-(\\d+)$/', $reply_to, $match);
                     $admin = $this->getDi()->adminTable->load($match[1], false);
                     if ($admin) {
                         $email = $admin->email;
                         $name = $admin->getName();
                     }
                     break;
             }
             if ($email = filter_var($email, FILTER_VALIDATE_EMAIL)) {
                 $m->setReplyTo($email, $name);
             }
         }
         $subject = $this->getParam('subject');
         $body = $this->getParam('body');
         $tpl = new Am_SimpleTemplate();
         $tpl->assignStdVars();
         if (strpos($body, '%user.unsubscribe_link%') !== false || strpos($subject, '%user.unsubscribe_link%') !== false) {
             $r['unsubscribe_link'] = Am_Mail::getUnsubscribeLink($r['email'], Am_Mail::LINK_USER);
         }
         $tpl->user = $r;
         $this->getDi()->hook->call(Am_Event::MAIL_SIMPLE_TEMPLATE_BEFORE_PARSE, array('template' => $tpl, 'body' => $body, 'subject' => $subject, 'mail' => $m, 'request' => $this->getRequest()));
         $subject = $tpl->render($subject);
         $body = $tpl->render($body);
         if ($this->getParam('email_template_layout_id') && ($layout = $this->getDi()->emailTemplateLayoutTable->load($this->getParam('email_template_layout_id', false)))) {
             $tpl->assign('content', $body);
             $body = $tpl->render($layout->layout);
         }
         $m->setSubject($subject);
         if ($this->getParam('format') == 'text') {
             $m->setBodyText($body);
         } else {
             $text = strip_tags($body);
             $html = strpos($body, '<html') === false ? "<html><head><title>{$subject}</title></head><body>{$body}</body></html>" : $body;
             $m->setBodyHtml($html);
             $m->setBodyText($text);
         }
         foreach ($this->getAttachments() as $at) {
             $m->addAttachment($at);
         }
         try {
             $m->send();
         } catch (Zend_Mail_Exception $e) {
             trigger_error("Error happened while sending e-mail to {$r['email']} : " . $e->getMessage(), E_USER_WARNING);
         }
     }
     $this->getDi()->db->freeResult($q);
     if (!$foundrows) {
         return true;
     }
     if ($this->saved->count_users <= $this->saved->sent_users) {
         return true;
     }
     // we are done;
 }