1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
<?php
/**
* $Header: /cvsroot/bitweaver/_bit_newsletters/Attic/BitMailer.php,v 1.13 2005/12/29 21:55:55 spiderr Exp $
*
* Copyright (c) 2004 bitweaver.org
* All Rights Reserved. See copyright.txt for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details
*
* $Id: BitMailer.php,v 1.13 2005/12/29 21:55:55 spiderr Exp $
*
* Class that handles editions of newsletters
* @package newsletters
*
* created 2005/12/08
*
* @author spiderr <spider@steelsun.com>
*
* @version $Revision: 1.13 $ $Date: 2005/12/29 21:55:55 $ $Author: spiderr $
*/
/**
* required setup
*/
require_once( NEWSLETTERS_PKG_PATH.'BitNewsletter.php' );
require_once( UTIL_PKG_PATH.'phpmailer/class.phpmailer.php' );
class BitMailer extends phpmailer {
// 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;
$this->From = $gBitSystem->getPreference( 'bitmailer_sender_email', $gBitSystem->getPreference( 'sender_email', $_SERVER['SERVER_ADMIN'] ) );
$this->FromName = $gBitSystem->getPreference( 'bitmailer_from', $gBitSystem->getPreference( 'siteTitle' ) );
$this->Host = $gBitSystem->getPreference( 'bitmailer_servers', $gBitSystem->getPreference( 'feature_server_name', $_SERVER['HTTP_HOST'] ) );
$this->Mailer = $gBitSystem->getPreference( 'bitmailer_protocol', 'smtp' ); // Alternative to IsSMTP()
$this->WordWrap = $gBitSystem->getPreference( 'bitmailer_word_wrap', 75 );
if( !$this->SetLanguage( $gBitLanguage->getLanguage(), UTIL_PKG_PATH.'phpmailer/language/' ) ) {
$this->SetLanguage( 'en' );
}
}
// 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 queueRecipients( $pContentId, $pNewsletterContentId, $pRecipients ) {
$ret = 0;
if( !empty( $pRecipients ) && BitBase::verifyId( $pContentId ) ) {
$queueTime = time();
foreach( array_keys( $pRecipients ) AS $email ) {
$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.'tiki_mail_queue', $insertHash );
$ret++;
}
}
return $ret;
}
function tendQueue() {
global $gBitSmarty;
$body = array();
$this->mDb->StartTrans();
$query = "SELECT *
FROM `".BIT_DB_PREFIX."tiki_mail_queue` tmq
WHERE `sent_date` IS NULL ".$this->mDb->SQLForUpdate();
while( ($rs = $this->mDb->query( $query, NULL, 1 )) && $rs->RowCount() ) {
$pick = $rs->fields;
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']] ) ) {
// 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']]['object'] = $content;
}
// $content[$pick['content_id']] = LibertyBase::getLibertyObject();
}
if( !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->mInfo['unsub_msg'] ) {
$gBitSmarty->assign( 'url_code', $pick['url_code'] );
$gBitSmarty->assign( 'sending', TRUE );
$unsub = $gBitSmarty->fetch( 'bitpackage:newsletters/unsubscribe_inc.tpl' );
}
$htmlBody = $unsub . $body[$pick['content_id']]['body'] . $unsub . '<img src="'.NEWSLETTERS_PKG_URI.'track.php?sub='.$pick['url_code'].'" alt="" />';
if( !$this->sendMail( $pick, $body[$pick['content_id']]['subject'], $htmlBody ) ) {
$this->logError( $pick );
}
$updateQuery = "UPDATE `".BIT_DB_PREFIX."tiki_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'] ) );
$this->mDb->CompleteTrans();
$this->mDb->StartTrans();
}
$rs->MoveNext();
}
$this->mDb->CompleteTrans();
$this->mDb->query( "UPDATE tiki_mail_queue set sent_date=NULL" );
}
function sendMail( $pRecipient, $pSubject, $pHtmlBody ) {
$ret = TRUE;
$this->Body = $pHtmlBody;
$this->Subject = $pSubject;
$this->IsHTML( TRUE );
$this->AltBody = '';
$this->AddAddress( $pRecipient['email'], $pRecipient["full_name"] );
if(!$this->Send()) {
$ret = FALSE;
}
// Clear all addresses and attachments for next loop
$this->ClearAddresses();
$this->ClearAttachments();
return $ret;
}
function trackMail( $pUrlCode ) {
global $gBitDb;
$query = "UPDATE `".BIT_DB_PREFIX."tiki_mail_queue` SET `reads`=`reads`+1, `last_read_date`=? WHERE `url_code`=? ";
$gBitDb->query( $query, array( time(), $pUrlCode ) );
}
function logError( $pInfo ) {
if( !empty( $pInfo['url_code'] ) && !$this->mDb->getOne( "SELECT `url_code` FROM `".BIT_DB_PREFIX."tiki_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'] = $this->ErrorInfo;
$store['error_date'] = time();
$this->mDb->associateInsert( BIT_DB_PREFIX."tiki_mail_errors", $store );
}
}
// 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 tmq.*, tc.title, tct.*, uu.`real_name`, uu.`login`, uu.`email` FROM `".BIT_DB_PREFIX."tiki_mail_queue` tmq
INNER JOIN `".BIT_DB_PREFIX."tiki_content` tc ON( tmq.`content_id`=tc.`content_id` )
INNER JOIN `".BIT_DB_PREFIX."tiki_content_types` tct ON( tct.`content_type_guid`=tc.`content_type_guid` )
LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON( tmq.`user_id`=uu.`user_id` )
WHERE tmq.`".key( $pLookup )."`=? ";
$ret = $gBitDb->getRow( $query, array( current( $pLookup ) ) );
}
return( $ret );
}
// Accepts a single row has containing the column of tiki_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 tms.`nl_content_id` AS `hash_key`, tms.*, uu.*, tc.title
FROM `".BIT_DB_PREFIX."tiki_mail_subscriptions` tms
LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON( tms.`user_id`=uu.`user_id` )
LEFT OUTER JOIN `".BIT_DB_PREFIX."tiki_content` tc ON( tms.`nl_content_id`=tc.`content_id` )
WHERE tms.`$col`=? ";
$ret = $gBitDb->getAssoc( $query, $bindVars );
}
return( $ret );
}
function storeSubscriptions( $pSubHash ) {
global $gBitSystem, $gBitDb;
$ret = FALSE;
$query = "delete from `".BIT_DB_PREFIX."tiki_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['nl_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."tiki_mail_subscriptions", $storeHash );
}
}
return $ret;
}
}
?>
|