newsletters
[ class tree: newsletters ] [ index: newsletters ] [ all elements ]

Source for file BitMailer.php

Documentation is available at BitMailer.php

  1. <?php
  2. /**
  3.  * $Header: /cvsroot/bitweaver/_bit_newsletters/BitMailer.php,v 1.25 2006/10/13 09:22:47 lsces Exp $
  4.  *
  5.  * Copyright (c) 2004 bitweaver.org
  6.  * All Rights Reserved. See copyright.txt for details and a complete list of authors.
  7.  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details
  8.  *
  9.  * $Id: BitMailer.php,v 1.25 2006/10/13 09:22:47 lsces Exp $
  10.  *
  11.  * Class that handles editions of newsletters
  12.  * @package newsletters
  13.  *
  14.  *  created 2005/12/08
  15.  *
  16.  * @author spiderr <spider@steelsun.com>
  17.  *
  18.  * @version $Revision: 1.25 $ $Date: 2006/10/13 09:22:47 $ $Author: lsces $
  19.  */
  20.  
  21. /**
  22.  * required setup
  23.  */
  24. require_onceNEWSLETTERS_PKG_PATH.'BitNewsletter.php' );
  25. require_onceUTIL_PKG_PATH.'phpmailer/class.phpmailer.php' );
  26.  
  27. /**
  28.  * @package newsletters
  29.  */
  30. class BitMailer extends phpmailer {
  31.     // Set default variables for all new objects
  32.     
  33.     var $From;
  34.     var $FromName;
  35.     var $Host;
  36.     var $Mailer;                         // Alternative to IsSMTP()
  37.     
  38.     var $WordWrap;
  39.     function BitMailer ({
  40.         global $gBitDb$gBitSystem$gBitLanguage;
  41.         $this->mDb $gBitDb;
  42.         $this->From     = $gBitSystem->getConfig'bitmailer_sender_email'$gBitSystem->getConfig'site_sender_email'$_SERVER['SERVER_ADMIN') );
  43.         $this->FromName = $gBitSystem->getConfig'bitmailer_from'$gBitSystem->getConfig'siteTitle' ) );
  44.         $this->Host     = $gBitSystem->getConfig'bitmailer_servers'$gBitSystem->getConfig'kernel_server_name''127.0.0.1' ) );
  45.         $this->Mailer   = $gBitSystem->getConfig'bitmailer_protocol''smtp' )// Alternative to IsSMTP()
  46.         $this->WordWrap = $gBitSystem->getConfig'bitmailer_word_wrap'75 );
  47.         if!$this->SetLanguage$gBitLanguage->getLanguage()UTIL_PKG_PATH.'phpmailer/language/' ) ) {
  48.             $this->SetLanguage'en' );
  49.         }
  50.     }
  51.  
  52.     // Replace the default error_handler
  53.     
  54.     function error_handler$msg {
  55.         global $gBitDb;
  56.         bit_error_handlerNULLNULLNULL"FULFILLMENT ERRORMISSSING PDF for ORDER $pOrderId CID ".$prod->mInfo['related_content_id']$pdfInfo['pdf_file']''$prod->mDb );
  57.         print("My Site Error");
  58.         print("Description:");
  59.         printf("%s"$msg);
  60.         exit;
  61.     }
  62.  
  63.     function queueRecipients$pContentId$pNewsletterContentId$pRecipients {
  64.         $ret 0;
  65.         if!empty$pRecipients && BitBase::verifyId$pContentId ) ) {
  66.             $queueTime time();
  67.             foreacharray_keys$pRecipients AS $email {
  68.                 $insertHash['mail_queue_id'$this->mDb->GenID'mail_queue_id' );
  69.                 $insertHash['email'$email;
  70.                 if!empty$pRecipients[$email]['user_id') ) {
  71.                     $insertHash['user_id'$pRecipients[$email]['user_id'];
  72.                 }
  73.                 $insertHash['content_id'$pContentId;
  74.                 $insertHash['nl_content_id'$pNewsletterContentId;
  75.                 $insertHash['queue_date'$queueTime;
  76.                 $this->mDb->associateInsertBIT_DB_PREFIX.'mail_queue'$insertHash );
  77.                 $ret++;
  78.             }
  79.         }
  80.         return $ret;
  81.     }
  82.  
  83.     function tendQueue({
  84.         global $gBitSmarty$gBitSystem;
  85.         $body array();
  86.         $this->mDb->StartTrans();
  87.         $query "SELECT *
  88.                   FROM `".BIT_DB_PREFIX."mail_queue` mq
  89.                   WHERE `sent_date` IS NULL ".$this->mDb->SQLForUpdate();
  90.         if$rs $this->mDb->query$queryNULL ) ) {
  91.             while$pick $rs->fetchRow() ) {
  92.                 if!empty$pick['user_id') ) {
  93.                     $userHash $this->mDb->getRow"SELECT * FROM `".BIT_DB_PREFIX."users_users` WHERE `user_id`=?"array$pick['user_id') );
  94.                     $pick['full_name'BitUser::getDisplayNameFALSE$userHash );
  95.                 else {
  96.                     $pick['full_name'NULL;
  97.                 }
  98.                 if!isset$body[$pick['content_id']] ) ) {
  99.                     $gBitSmarty->assign'sending'TRUE );
  100.                     // We only support sending of newsletters currently
  101.                     $content new BitNewsletterEditionNULL$pick['content_id');
  102.                     if$content->load() ) {
  103.                         $body[$pick['content_id']]['body'$content->render();
  104.                         $body[$pick['content_id']]['subject'$content->getTitle();
  105.                         $body[$pick['content_id']]['reply_to'$content->getField'reply_to'$gBitSystem->getConfig'site_sender_email'$_SERVER['SERVER_ADMIN') );
  106.                         $body[$pick['content_id']]['object'$content;
  107.                     }
  108.     //                $content[$pick['content_id']] = LibertyBase::getLibertyObject();
  109.                 }
  110.                 if!empty$body[$pick['content_id']] ) ) {
  111.                     $pick['url_code'md5$pick['content_id'].$pick['email'].$pick['queue_date');
  112.                     $unsub '';
  113.                     if$body[$pick['content_id']]['object']->mNewsletter->getField('unsub_msg') ) {
  114.                         $gBitSmarty->assign'url_code'$pick['url_code');
  115.                         $gBitSmarty->assign'sending'TRUE );
  116.                     }
  117.                     $gBitSystem->preDisplay('');
  118.                     $gBitSmarty->assign'mid''bitpackage:newsletters/view_edition.tpl' );
  119.                     $htmlBody $gBitSmarty->fetch'bitpackage:kernel/bitweaver.tpl' );
  120.                     $gBitSmarty->assign'unsubMessage'$unsub );
  121.                     $gBitSmarty->assign'trackCode'$pick['url_code');
  122.                     $this->ClearReplyTos();
  123.                     $this->AddReplyTo$body[$pick['content_id']]['reply_to']$gBitSystem->getConfig'bitmailer_from' ) );
  124.                     print "TO$pick[email]\t";
  125.                     if$this->sendMail$pick$body[$pick['content_id']]['subject']$htmlBody ) ) {
  126.                         print "SENT\n";
  127.                         $updateQuery "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `sent_date`=?,`url_code`=?  WHERE `content_id`=? AND `email`=?";
  128.                         $this->mDb->query$updateQueryarraytime()$pick['url_code']$pick['content_id']$pick['email') );
  129.                     else {
  130.                         $this->logError$pick );
  131.                     }
  132.                     $this->mDb->CompleteTrans();
  133.                     $this->mDb->StartTrans();
  134.                 }
  135.             }
  136.         }
  137.         $this->mDb->CompleteTrans();
  138.     }
  139.  
  140.  
  141.     function sendMail$pRecipient$pSubject$pHtmlBody {
  142.         $ret TRUE;
  143.         $this->Body    $pHtmlBody;
  144.         $this->Subject $pSubject;
  145.         $this->IsHTMLTRUE );
  146.         $this->AltBody '';
  147.         $this->AddAddress$pRecipient['email']$pRecipient["full_name");
  148.         if(!$this->Send()) {
  149.             $ret FALSE;
  150.         }
  151.  
  152.         // Clear all addresses and attachments for next loop
  153.         $this->ClearAddresses();
  154.         $this->ClearAttachments();
  155.         return $ret;
  156.     }
  157.  
  158.     function trackMail$pUrlCode {
  159.         global $gBitDb;
  160.         $query "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `reads`=`reads`+1, `last_read_date`=? WHERE `url_code`=? ";
  161.         $gBitDb->query$queryarraytime()$pUrlCode ) );
  162.     }
  163.  
  164.     function logError$pInfo {
  165.         if!empty$pInfo['url_code'&& !$this->mDb->getOne"SELECT `url_code` FROM `".BIT_DB_PREFIX."mail_errors` WHERE `url_code`=?"array$pInfo['url_code') ) ) {
  166.             $store['url_code'$pInfo['url_code'];
  167.             $store['user_id'!empty$pInfo['user_id'$pInfo['user_id'NULL;
  168.             $store['content_id'!empty$pInfo['content_id'$pInfo['content_id'NULL;
  169.             $store['email'!empty$pInfo['email'$pInfo['email'NULL;
  170.             $store['error_message'$this->ErrorInfo;
  171.             $store['error_date'time();
  172.             $this->mDb->associateInsertBIT_DB_PREFIX."mail_errors"$store );
  173.         }
  174.         print "ERROR: ".$this->ErrorInfo."\n";
  175.     }
  176.  
  177.     // Looks up the code from the url to determine if the unsubscribe URL is valid.
  178.     // Can be statically called
  179.     
  180.     function lookupSubscription$pLookup {
  181.         global $gBitDb;
  182.         $ret NULL;
  183.         ifis_array$pLookup ) ) {
  184.             $query "SELECT mq.*, lc.title, tct.*, uu.`real_name`, uu.`login`, uu.`email` FROM `".BIT_DB_PREFIX."mail_queue` mq
  185.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( mq.`content_id`=lc.`content_id` )
  186.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content_types` tct ON( tct.`content_type_guid`=lc.`content_type_guid` )
  187.                         LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON( mq.`user_id`=uu.`user_id` )
  188.                       WHERE mq.`".key$pLookup )."`=? ";
  189.             $ret $gBitDb->getRow$queryarraycurrent$pLookup ) ) );
  190.         }
  191.         return$ret );
  192.     }
  193.  
  194.     // Accepts a single row has containing the column of mail_subscriptions as the key to lookup the unsubscription info
  195.     // Can be statically called
  196.     
  197.     function getUnsubscriptions$pMixed {
  198.         global $gBitDb;
  199.         $ret NULL;
  200.         ifis_array$pMixed ) ) {
  201.             $col key$pMixed );
  202.             $bindVars[current$pMixed );
  203.             $query "SELECT ms.`content_id` AS `hash_key`, ms.*, uu.*, lc.title
  204.                       FROM `".BIT_DB_PREFIX."mail_subscriptions` ms
  205.                         LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON( ms.`user_id`=uu.`user_id` )
  206.                         LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_contentlc ONms.`content_id`=lc.`content_id` )
  207.                       WHERE ms.`$col`=? ";
  208.             $ret $gBitDb->getAssoc$query$bindVars );
  209.         }
  210.         return$ret );
  211.     }
  212.  
  213.     function storeSubscriptions$pSubHash {
  214.         global $gBitSystem$gBitDb;
  215.         $ret FALSE;
  216.         $query "delete from `".BIT_DB_PREFIX."mail_subscriptions` where `".key$pSubHash['sub_lookup')."`=?";
  217.         $result $gBitDb->query($queryarraycurrent$pSubHash['sub_lookup') ) );
  218.         $ret TRUE;
  219.         if!empty$pSubHash['unsub_content') ) {
  220.             foreach$pSubHash['unsub_content'as $conId {
  221.                 $storeHash array();
  222.                 $storeHash['content_id'$conId;
  223.                 $storeHash['unsubscribe_all'!empty$pSubHash['unsubscribe_all''y' NULL;
  224.                 $storeHash['unsubscribe_date'time();
  225.                 $storeHash[key$pSubHash['sub_lookup')current$pSubHash['sub_lookup');
  226.                 if!empty$pSubHash['response_content_id') ) {
  227.                     $storeHash['response_content_id'$pSubHash['response_content_id'];
  228.                 }
  229.                 $gBitDb->associateInsertBIT_DB_PREFIX."mail_subscriptions"$storeHash );
  230.             }
  231.         }
  232.         return $ret;
  233.     }
  234.  
  235.     function getQueue&$pListHash {
  236.         $ret array();
  237.         
  238.         LibertyContent::prepGetList$pListHash );
  239.         
  240.         $query "SELECT mq.`mail_queue_id` AS `hash_key`, mq.*, lc.`title`, lc2.`title` AS newsletter_title
  241.                   FROM `".BIT_DB_PREFIX."mail_queue` mq 
  242.                     INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.content_id=mq.content_id)
  243.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lc2 ON (lc2.content_id=mq.nl_content_id)
  244.                   WHERE begin_date IS NULL 
  245.                   ORDER BY queue_date";
  246.         if$rs $this->mDb->query$query ) ) {
  247.             $ret $rs->getAssoc();
  248.         }
  249.         
  250.         return $ret;
  251.     }
  252.  
  253.     function expungeQueueRow$pQueueId {
  254.         ifBitBase::verifyId$pQueueId ) ) {
  255.             $this->mDb->query"DELETE FROM ".BIT_DB_PREFIX."mail_queue` WHERE `mail_queue_id`=?"array$pQueueId ) );
  256.         }
  257.     }
  258. }
  259. ?>

Documentation generated on Thu, 15 Feb 2007 20:31:12 +0000 by phpDocumentor 1.3.0