From dd08c0f078e3e30f0935e5d91ca2a1a7c038efd9 Mon Sep 17 00:00:00 2001 From: Cameron Date: Thu, 21 May 2015 15:51:37 -0700 Subject: [PATCH] Fixes altBody mailing issue. --- e107_handlers/bounce_handler.php | 20 ++++++-- e107_handlers/mail.php | 42 +++++++++++----- e107_handlers/mail_manager_class.php | 72 +++++++++++++++++++--------- 3 files changed, 96 insertions(+), 38 deletions(-) diff --git a/e107_handlers/bounce_handler.php b/e107_handlers/bounce_handler.php index 760b799ea..81463ca0a 100644 --- a/e107_handlers/bounce_handler.php +++ b/e107_handlers/bounce_handler.php @@ -109,7 +109,12 @@ class e107Bounce { if($errors = $this->setUser_Bounced($e107_userid)) { - // $message .= print_a($errors,true); + if($this->debug) + { + echo "

Errors

"; + print_a($errors); + } + } } @@ -194,13 +199,18 @@ class e107Bounce function setUser_Bounced($bounceString = '', $email='' ) { - if(!$email && !$bounceString){ return; } + if(!$email && !$bounceString) + { + return false; + } // echo "Email bounced ID: ".$id_or_email; - require_once(e_HANDLER.'mail_manager_class.php'); - $mailManager = new e107MailManager(); + $mailManager = e107::getBulkEmail(); + + $mailManager->controlDebug($this->debug); + if ($errors = $mailManager->markBounce($bounceString, $email)) - { + { return $errors; // Failure } diff --git a/e107_handlers/mail.php b/e107_handlers/mail.php index 986313fff..e3ca1abca 100644 --- a/e107_handlers/mail.php +++ b/e107_handlers/mail.php @@ -570,7 +570,7 @@ class e107Email extends PHPMailer $this->MsgHTML($message); // Theoretically this should do everything, including handling of inline images. - + } else { // generate the plain text as the sole part of the email @@ -781,8 +781,8 @@ class e107Email extends PHPMailer if($this->debug) { - echo "

e107Email::arraySet() - line ".__LINE__."

"; - print_a($tmpl); + // echo "

e107Email::arraySet() - line ".__LINE__."

"; + // print_a($tmpl); } unset($eml['add_html_header']); // disable other headers when template is used. @@ -865,8 +865,9 @@ class e107Email extends PHPMailer if($this->debug) { - echo "

e107Email::arraySet() - line ".__LINE__."

"; - print_a($eml); + // echo "

e107Email::arraySet() - line ".__LINE__."

"; + return 0; + // print_a($eml); //$this->PreSend(); //$debugEml = $this->GetSentMIMEMessage(). //print_a($debugEml); @@ -947,7 +948,7 @@ class e107Email extends PHPMailer $result = TRUE; // Temporary 'success' flag $this->SendCount++; - if (($this->logEnable == 0) || ($this->logEnable == 2)) + if ($this->debug == false && (($this->logEnable == 0) || ($this->logEnable == 2)) ) { // prevent user/script details being exposed in X-PHP-Script header $oldphpself = $_SERVER['PHP_SELF']; @@ -969,8 +970,11 @@ class e107Email extends PHPMailer } else { // Debug - $result = TRUE; - //print_a($this); + $result = true; + // echo "

SendEmail()->Body

"; + // print_a($this->Body); + // echo "

SendEmail()->AltBody

"; + // print_a($this->AltBody); if (($this->logEnable == 3) && (($this->SendCount % 7) == 4)) $result = FALSE; // Fail one email in 7 for testing } @@ -1015,6 +1019,10 @@ class e107Email extends PHPMailer } + function setDebug($val) + { + $this->debug = $val; + } /** * Called after a bulk mailing completed, to tidy up nicely @@ -1126,18 +1134,30 @@ class e107Email extends PHPMailer $this->IsHTML(true); $this->Body = $message; //print_a($message); - $textMsg = str_replace(array('
', '
'), "\n", $message); // Modified to make sure newlines carried through + $textMsg = str_replace("\n", "", $message); + $textMsg = str_replace(array('
', '
'), "\n", $textMsg); // Modified to make sure newlines carried through $textMsg = preg_replace('#^.*?#', '', $textMsg); // Knock off everything up to and including the body statement (if present) $textMsg = preg_replace('#.*$#', '', $textMsg); // Knock off everything after and including the (if present) $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$textMsg))); - if (!empty($textMsg) && empty($this->AltBody)) + + if($this->debug) + { + echo "

".__METHOD__.' $textMsg Line: '.__LINE__.'

'; + print_a($textMsg); + } + + if(!empty($textMsg)) // Always set it, even if AltBody is empty. { $this->AltBody = html_entity_decode($textMsg); + } - if (empty($this->AltBody)) + + if(empty($this->AltBody)) { $this->AltBody = 'To view this email message, enable HTML!' . "\n\n"; } + + } } // End of e107Mailer class diff --git a/e107_handlers/mail_manager_class.php b/e107_handlers/mail_manager_class.php index a8b5bb6df..17fb07315 100644 --- a/e107_handlers/mail_manager_class.php +++ b/e107_handlers/mail_manager_class.php @@ -500,13 +500,13 @@ class e107MailManager */ protected function checkDB($which = 1) { - if (($which == 1) && ($this->db == NULL)) + if (($which == 1) && ($this->db == null)) { - $this->db = new db; + $this->db = e107::getDb('mail1'); } - if (($which == 2) && ($this->db2 == NULL)) + if (($which == 2) && ($this->db2 == null)) { - $this->db2 = new db; + $this->db2 = e107::getDb('mail2');; } } @@ -621,17 +621,17 @@ class e107MailManager { if (!$this->queryActive) { - return FALSE; + return false; } - if ($result = $this->db->db_Fetch(MYSQL_ASSOC)) + if ($result = $this->db->fetch(MYSQL_ASSOC)) { $this->queryActive--; return $this->dbToBoth($result); } else { - $this->queryActive = FALSE; // Make sure no further attempts to read emails - return FALSE; + $this->queryActive = false; // Make sure no further attempts to read emails + return false; } } @@ -714,7 +714,8 @@ class e107MailManager } $this->currentTextBody = $email['mail_body_alt']; // May be null } - + + $mailToSend = $this->makeEmailBlock($email); // Substitute mail-specific variables, attachments etc @@ -729,13 +730,30 @@ class e107MailManager print_a($preview); $logName = "mailout_simulation_".$email['mail_source_id']; e107::getLog()->addDebug("Sending Email to <".$email['mail_recipient_name']."> ".$email['mail_recipient_email'])->toFile($logName,'Mailout Simulation Log',true); - $result = true; + $result = true; + + + $this->mailer->setDebug(true); + echo "

SendEmail()->Body

"; + print_a($this->mailer->Body); + echo "

SendEmail()->AltBody

"; + print_a($this->mailer->AltBody); + echo "

_________________________________________________________________________

"; + return; + + } - else + + // else { $result = $this->mailer->sendEmail($email['mail_recipient_email'], $email['mail_recipient_name'], $mailToSend, TRUE); } + if($this->debugMode) + { + return true; + } + // Try and send @@ -973,7 +991,7 @@ class e107MailManager $result = array_merge($result, $email['mail_overrides']); } - $title = "

".__METHOD__." Line: ".__LINE__."

"; + // $title = "

".__METHOD__." Line: ".__LINE__."

"; // e107::getAdminLog()->addDebug($title.print_a($email,true),true); if(!empty($email['mail_media'])) @@ -981,7 +999,7 @@ class e107MailManager $result['media'] = $email['mail_media']; } - $title2 = "

".__METHOD__." Line: ".__LINE__."

"; + // $title2 = "

".__METHOD__." Line: ".__LINE__."

"; // e107::getAdminLog()->addDebug($title2.print_a($result,true),true); $result['shortcodes']['MAILREF'] = $email['mail_source_id']; @@ -1368,11 +1386,14 @@ class e107MailManager $bounceInfo = array('mail_bounce_string' => $bounceString, 'mail_recipient_email' => $emailAddress); // Ready for event data $errors = array(); // Log all errors, at least until proven $vals = explode('/', $bounceString); // Should get one or four fields - - // echo "

Bounce String

"; - // print_a($bounceString); - // echo "

Vals

"; - // print_a($vals); + + if($this->debugMode) + { + echo "

Bounce String

"; + print_a($bounceString); + echo "

Vals

"; + print_a($vals); + } if (!is_numeric($vals[0])) // Email recipient user id number (may be zero) { @@ -1448,13 +1469,13 @@ class e107MailManager if(!$this->db->update('mail_content', '`mail_bounce_count` = `mail_bounce_count` + 1 WHERE `mail_source_id` = '.$vals[1])) { - e107::getAdminLog()->add('Unable to increment bounce-count on mail_source_id='.$vals[1],$bounceInfo, E_LOG_FATAL, 'BOUNCE',LOG_TO_ROLLING); + e107::getAdminLog()->add('Unable to increment bounce-count on mail_source_id='.$vals[1],$bounceInfo, E_LOG_FATAL, 'BOUNCE', LOG_TO_ROLLING); } if(!$this->db->update('mail_recipients', '`mail_status` = '.MAIL_STATUS_BOUNCED.' WHERE `mail_target_id` = '.$vals[2])) { - e107::getAdminLog()->add('Unable to update recipient mail_status to bounce on mail_target_id = '.$vals[2],$bounceInfo, E_LOG_FATAL, 'BOUNCE',LOG_TO_ROLLING); + e107::getAdminLog()->add('Unable to update recipient mail_status to bounce on mail_target_id = '.$vals[2],$bounceInfo, E_LOG_FATAL, 'BOUNCE', LOG_TO_ROLLING); } $addons = array_keys($row['mail_selectors']); // trigger e_mailout.php addons. 'bounce' method. @@ -1503,12 +1524,19 @@ class e107MailManager $errors[] = $err; } } - + if (!empty($errors)) { + $logErrors =$bounceInfo; + $logErrors['user_id'] = $uid; + $logErrors['mailshot'] = $vals[1]; + $logErrors['mailshot_recipient'] = $vals[2]; + $logErrors['errors'] = $errors; + $logErrors['email'] = $emailAddress; + $logErrors['bounceString'] = $bounceString; $logString = $bounceString.' ('.$emailAddress.')[!br!]'.implode('[!br!]',$errors).implode('[!br!]',$bounceInfo); // e107::getAdminLog()->e_log_event(10,-1,'BOUNCE','Bounce receive error',$logString, FALSE,LOG_TO_ROLLING); - e107::getAdminLog()->add('Bounce receive error',$logString, E_LOG_WARNING, 'BOUNCE', LOG_TO_ROLLING); + e107::getAdminLog()->add('Bounce receive error',$logErrors, E_LOG_WARNING, 'BOUNCE', LOG_TO_ROLLING); return $errors; } else