From 7f42826200be90d39ad98a23a06977e656a0115c Mon Sep 17 00:00:00 2001 From: Christian Fowler Date: Wed, 18 Jun 2008 16:47:20 +0000 Subject: mv BitMailer to BitNewsletterMailer to avoid conflict and confusion with the kernel class --- BitMailer.php | 351 ---------------------------------- BitNewsletterMailer.php | 351 ++++++++++++++++++++++++++++++++++ admin/mail_queue.php | 12 +- admin/send.php | 10 +- admin/tend_mail_queue.php | 8 +- bit_setup_inc.php | 4 +- sub.php | 16 +- templates/center_list_newsletters.php | 2 +- track.php | 6 +- 9 files changed, 380 insertions(+), 380 deletions(-) delete mode 100644 BitMailer.php create mode 100644 BitNewsletterMailer.php diff --git a/BitMailer.php b/BitMailer.php deleted file mode 100644 index de1b739..0000000 --- a/BitMailer.php +++ /dev/null @@ -1,351 +0,0 @@ - - * - * @version $Revision: 1.40 $ $Date: 2008/01/02 18:26:31 $ $Author: spiderr $ - */ - -/** - * required setup - */ -require_once( NEWSLETTERS_PKG_PATH.'BitNewsletter.php' ); -require_once( UTIL_PKG_PATH.'phpmailer/class.phpmailer.php' ); - -/** - * @package newsletters - */ -class BitMailer { - // Set default variables for all new objects - var $From; - var $FromName; - var $Host; - var $Mailer; // Alternative to IsSMTP() - var $WordWrap; - function BitMailer () { - global $gBitDb, $gBitSystem, $gBitLanguage; - $this->mDb = $gBitDb; - } - - // Replace the default error_handler - function error_handler( $msg ) { - global $gBitDb; - bit_error_handler( NULL, NULL, NULL, "FULFILLMENT ERROR: MISSSING PDF for ORDER $pOrderId CID ".$prod->mInfo['related_content_id'], $pdfInfo['pdf_file'], '', $prod->mDb ); - print("My Site Error"); - print("Description:"); - printf("%s", $msg); - exit; - } - - function isRecipientQueued( $pRecipientMixed, $pContentId ) { - if( BitBase::verifyId( $pRecipientMixed ) ) { - $lookupCol = 'user_id'; - } else { - $lookupCol = 'email'; - } - return( $this->mDb->getOne( "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."mail_queue` WHERE `content_id`=? AND `$lookupCol`=?", array( $pContentId, $pRecipientMixed ) ) ); - } - - function queueRecipients( $pContentId, $pNewsletterContentId, $pRecipients, $pRequeue=FALSE ) { - $ret = 0; - if( !empty( $pRecipients ) && BitBase::verifyId( $pContentId ) ) { - $queueTime = time(); - foreach( array_keys( $pRecipients ) AS $email ) { - $unsub = $this->getUnsubscription( $email, $pContentId ); - $lookup = !empty( $pRecipients[$email]['user_id'] ) ? $pRecipients[$email]['user_id'] : $email; - if( empty( $unsub ) && !$this->isRecipientQueued( $lookup, $pContentId ) ) { - $insertHash['mail_queue_id'] = $this->mDb->GenID( 'mail_queue_id' ); - $insertHash['email'] = $email; - if( !empty( $pRecipients[$email]['user_id'] ) ) { - $insertHash['user_id'] = $pRecipients[$email]['user_id']; - } - $insertHash['content_id'] = $pContentId; - $insertHash['nl_content_id'] = $pNewsletterContentId; - $insertHash['queue_date'] = $queueTime; - $this->mDb->associateInsert( BIT_DB_PREFIX.'mail_queue', $insertHash ); - $ret++; - } elseif( empty( $unsub ) && $pRequeue ) { - $bindVars = array( $queueTime, $pContentId ); - if( !empty( $pRecipients[$email]['user_id'] ) ) { - $lookupCol = 'user_id'; - $bindVars[] = $pRecipients[$email]['user_id']; - } else { - $lookupCol = 'email'; - $bindVars[] = $email; - } - $rs = $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `queue_date`=?, `begin_date`=NULL, `sent_date`=NULL, `last_read_date`=NULL, `mail_error`=NULL, `reads`=0 WHERE `content_id`=? AND `$lookupCol`=?", array( $bindVars ) ); - $ret++; - } - } - } - return $ret; - } - - - function tendQueue() { - $this->mDb->StartTrans(); - $query = "SELECT * - FROM `".BIT_DB_PREFIX."mail_queue` mq - WHERE `sent_date` IS NULL AND `mail_error` IS NULL - ORDER BY `queue_date`,`user_id`,`email` ".$this->mDb->SQLForUpdate(); - if( $rs = $this->mDb->query( $query, NULL ) ) { - while( $pick = $rs->fetchRow() ) { - $this->sendQueue( $pick ); - $this->mDb->CompleteTrans(); - $this->mDb->StartTrans(); - } - } - $this->mDb->CompleteTrans(); - } - - function sendQueue( $pQueueMixed ) { - global $gBitSmarty, $gBitSystem, $gBitLanguage; - static $body = array(); - if( is_array( $pQueueMixed ) ) { - $pick = $pQueueMixed; - } elseif( is_numeric( $pQueueMixed ) ) { - $pick = $this->mDb->GetRow( "SELECT * FROM `".BIT_DB_PREFIX."mail_queue` mq WHERE `mail_queue_id` = ? ".$this->mDb->SQLForUpdate(), array( $pQueueMixed ) ); - } - - if( !empty( $pick ) ) { - $startTime = microtime( TRUE ); - $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `begin_date`=? WHERE `mail_queue_id` = ? ", array( time(), $pick['mail_queue_id'] ) ); - if( !empty( $pick['user_id'] ) ) { - $userHash = $this->mDb->getRow( "SELECT * FROM `".BIT_DB_PREFIX."users_users` WHERE `user_id`=?", array( $pick['user_id'] ) ); - $pick['full_name'] = BitUser::getDisplayName( FALSE, $userHash ); - } else { - $pick['full_name'] = NULL; - } - if( !isset( $body[$pick['content_id']] ) ) { - $gBitSmarty->assign( 'sending', TRUE ); - // We only support sending of newsletters currently - $content = new BitNewsletterEdition( NULL, $pick['content_id'] ); - if( $content->load() ) { - $body[$pick['content_id']]['body'] = $content->render(); - $body[$pick['content_id']]['subject'] = $content->getTitle(); - $body[$pick['content_id']]['reply_to'] = $content->getField( 'reply_to', $gBitSystem->getConfig( 'site_sender_email', $_SERVER['SERVER_ADMIN'] ) ); - $body[$pick['content_id']]['object'] = $content; - } else { - bit_log_error( $this->mErrors ); - } -// $content[$pick['content_id']] = LibertyBase::getLibertyObject(); - } - - print "[ $pick[mail_queue_id] ] $pick[content_id] TO: $pick[email]\t"; - $unsub = $this->getUnsubscription( $pick['email'], $pick['content_id'] ); - if( !empty( $unsub ) ) { - print " SKIPPED (unsubscribed)
\n"; - $this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."mail_queue` WHERE `mail_queue_id`=?", array( $pick['mail_queue_id'] ) ); - } elseif( !empty( $body[$pick['content_id']] ) ) { - $pick['url_code'] = md5( $pick['content_id'].$pick['email'].$pick['queue_date'] ); - $unsub = ''; - if( $body[$pick['content_id']]['object']->mNewsletter->getField('unsub_msg') ) { - $gBitSmarty->assign( 'url_code', $pick['url_code'] ); - } - $gBitSystem->preDisplay(''); - $gBitSmarty->assign( 'sending', TRUE ); - $gBitSmarty->assign( 'unsubMessage', $unsub ); - $gBitSmarty->assign( 'trackCode', $pick['url_code'] ); - $gBitSmarty->assign( 'mid', 'bitpackage:newsletters/view_edition.tpl' ); - $htmlBody = $gBitSmarty->fetch( 'bitpackage:newsletters/mail_edition.tpl' ); - $htmlBody = bit_add_clickthrough( $htmlBody, $pick['url_code'] ); - - $mailer = new PHPMailer(); - $mailer->From = $gBitSystem->getConfig( 'bitmailer_sender_email', $gBitSystem->getConfig( 'site_sender_email', $_SERVER['SERVER_ADMIN'] ) ); - $mailer->FromName = $gBitSystem->getConfig( 'bitmailer_from', $gBitSystem->getConfig( 'site_title' ) ); - $mailer->Host = $gBitSystem->getConfig( 'bitmailer_servers', $gBitSystem->getConfig( 'kernel_server_name', '127.0.0.1' ) ); - $mailer->Mailer = $gBitSystem->getConfig( 'bitmailer_protocol', 'smtp' ); // Alternative to IsSMTP() - if( $gBitSystem->getConfig( 'bitmailer_smtp_username' ) ) { - $mailer->SMTPAuth = TRUE; - $mailer->Username = $gBitSystem->getConfig( 'bitmailer_smtp_username' ); - } - if( $gBitSystem->getConfig( 'bitmailer_smtp_password' ) ) { - $mailer->Password = $gBitSystem->getConfig( 'bitmailer_smtp_password' ); - } - $mailer->WordWrap = $gBitSystem->getConfig( 'bitmailer_word_wrap', 75 ); - if( !$mailer->SetLanguage( $gBitLanguage->getLanguage(), UTIL_PKG_PATH.'phpmailer/language/' ) ) { - $mailer->SetLanguage( 'en' ); - } - $mailer->ClearReplyTos(); - $mailer->AddReplyTo( $body[$pick['content_id']]['reply_to'], $gBitSystem->getConfig( 'bitmailer_from' ) ); - $mailer->Body = $htmlBody; - $mailer->Subject = $body[$pick['content_id']]['subject']; - $mailer->IsHTML( TRUE ); - $mailer->AltBody = ''; - $mailer->AddAddress( $pick['email'], $pick["full_name"] ); - if( $mailer->Send() ) { - print " SENT ".round( microtime( TRUE ) - $startTime, 2)." secs
\n"; flush(); - $updateQuery = "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `sent_date`=?,`url_code`=? WHERE `content_id`=? AND `email`=?"; - $this->mDb->query( $updateQuery, array( time(), $pick['url_code'], $pick['content_id'], $pick['email'] ) ); - } else { - $updateQuery = "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `mail_error`=?,`sent_date`=? WHERE `content_id`=? AND `email`=?"; - $this->mDb->query( $updateQuery, array( $mailer->ErrorInfo, time(), $pick['content_id'], $pick['email'] ) ); - $pick['error'] = $mailer->ErrorInfo; - $this->logError( $pick ); - } - } - } - } - - function trackMail( $pUrlCode ) { - global $gBitDb; - $query = "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `reads`=`reads`+1, `last_read_date`=?, `last_read_ip`=? WHERE `url_code`=? "; - $gBitDb->query( $query, array( time(), $_SERVER['REMOTE_ADDR'], $pUrlCode ) ); - } - - function logError( $pInfo ) { - if( !empty( $pInfo['url_code'] ) && !$this->mDb->getOne( "SELECT `url_code` FROM `".BIT_DB_PREFIX."mail_errors` WHERE `url_code`=?", array( $pInfo['url_code'] ) ) ) { - $store['url_code'] = $pInfo['url_code']; - $store['user_id'] = !empty( $pInfo['user_id'] ) ? $pInfo['user_id'] : NULL; - $store['content_id'] = !empty( $pInfo['content_id'] ) ? $pInfo['content_id'] : NULL; - $store['email'] = !empty( $pInfo['email'] ) ? $pInfo['email'] : NULL; - $store['error_message'] = $pInfo['error']; - $store['error_date'] = time(); - $this->mDb->associateInsert( BIT_DB_PREFIX."mail_errors", $store ); - } - print "ERROR: ".$pInfo['error']."\n"; - } - - // Looks up the code from the url to determine if the unsubscribe URL is valid. - // Can be statically called - function lookupSubscription( $pLookup ) { - global $gBitDb; - $ret = NULL; - if( is_array( $pLookup ) ) { - $query = "SELECT mq.*, lc.title, tct.*, uu.`real_name`, uu.`login`, uu.`email` FROM `".BIT_DB_PREFIX."mail_queue` mq - INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( mq.`content_id`=lc.`content_id` ) - INNER JOIN `".BIT_DB_PREFIX."liberty_content_types` tct ON( tct.`content_type_guid`=lc.`content_type_guid` ) - LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON( mq.`user_id`=uu.`user_id` ) - WHERE mq.`".key( $pLookup )."`=? "; - $ret = $gBitDb->getRow( $query, array( current( $pLookup ) ) ); - } - return( $ret ); - } - - // Accepts a single row has containing the column of mail_subscriptions as the key to lookup the unsubscription info - // Can be statically called - function getUnsubscriptions( $pMixed ) { - global $gBitDb; - $ret = NULL; - if( is_array( $pMixed ) ) { - $col = key( $pMixed ); - $bindVars[] = current( $pMixed ); - $query = "SELECT ms.`content_id` AS `hash_key`, ms.*, uu.*, lc.title - FROM `".BIT_DB_PREFIX."mail_subscriptions` ms - LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON( ms.`user_id`=uu.`user_id` ) - LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( ms.`content_id`=lc.`content_id` ) - WHERE ms.`$col`=? "; - $ret = $gBitDb->getAssoc( $query, $bindVars ); - } - return( $ret ); - } - - function getUnsubscription( $pEmail, $pContentId ) { - global $gBitDb; - return $gBitDb->getRow( "SELECT * FROM `".BIT_DB_PREFIX."mail_subscriptions` ms LEFT JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`user_id`=ms.`user_id`) WHERE (ms.`content_id`=? OR `unsubscribe_all`='y') AND (ms.`email`=? OR uu.`email`=?)", array( $pContentId, $pEmail, $pEmail ) ); - } - - function storeSubscriptions( $pSubHash ) { - global $gBitSystem, $gBitDb; - $ret = FALSE; - $query = "delete from `".BIT_DB_PREFIX."mail_subscriptions` where `".key( $pSubHash['sub_lookup'] )."`=?"; - $result = $gBitDb->query($query, array( current( $pSubHash['sub_lookup'] ) ) ); - $ret = TRUE; - if( !empty( $pSubHash['unsub_content'] ) ) { - foreach( $pSubHash['unsub_content'] as $conId ) { - $storeHash = array(); - $storeHash['content_id'] = $conId; - $storeHash['unsubscribe_all'] = !empty( $pSubHash['unsubscribe_all'] ) ? 'y' : NULL; - $storeHash['unsubscribe_date'] = time(); - $storeHash[key( $pSubHash['sub_lookup'] )] = current( $pSubHash['sub_lookup'] ); - if( !empty( $pSubHash['response_content_id'] ) ) { - $storeHash['response_content_id'] = $pSubHash['response_content_id']; - } - $gBitDb->associateInsert( BIT_DB_PREFIX."mail_subscriptions", $storeHash ); - } - } - return $ret; - } - - function getQueue( &$pListHash ) { - $ret = array(); - - LibertyContent::prepGetList( $pListHash ); - - $query = "SELECT mq.`mail_queue_id` AS `hash_key`, mq.*, lc.`title`, lc2.`title` AS newsletter_title - FROM `".BIT_DB_PREFIX."mail_queue` mq - INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.content_id=mq.content_id) - LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lc2 ON (lc2.content_id=mq.nl_content_id) - WHERE begin_date IS NULL - ORDER BY queue_date"; - if( $rs = $this->mDb->query( $query ) ) { - $ret = $rs->getAssoc(); - } - - return $ret; - } - - function expungeQueueRow( $pQueueId ) { - if( BitBase::verifyId( $pQueueId ) ) { - $this->mDb->query( "DELETE FROM ".BIT_DB_PREFIX."mail_queue` WHERE `mail_queue_id`=?", array( $pQueueId ) ); - } - } - - function storeClickthrough( $pUrlCode ) { - global $gBitDb; - - $uri = substr( preg_replace( '/[&\?]?ct=[a-z0-9]{32}/', '', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] ), 0, 250 ); - $query = "SELECT mc.`clicks`, mq.`content_id`, mq.`user_id`, mc.`clicked_url` FROM `".BIT_DB_PREFIX."mail_queue` mq - LEFT JOIN `".BIT_DB_PREFIX."mail_clickthrough` mc ON (mq.`user_id`=mc.`user_id` AND mq.`content_id`=mc.`content_id` AND mc.`clicked_url`=?) - WHERE `url_code`=?"; - if( $row = $gBitDb->getRow( $query, array( $uri, $pUrlCode ) ) ) { - if( $row['clicked_url'] ) { - $gBitDb->query( "UPDATE `".BIT_DB_PREFIX."mail_clickthrough` SET `clicks`=`clicks`+1 WHERE `user_id`=? AND `content_id`=? AND `clicked_url`=? ", array( $row['user_id'], $row['content_id'], $row['clicked_url'] ) ); - } else { - $row['clicks'] = 1; - $row['clicked_url'] = $uri; - $gBitDb->associateInsert( BIT_DB_PREFIX.'mail_clickthrough', $row ); - } - } - } -} - -// This will insert a ticket on all template URL's that have GET parameters. -function bit_add_clickthrough( $pSource, $pUrlCode ) { - global $gBitUser, $gUrlCode; - - $gUrlCode = $pUrlCode; - - $pcre = '%href[\s]*=[\s]*["\']*((http|https)://[^\s"\']+?)(.*)%sU'; - - $ret = preg_replace_callback( $pcre, 'process_clickthrough_match', $pSource ); - - return $ret; - -} - - -function process_clickthrough_match( $matches ) { - global $gUrlCode; - $ret = $matches[0]; - if( strpos( $matches[0], '?' ) ) { - $ret .= '&ct='.$gUrlCode; - } else { - $ret .= '?ct='.$gUrlCode; - } - return $ret; -} - -?> diff --git a/BitNewsletterMailer.php b/BitNewsletterMailer.php new file mode 100644 index 0000000..28daf88 --- /dev/null +++ b/BitNewsletterMailer.php @@ -0,0 +1,351 @@ + + * + * @version $Revision: 1.1 $ $Date: 2008/06/18 16:47:20 $ $Author: spiderr $ + */ + +/** + * required setup + */ +require_once( NEWSLETTERS_PKG_PATH.'BitNewsletter.php' ); +require_once( UTIL_PKG_PATH.'phpmailer/class.phpmailer.php' ); + +/** + * @package newsletters + */ +class BitNewsletterMailer { + // Set default variables for all new objects + var $From; + var $FromName; + var $Host; + var $Mailer; // Alternative to IsSMTP() + var $WordWrap; + function BitNewsletterMailer () { + global $gBitDb, $gBitSystem, $gBitLanguage; + $this->mDb = $gBitDb; + } + + // Replace the default error_handler + function error_handler( $msg ) { + global $gBitDb; + bit_error_handler( NULL, NULL, NULL, "FULFILLMENT ERROR: MISSSING PDF for ORDER $pOrderId CID ".$prod->mInfo['related_content_id'], $pdfInfo['pdf_file'], '', $prod->mDb ); + print("My Site Error"); + print("Description:"); + printf("%s", $msg); + exit; + } + + function isRecipientQueued( $pRecipientMixed, $pContentId ) { + if( BitBase::verifyId( $pRecipientMixed ) ) { + $lookupCol = 'user_id'; + } else { + $lookupCol = 'email'; + } + return( $this->mDb->getOne( "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."mail_queue` WHERE `content_id`=? AND `$lookupCol`=?", array( $pContentId, $pRecipientMixed ) ) ); + } + + function queueRecipients( $pContentId, $pNewsletterContentId, $pRecipients, $pRequeue=FALSE ) { + $ret = 0; + if( !empty( $pRecipients ) && BitBase::verifyId( $pContentId ) ) { + $queueTime = time(); + foreach( array_keys( $pRecipients ) AS $email ) { + $unsub = $this->getUnsubscription( $email, $pContentId ); + $lookup = !empty( $pRecipients[$email]['user_id'] ) ? $pRecipients[$email]['user_id'] : $email; + if( empty( $unsub ) && !$this->isRecipientQueued( $lookup, $pContentId ) ) { + $insertHash['mail_queue_id'] = $this->mDb->GenID( 'mail_queue_id' ); + $insertHash['email'] = $email; + if( !empty( $pRecipients[$email]['user_id'] ) ) { + $insertHash['user_id'] = $pRecipients[$email]['user_id']; + } + $insertHash['content_id'] = $pContentId; + $insertHash['nl_content_id'] = $pNewsletterContentId; + $insertHash['queue_date'] = $queueTime; + $this->mDb->associateInsert( BIT_DB_PREFIX.'mail_queue', $insertHash ); + $ret++; + } elseif( empty( $unsub ) && $pRequeue ) { + $bindVars = array( $queueTime, $pContentId ); + if( !empty( $pRecipients[$email]['user_id'] ) ) { + $lookupCol = 'user_id'; + $bindVars[] = $pRecipients[$email]['user_id']; + } else { + $lookupCol = 'email'; + $bindVars[] = $email; + } + $rs = $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `queue_date`=?, `begin_date`=NULL, `sent_date`=NULL, `last_read_date`=NULL, `mail_error`=NULL, `reads`=0 WHERE `content_id`=? AND `$lookupCol`=?", array( $bindVars ) ); + $ret++; + } + } + } + return $ret; + } + + + function tendQueue() { + $this->mDb->StartTrans(); + $query = "SELECT * + FROM `".BIT_DB_PREFIX."mail_queue` mq + WHERE `sent_date` IS NULL AND `mail_error` IS NULL + ORDER BY `queue_date`,`user_id`,`email` ".$this->mDb->SQLForUpdate(); + if( $rs = $this->mDb->query( $query, NULL ) ) { + while( $pick = $rs->fetchRow() ) { + $this->sendQueue( $pick ); + $this->mDb->CompleteTrans(); + $this->mDb->StartTrans(); + } + } + $this->mDb->CompleteTrans(); + } + + function sendQueue( $pQueueMixed ) { + global $gBitSmarty, $gBitSystem, $gBitLanguage; + static $body = array(); + if( is_array( $pQueueMixed ) ) { + $pick = $pQueueMixed; + } elseif( is_numeric( $pQueueMixed ) ) { + $pick = $this->mDb->GetRow( "SELECT * FROM `".BIT_DB_PREFIX."mail_queue` mq WHERE `mail_queue_id` = ? ".$this->mDb->SQLForUpdate(), array( $pQueueMixed ) ); + } + + if( !empty( $pick ) ) { + $startTime = microtime( TRUE ); + $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `begin_date`=? WHERE `mail_queue_id` = ? ", array( time(), $pick['mail_queue_id'] ) ); + if( !empty( $pick['user_id'] ) ) { + $userHash = $this->mDb->getRow( "SELECT * FROM `".BIT_DB_PREFIX."users_users` WHERE `user_id`=?", array( $pick['user_id'] ) ); + $pick['full_name'] = BitUser::getDisplayName( FALSE, $userHash ); + } else { + $pick['full_name'] = NULL; + } + if( !isset( $body[$pick['content_id']] ) ) { + $gBitSmarty->assign( 'sending', TRUE ); + // We only support sending of newsletters currently + $content = new BitNewsletterEdition( NULL, $pick['content_id'] ); + if( $content->load() ) { + $body[$pick['content_id']]['body'] = $content->render(); + $body[$pick['content_id']]['subject'] = $content->getTitle(); + $body[$pick['content_id']]['reply_to'] = $content->getField( 'reply_to', $gBitSystem->getConfig( 'site_sender_email', $_SERVER['SERVER_ADMIN'] ) ); + $body[$pick['content_id']]['object'] = $content; + } else { + bit_log_error( $this->mErrors ); + } +// $content[$pick['content_id']] = LibertyBase::getLibertyObject(); + } + + print "[ $pick[mail_queue_id] ] $pick[content_id] TO: $pick[email]\t"; + $unsub = $this->getUnsubscription( $pick['email'], $pick['content_id'] ); + if( !empty( $unsub ) ) { + print " SKIPPED (unsubscribed)
\n"; + $this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."mail_queue` WHERE `mail_queue_id`=?", array( $pick['mail_queue_id'] ) ); + } elseif( !empty( $body[$pick['content_id']] ) ) { + $pick['url_code'] = md5( $pick['content_id'].$pick['email'].$pick['queue_date'] ); + $unsub = ''; + if( $body[$pick['content_id']]['object']->mNewsletter->getField('unsub_msg') ) { + $gBitSmarty->assign( 'url_code', $pick['url_code'] ); + } + $gBitSystem->preDisplay(''); + $gBitSmarty->assign( 'sending', TRUE ); + $gBitSmarty->assign( 'unsubMessage', $unsub ); + $gBitSmarty->assign( 'trackCode', $pick['url_code'] ); + $gBitSmarty->assign( 'mid', 'bitpackage:newsletters/view_edition.tpl' ); + $htmlBody = $gBitSmarty->fetch( 'bitpackage:newsletters/mail_edition.tpl' ); + $htmlBody = bit_add_clickthrough( $htmlBody, $pick['url_code'] ); + + $mailer = new PHPMailer(); + $mailer->From = $gBitSystem->getConfig( 'bitmailer_sender_email', $gBitSystem->getConfig( 'site_sender_email', $_SERVER['SERVER_ADMIN'] ) ); + $mailer->FromName = $gBitSystem->getConfig( 'bitmailer_from', $gBitSystem->getConfig( 'site_title' ) ); + $mailer->Host = $gBitSystem->getConfig( 'bitmailer_servers', $gBitSystem->getConfig( 'kernel_server_name', '127.0.0.1' ) ); + $mailer->Mailer = $gBitSystem->getConfig( 'bitmailer_protocol', 'smtp' ); // Alternative to IsSMTP() + if( $gBitSystem->getConfig( 'bitmailer_smtp_username' ) ) { + $mailer->SMTPAuth = TRUE; + $mailer->Username = $gBitSystem->getConfig( 'bitmailer_smtp_username' ); + } + if( $gBitSystem->getConfig( 'bitmailer_smtp_password' ) ) { + $mailer->Password = $gBitSystem->getConfig( 'bitmailer_smtp_password' ); + } + $mailer->WordWrap = $gBitSystem->getConfig( 'bitmailer_word_wrap', 75 ); + if( !$mailer->SetLanguage( $gBitLanguage->getLanguage(), UTIL_PKG_PATH.'phpmailer/language/' ) ) { + $mailer->SetLanguage( 'en' ); + } + $mailer->ClearReplyTos(); + $mailer->AddReplyTo( $body[$pick['content_id']]['reply_to'], $gBitSystem->getConfig( 'bitmailer_from' ) ); + $mailer->Body = $htmlBody; + $mailer->Subject = $body[$pick['content_id']]['subject']; + $mailer->IsHTML( TRUE ); + $mailer->AltBody = ''; + $mailer->AddAddress( $pick['email'], $pick["full_name"] ); + if( $mailer->Send() ) { + print " SENT ".round( microtime( TRUE ) - $startTime, 2)." secs
\n"; flush(); + $updateQuery = "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `sent_date`=?,`url_code`=? WHERE `content_id`=? AND `email`=?"; + $this->mDb->query( $updateQuery, array( time(), $pick['url_code'], $pick['content_id'], $pick['email'] ) ); + } else { + $updateQuery = "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `mail_error`=?,`sent_date`=? WHERE `content_id`=? AND `email`=?"; + $this->mDb->query( $updateQuery, array( $mailer->ErrorInfo, time(), $pick['content_id'], $pick['email'] ) ); + $pick['error'] = $mailer->ErrorInfo; + $this->logError( $pick ); + } + } + } + } + + function trackMail( $pUrlCode ) { + global $gBitDb; + $query = "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `reads`=`reads`+1, `last_read_date`=?, `last_read_ip`=? WHERE `url_code`=? "; + $gBitDb->query( $query, array( time(), $_SERVER['REMOTE_ADDR'], $pUrlCode ) ); + } + + function logError( $pInfo ) { + if( !empty( $pInfo['url_code'] ) && !$this->mDb->getOne( "SELECT `url_code` FROM `".BIT_DB_PREFIX."mail_errors` WHERE `url_code`=?", array( $pInfo['url_code'] ) ) ) { + $store['url_code'] = $pInfo['url_code']; + $store['user_id'] = !empty( $pInfo['user_id'] ) ? $pInfo['user_id'] : NULL; + $store['content_id'] = !empty( $pInfo['content_id'] ) ? $pInfo['content_id'] : NULL; + $store['email'] = !empty( $pInfo['email'] ) ? $pInfo['email'] : NULL; + $store['error_message'] = $pInfo['error']; + $store['error_date'] = time(); + $this->mDb->associateInsert( BIT_DB_PREFIX."mail_errors", $store ); + } + print "ERROR: ".$pInfo['error']."\n"; + } + + // Looks up the code from the url to determine if the unsubscribe URL is valid. + // Can be statically called + function lookupSubscription( $pLookup ) { + global $gBitDb; + $ret = NULL; + if( is_array( $pLookup ) ) { + $query = "SELECT mq.*, lc.title, tct.*, uu.`real_name`, uu.`login`, uu.`email` FROM `".BIT_DB_PREFIX."mail_queue` mq + INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( mq.`content_id`=lc.`content_id` ) + INNER JOIN `".BIT_DB_PREFIX."liberty_content_types` tct ON( tct.`content_type_guid`=lc.`content_type_guid` ) + LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON( mq.`user_id`=uu.`user_id` ) + WHERE mq.`".key( $pLookup )."`=? "; + $ret = $gBitDb->getRow( $query, array( current( $pLookup ) ) ); + } + return( $ret ); + } + + // Accepts a single row has containing the column of mail_subscriptions as the key to lookup the unsubscription info + // Can be statically called + function getUnsubscriptions( $pMixed ) { + global $gBitDb; + $ret = NULL; + if( is_array( $pMixed ) ) { + $col = key( $pMixed ); + $bindVars[] = current( $pMixed ); + $query = "SELECT ms.`content_id` AS `hash_key`, ms.*, uu.*, lc.title + FROM `".BIT_DB_PREFIX."mail_subscriptions` ms + LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON( ms.`user_id`=uu.`user_id` ) + LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( ms.`content_id`=lc.`content_id` ) + WHERE ms.`$col`=? "; + $ret = $gBitDb->getAssoc( $query, $bindVars ); + } + return( $ret ); + } + + function getUnsubscription( $pEmail, $pContentId ) { + global $gBitDb; + return $gBitDb->getRow( "SELECT * FROM `".BIT_DB_PREFIX."mail_subscriptions` ms LEFT JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`user_id`=ms.`user_id`) WHERE (ms.`content_id`=? OR `unsubscribe_all`='y') AND (ms.`email`=? OR uu.`email`=?)", array( $pContentId, $pEmail, $pEmail ) ); + } + + function storeSubscriptions( $pSubHash ) { + global $gBitSystem, $gBitDb; + $ret = FALSE; + $query = "delete from `".BIT_DB_PREFIX."mail_subscriptions` where `".key( $pSubHash['sub_lookup'] )."`=?"; + $result = $gBitDb->query($query, array( current( $pSubHash['sub_lookup'] ) ) ); + $ret = TRUE; + if( !empty( $pSubHash['unsub_content'] ) ) { + foreach( $pSubHash['unsub_content'] as $conId ) { + $storeHash = array(); + $storeHash['content_id'] = $conId; + $storeHash['unsubscribe_all'] = !empty( $pSubHash['unsubscribe_all'] ) ? 'y' : NULL; + $storeHash['unsubscribe_date'] = time(); + $storeHash[key( $pSubHash['sub_lookup'] )] = current( $pSubHash['sub_lookup'] ); + if( !empty( $pSubHash['response_content_id'] ) ) { + $storeHash['response_content_id'] = $pSubHash['response_content_id']; + } + $gBitDb->associateInsert( BIT_DB_PREFIX."mail_subscriptions", $storeHash ); + } + } + return $ret; + } + + function getQueue( &$pListHash ) { + $ret = array(); + + LibertyContent::prepGetList( $pListHash ); + + $query = "SELECT mq.`mail_queue_id` AS `hash_key`, mq.*, lc.`title`, lc2.`title` AS newsletter_title + FROM `".BIT_DB_PREFIX."mail_queue` mq + INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.content_id=mq.content_id) + LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lc2 ON (lc2.content_id=mq.nl_content_id) + WHERE begin_date IS NULL + ORDER BY queue_date"; + if( $rs = $this->mDb->query( $query ) ) { + $ret = $rs->getAssoc(); + } + + return $ret; + } + + function expungeQueueRow( $pQueueId ) { + if( BitBase::verifyId( $pQueueId ) ) { + $this->mDb->query( "DELETE FROM ".BIT_DB_PREFIX."mail_queue` WHERE `mail_queue_id`=?", array( $pQueueId ) ); + } + } + + function storeClickthrough( $pUrlCode ) { + global $gBitDb; + + $uri = substr( preg_replace( '/[&\?]?ct=[a-z0-9]{32}/', '', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] ), 0, 250 ); + $query = "SELECT mc.`clicks`, mq.`content_id`, mq.`user_id`, mc.`clicked_url` FROM `".BIT_DB_PREFIX."mail_queue` mq + LEFT JOIN `".BIT_DB_PREFIX."mail_clickthrough` mc ON (mq.`user_id`=mc.`user_id` AND mq.`content_id`=mc.`content_id` AND mc.`clicked_url`=?) + WHERE `url_code`=?"; + if( $row = $gBitDb->getRow( $query, array( $uri, $pUrlCode ) ) ) { + if( $row['clicked_url'] ) { + $gBitDb->query( "UPDATE `".BIT_DB_PREFIX."mail_clickthrough` SET `clicks`=`clicks`+1 WHERE `user_id`=? AND `content_id`=? AND `clicked_url`=? ", array( $row['user_id'], $row['content_id'], $row['clicked_url'] ) ); + } else { + $row['clicks'] = 1; + $row['clicked_url'] = $uri; + $gBitDb->associateInsert( BIT_DB_PREFIX.'mail_clickthrough', $row ); + } + } + } +} + +// This will insert a ticket on all template URL's that have GET parameters. +function bit_add_clickthrough( $pSource, $pUrlCode ) { + global $gBitUser, $gUrlCode; + + $gUrlCode = $pUrlCode; + + $pcre = '%href[\s]*=[\s]*["\']*((http|https)://[^\s"\']+?)(.*)%sU'; + + $ret = preg_replace_callback( $pcre, 'process_clickthrough_match', $pSource ); + + return $ret; + +} + + +function process_clickthrough_match( $matches ) { + global $gUrlCode; + $ret = $matches[0]; + if( strpos( $matches[0], '?' ) ) { + $ret .= '&ct='.$gUrlCode; + } else { + $ret .= '?ct='.$gUrlCode; + } + return $ret; +} + +?> diff --git a/admin/mail_queue.php b/admin/mail_queue.php index 240c0f4..a6c05ef 100644 --- a/admin/mail_queue.php +++ b/admin/mail_queue.php @@ -9,25 +9,25 @@ require_once( '../../bit_setup_inc.php' ); $gBitSystem->verifyPermission( 'p_mail_admin' ); $gBitSystem->verifyPermission( 'newsletters' ); -require_once( NEWSLETTERS_PKG_PATH.'BitMailer.php' ); -global $gBitMailer; -$gBitMailer = new BitMailer(); +require_once( NEWSLETTERS_PKG_PATH.'BitNewsletterMailer.php' ); +global $gBitNewsletterMailer; +$gBitNewsletterMailer = new BitNewsletterMailer(); if( !empty( $_REQUEST['batch_command'] ) && !empty( $_REQUEST['queue_id'] ) ) { if( $_REQUEST['batch_command'] == 'delete' ) { foreach( $_REQUEST['queue_id'] as $qId ) { - $gBitMailer->expungeQueueRow( $qId ); + $gBitNewsletterMailer->expungeQueueRow( $qId ); } } elseif( $_REQUEST['batch_command'] == 'send' && !empty( $_REQUEST['queue_id'] ) ) { foreach( $_REQUEST['queue_id'] as $queueId ) { - $gBitMailer->sendQueue( $queueId ); + $gBitNewsletterMailer->sendQueue( $queueId ); } } } if( empty( $_REQUEST['batch_command'] ) || $_REQUEST['batch_command'] != 'send' ) { $listHash = array(); - $queue = $gBitMailer->getQueue( $listHash ); + $queue = $gBitNewsletterMailer->getQueue( $listHash ); $gBitSmarty->assign_by_ref( 'queue', $queue ); $gBitSystem->display( 'bitpackage:newsletters/mail_queue.tpl' ); diff --git a/admin/send.php b/admin/send.php index 66e97e7..8c1b70b 100644 --- a/admin/send.php +++ b/admin/send.php @@ -1,6 +1,6 @@ verifyPackage( 'newsletters' ); @@ -38,9 +38,9 @@ if( $gContent->isValid() && isset( $_REQUEST['preview'] ) && isset( $_REQUEST['s $gBitSmarty->assign( 'sending', TRUE ); } elseif( $gContent->isValid() && isset( $_REQUEST["send"] ) ) { if( $emails = $gContent->getRecipients( $_REQUEST['send_group'], $validated, !empty( $_REQUEST['test_mode'] ) ) ) { - global $gBitMailer; - $gBitMailer = new BitMailer(); - $queueCount = $gBitMailer->queueRecipients( $gContent->mContentId, $gContent->mNewsletter->mContentId, $emails, !empty( $_REQUEST['test_mode'] ) ); + global $gBitNewsletterMailer; + $gBitNewsletterMailer = new BitNewsletterMailer(); + $queueCount = $gBitNewsletterMailer->queueRecipients( $gContent->mContentId, $gContent->mNewsletter->mContentId, $emails, !empty( $_REQUEST['test_mode'] ) ); $feedback['success'] = $queueCount.' '.tra( 'emails were queued to be sent:' ).' '.$gContent->getTitle(); $gContent->mEditionId = NULL; } else { diff --git a/admin/tend_mail_queue.php b/admin/tend_mail_queue.php index ba9b79d..df84eda 100644 --- a/admin/tend_mail_queue.php +++ b/admin/tend_mail_queue.php @@ -15,10 +15,10 @@ require_once( '../../bit_setup_inc.php' ); if( $gBitSystem->isPackageActive( 'newsletters' ) ) { - require_once( NEWSLETTERS_PKG_PATH.'BitMailer.php' ); - global $gBitMailer; - $gBitMailer = new BitMailer(); - $gBitMailer->tendQueue(); + require_once( NEWSLETTERS_PKG_PATH.'BitNewsletterMailer.php' ); + global $gBitNewsletterMailer; + $gBitNewsletterMailer = new BitNewsletterMailer(); + $gBitNewsletterMailer->tendQueue(); } ?> diff --git a/bit_setup_inc.php b/bit_setup_inc.php index 76d14d6..d052cb6 100644 --- a/bit_setup_inc.php +++ b/bit_setup_inc.php @@ -16,8 +16,8 @@ if( $gBitSystem->isPackageActive( NEWSLETTERS_PKG_NAME ) ) { ); $gBitSystem->registerAppMenu( $menuHash ); if( isset( $_GET['ct'] ) && strlen( $_GET['ct'] ) == 32 ) { - require_once( NEWSLETTERS_PKG_PATH.'BitMailer.php' ); - BitMailer::storeClickthrough( $_GET['ct'] ); + require_once( NEWSLETTERS_PKG_PATH.'BitNewsletterMailer.php' ); + BitNewsletterMailer::storeClickthrough( $_GET['ct'] ); } } ?> diff --git a/sub.php b/sub.php index 717f6da..31de7b2 100644 --- a/sub.php +++ b/sub.php @@ -1,6 +1,6 @@ verifyPackage( 'newsletters' ); @@ -63,19 +63,19 @@ $gBitSmarty->assign('url_subscribe', httpPrefix(). $foo["path"]); if( isset( $_REQUEST['c'] ) ) { //$gBitDb->debug(); $unsubs = array(); - if( isset( $_REQUEST['c'] ) && strlen( $_REQUEST['c'] ) == 32 && ($subInfo = BitMailer::lookupSubscription( array( 'url_code' => $_REQUEST['c'] ) )) ) { + if( isset( $_REQUEST['c'] ) && strlen( $_REQUEST['c'] ) == 32 && ($subInfo = BitNewsletterMailer::lookupSubscription( array( 'url_code' => $_REQUEST['c'] ) )) ) { if( !empty( $subInfo['user_id'] ) && BitBase::verifyId( $subInfo['user_id'] ) ) { $lookup['user_id'] = $subInfo['user_id']; } else { $lookup['email'] = $subInfo['email']; } - $unsubs = BitMailer::getUnsubscriptions( $lookup ); + $unsubs = BitNewsletterMailer::getUnsubscriptions( $lookup ); } else { - if( !$subInfo = BitMailer::lookupSubscription( array( 'user_id' => $gBitUser->mUserId ) ) ) { + if( !$subInfo = BitNewsletterMailer::lookupSubscription( array( 'user_id' => $gBitUser->mUserId ) ) ) { $subInfo = $gBitUser->mInfo; } $lookup['user_id'] = $gBitUser->mUserId; - $unsubs = BitMailer::getUnsubscriptions( $lookup ); + $unsubs = BitNewsletterMailer::getUnsubscriptions( $lookup ); } if( isset( $_REQUEST["update"] ) ) { @@ -95,12 +95,12 @@ if( isset( $_REQUEST['c'] ) ) { } } - if( BitMailer::storeSubscriptions( $subHash ) ) { + if( BitNewsletterMailer::storeSubscriptions( $subHash ) ) { $feedback['success'] = tra( "Your subscriptions were updated." ); } else { $feedback['error'] = tra( "Subscriptions were not updated." ); } - $unsubs = BitMailer::getUnsubscriptions( $lookup ); + $unsubs = BitNewsletterMailer::getUnsubscriptions( $lookup ); } foreach( $unsubs as $sub ) { if( !empty( $sub['unsubscribe_all'] ) ) { diff --git a/templates/center_list_newsletters.php b/templates/center_list_newsletters.php index 809531d..853fe6e 100644 --- a/templates/center_list_newsletters.php +++ b/templates/center_list_newsletters.php @@ -1,6 +1,6 @@ verifyPackage( 'newsletters' ); diff --git a/track.php b/track.php index 17daef2..d39ecb3 100644 --- a/track.php +++ b/track.php @@ -14,11 +14,11 @@ * Initialization */ require_once( '../bit_setup_inc.php' ); -include_once( NEWSLETTERS_PKG_PATH.'BitMailer.php' ); +include_once( NEWSLETTERS_PKG_PATH.'BitNewsletterMailer.php' ); if( isset( $_REQUEST["sub"] ) || $gBitUser->isRegistered() ) { - if( isset( $_REQUEST["sub"] ) && strlen( $_REQUEST["sub"] ) == 32 && ($subInfo = BitMailer::lookupSubscription( array( 'url_code' => $_REQUEST["sub"] ) )) ) { - BitMailer::trackMail( $subInfo['url_code'] ); + if( isset( $_REQUEST["sub"] ) && strlen( $_REQUEST["sub"] ) == 32 && ($subInfo = BitNewsletterMailer::lookupSubscription( array( 'url_code' => $_REQUEST["sub"] ) )) ) { + BitNewsletterMailer::trackMail( $subInfo['url_code'] ); } } -- cgit v1.3